jinja_partials 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Michael Kennedy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,142 @@
1
+ Metadata-Version: 2.1
2
+ Name: jinja_partials
3
+ Version: 0.1.1
4
+ Summary: Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.
5
+ Home-page: https://github.com/mikeckennedy/jinja_partials
6
+ Author: Michael Kennedy
7
+ Author-email: michael@talkpython.fm
8
+ License: MIT
9
+ Platform: UNKNOWN
10
+ Classifier: Development Status :: 2 - Pre-Alpha
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.6
15
+ Classifier: Programming Language :: Python :: 3.7
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+
22
+ # Jinja Partials
23
+
24
+ Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.
25
+ (There is also a [Pyramid/Chameleon version here](https://github.com/mikeckennedy/chameleon_partials).)
26
+
27
+ ## Overview
28
+
29
+ When building real-world web apps with Flask + Jinja2, it's easy to end up with repeated HTML fragments.
30
+ Just like organizing code for reuse, it would be ideal to reuse smaller sections of HTML template code.
31
+ That's what this library is all about.
32
+
33
+ ## Example
34
+
35
+ This project comes with a sample flask application (see the `example` folder). This app displays videos
36
+ that can be played on YouTube. The image, subtitle of author and view count are reused throughout the
37
+ app. Here's a visual:
38
+
39
+ ![](https://raw.githubusercontent.com/mikeckennedy/jinja_partials/main/readme_resources/reused-html-visual.png)
40
+
41
+ Check out the [**demo / example application**](https://github.com/mikeckennedy/jinja_partials/tree/main/example)
42
+ to see it in action.
43
+
44
+ ## Installation
45
+
46
+ It's just `pip install jinja-partials` and you're all set with this pure Python package.
47
+
48
+ ## Usage
49
+
50
+ Using the library is incredible easy. The first step is to register the partial method with Jinja and Flask.
51
+ Do this once at app startup:
52
+
53
+ ```python
54
+ import flask
55
+ import jinja_partials
56
+
57
+ app = flask.Flask(__name__)
58
+
59
+ jinja_partials.register_extensions(app)
60
+ # ...
61
+ ```
62
+
63
+ You can also use this library in your FastAPI (or Starlette) project!
64
+ ```python
65
+ from fastapi.templating import Jinja2Templates
66
+ # or `from starlette.templating import Jinja2Templates`
67
+
68
+ import jinja_partials
69
+
70
+ templates = Jinja2Templates("tests/test_templates")
71
+
72
+ jinja_partials.register_starlette_extensions(templates)
73
+ # ...
74
+ ```
75
+
76
+ Next, you define your main HTML (Jinja2) templates as usual. Then
77
+ define your partial templates. I recommend locating and naming them accordingly:
78
+
79
+ ```
80
+ ├── templates
81
+ │   ├── home
82
+ │   │   ├── index.html
83
+ │   │   └── listing.html
84
+ │   └── shared
85
+ │   ├── _layout.html
86
+ │   └── partials
87
+ │   ├── video_image.html
88
+ │   └── video_square.html
89
+ ```
90
+
91
+ Notice the `partials` subfolder in the `templates/shared` folder.
92
+
93
+ The templates are just HTML fragments. Here is a stand-alone one for the YouTube thumbnail from
94
+ the example app:
95
+
96
+ ```html
97
+ <img src="https://img.youtube.com/vi/{{ video.id }}/maxresdefault.jpg"
98
+ class="img img-responsive {{ ' '.join(classes) }}"
99
+ alt="{{ video.title }}"
100
+ title="{{ video.title }}">
101
+ ```
102
+
103
+ Notice that an object called `video` and list of classes are passed in as the model.
104
+
105
+ Templates can also be nested. Here is the whole single video fragment with the image as well as other info
106
+ linking out to YouTube:
107
+
108
+ ```html
109
+ <div>
110
+ <a href="https://www.youtube.com/watch?v={{ video.id }}" target="_blank">
111
+ {{ render_partial('shared/partials/video_image.html', video=video) }}
112
+ </a>
113
+ <a href="https://www.youtube.com/watch?v={{ video.id }}" target="_blank"
114
+ class="author">{{ video.author }}</a>
115
+ <div class="views">{{ "{:,}".format(video.views) }} views</div>
116
+ </div>
117
+ ```
118
+
119
+ Now you see the `render_partial()` method. It takes the subpath into the templates folder and
120
+ any model data passed in as keyword arguments.
121
+
122
+ We can finally generate the list of video blocks as follows:
123
+
124
+ ```html
125
+ {% for v in videos %}
126
+
127
+ <div class="col-md-3 video">
128
+ {{ render_partial('shared/partials/video_square.html', video=v) }}
129
+ </div>
130
+
131
+ {% endfor %}
132
+ ```
133
+
134
+ This time, we reframe each item in the list from the outer template (called `v`) as the `video` model
135
+ in the inner HTML section.
136
+
137
+
138
+ ## Why not just use `include` or `macro` from Jinja?
139
+
140
+ The short answer is they are nearly the same, but both fall short in different ways.
141
+ For a more detailed response, see the discussion on [**issue #1**](https://github.com/mikeckennedy/jinja_partials/issues/1)
142
+
@@ -0,0 +1,120 @@
1
+ # Jinja Partials
2
+
3
+ Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.
4
+ (There is also a [Pyramid/Chameleon version here](https://github.com/mikeckennedy/chameleon_partials).)
5
+
6
+ ## Overview
7
+
8
+ When building real-world web apps with Flask + Jinja2, it's easy to end up with repeated HTML fragments.
9
+ Just like organizing code for reuse, it would be ideal to reuse smaller sections of HTML template code.
10
+ That's what this library is all about.
11
+
12
+ ## Example
13
+
14
+ This project comes with a sample flask application (see the `example` folder). This app displays videos
15
+ that can be played on YouTube. The image, subtitle of author and view count are reused throughout the
16
+ app. Here's a visual:
17
+
18
+ ![](https://raw.githubusercontent.com/mikeckennedy/jinja_partials/main/readme_resources/reused-html-visual.png)
19
+
20
+ Check out the [**demo / example application**](https://github.com/mikeckennedy/jinja_partials/tree/main/example)
21
+ to see it in action.
22
+
23
+ ## Installation
24
+
25
+ It's just `pip install jinja-partials` and you're all set with this pure Python package.
26
+
27
+ ## Usage
28
+
29
+ Using the library is incredible easy. The first step is to register the partial method with Jinja and Flask.
30
+ Do this once at app startup:
31
+
32
+ ```python
33
+ import flask
34
+ import jinja_partials
35
+
36
+ app = flask.Flask(__name__)
37
+
38
+ jinja_partials.register_extensions(app)
39
+ # ...
40
+ ```
41
+
42
+ You can also use this library in your FastAPI (or Starlette) project!
43
+ ```python
44
+ from fastapi.templating import Jinja2Templates
45
+ # or `from starlette.templating import Jinja2Templates`
46
+
47
+ import jinja_partials
48
+
49
+ templates = Jinja2Templates("tests/test_templates")
50
+
51
+ jinja_partials.register_starlette_extensions(templates)
52
+ # ...
53
+ ```
54
+
55
+ Next, you define your main HTML (Jinja2) templates as usual. Then
56
+ define your partial templates. I recommend locating and naming them accordingly:
57
+
58
+ ```
59
+ ├── templates
60
+ │   ├── home
61
+ │   │   ├── index.html
62
+ │   │   └── listing.html
63
+ │   └── shared
64
+ │   ├── _layout.html
65
+ │   └── partials
66
+ │   ├── video_image.html
67
+ │   └── video_square.html
68
+ ```
69
+
70
+ Notice the `partials` subfolder in the `templates/shared` folder.
71
+
72
+ The templates are just HTML fragments. Here is a stand-alone one for the YouTube thumbnail from
73
+ the example app:
74
+
75
+ ```html
76
+ <img src="https://img.youtube.com/vi/{{ video.id }}/maxresdefault.jpg"
77
+ class="img img-responsive {{ ' '.join(classes) }}"
78
+ alt="{{ video.title }}"
79
+ title="{{ video.title }}">
80
+ ```
81
+
82
+ Notice that an object called `video` and list of classes are passed in as the model.
83
+
84
+ Templates can also be nested. Here is the whole single video fragment with the image as well as other info
85
+ linking out to YouTube:
86
+
87
+ ```html
88
+ <div>
89
+ <a href="https://www.youtube.com/watch?v={{ video.id }}" target="_blank">
90
+ {{ render_partial('shared/partials/video_image.html', video=video) }}
91
+ </a>
92
+ <a href="https://www.youtube.com/watch?v={{ video.id }}" target="_blank"
93
+ class="author">{{ video.author }}</a>
94
+ <div class="views">{{ "{:,}".format(video.views) }} views</div>
95
+ </div>
96
+ ```
97
+
98
+ Now you see the `render_partial()` method. It takes the subpath into the templates folder and
99
+ any model data passed in as keyword arguments.
100
+
101
+ We can finally generate the list of video blocks as follows:
102
+
103
+ ```html
104
+ {% for v in videos %}
105
+
106
+ <div class="col-md-3 video">
107
+ {{ render_partial('shared/partials/video_square.html', video=v) }}
108
+ </div>
109
+
110
+ {% endfor %}
111
+ ```
112
+
113
+ This time, we reframe each item in the list from the outer template (called `v`) as the `video` model
114
+ in the inner HTML section.
115
+
116
+
117
+ ## Why not just use `include` or `macro` from Jinja?
118
+
119
+ The short answer is they are nearly the same, but both fall short in different ways.
120
+ For a more detailed response, see the discussion on [**issue #1**](https://github.com/mikeckennedy/jinja_partials/issues/1)
@@ -0,0 +1,72 @@
1
+ """
2
+ jinja_partials - Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.
3
+ """
4
+
5
+ __version__ = '0.1.1'
6
+ __author__ = 'Michael Kennedy <michael@talkpython.fm>'
7
+ __all__ = [
8
+ 'register_extensions',
9
+ 'register_starlette_extensions',
10
+ 'render_partial',
11
+ 'generate_render_partial',
12
+ 'PartialsException',
13
+ ]
14
+
15
+ from functools import partial
16
+ from typing import TYPE_CHECKING, Any, Callable, Optional
17
+
18
+ from markupsafe import Markup as Markup
19
+
20
+ try:
21
+ import flask
22
+ except ImportError:
23
+ flask = None
24
+
25
+ try:
26
+ import starlette
27
+ except ImportError:
28
+ starlette = None
29
+
30
+ if TYPE_CHECKING:
31
+ from flask import Flask
32
+ from starlette.templating import Jinja2Templates
33
+
34
+
35
+
36
+ class PartialsException(Exception):
37
+ pass
38
+
39
+
40
+ def render_partial(
41
+ template_name: str,
42
+ renderer: Optional[Callable[..., Any]] = None,
43
+ **data: Any,
44
+ ) -> Markup:
45
+ if renderer is None:
46
+ if flask is None:
47
+ raise PartialsException('No renderer specified')
48
+ else:
49
+ renderer = flask.render_template
50
+
51
+ return Markup(renderer(template_name, **data))
52
+
53
+
54
+ def generate_render_partial(renderer: Callable[..., Any]) -> Callable[..., Markup]:
55
+ return partial(render_partial, renderer=renderer)
56
+
57
+
58
+ def register_extensions(app: 'Flask'):
59
+ if flask is None:
60
+ raise PartialsException('Install Flask to use `register_extensions`')
61
+
62
+ app.jinja_env.globals.update(render_partial=generate_render_partial(flask.render_template))
63
+
64
+
65
+ def register_starlette_extensions(templates: 'Jinja2Templates'):
66
+ if starlette is None:
67
+ raise PartialsException('Install Starlette to use `register_starlette_extensions`')
68
+
69
+ def renderer(template_name: str, **data: Any) -> str:
70
+ return templates.get_template(template_name).render(**data)
71
+
72
+ templates.env.globals.update(render_partial=generate_render_partial(renderer))
@@ -0,0 +1,142 @@
1
+ Metadata-Version: 2.1
2
+ Name: jinja-partials
3
+ Version: 0.1.1
4
+ Summary: Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.
5
+ Home-page: https://github.com/mikeckennedy/jinja_partials
6
+ Author: Michael Kennedy
7
+ Author-email: michael@talkpython.fm
8
+ License: MIT
9
+ Platform: UNKNOWN
10
+ Classifier: Development Status :: 2 - Pre-Alpha
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.6
15
+ Classifier: Programming Language :: Python :: 3.7
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+
22
+ # Jinja Partials
23
+
24
+ Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.
25
+ (There is also a [Pyramid/Chameleon version here](https://github.com/mikeckennedy/chameleon_partials).)
26
+
27
+ ## Overview
28
+
29
+ When building real-world web apps with Flask + Jinja2, it's easy to end up with repeated HTML fragments.
30
+ Just like organizing code for reuse, it would be ideal to reuse smaller sections of HTML template code.
31
+ That's what this library is all about.
32
+
33
+ ## Example
34
+
35
+ This project comes with a sample flask application (see the `example` folder). This app displays videos
36
+ that can be played on YouTube. The image, subtitle of author and view count are reused throughout the
37
+ app. Here's a visual:
38
+
39
+ ![](https://raw.githubusercontent.com/mikeckennedy/jinja_partials/main/readme_resources/reused-html-visual.png)
40
+
41
+ Check out the [**demo / example application**](https://github.com/mikeckennedy/jinja_partials/tree/main/example)
42
+ to see it in action.
43
+
44
+ ## Installation
45
+
46
+ It's just `pip install jinja-partials` and you're all set with this pure Python package.
47
+
48
+ ## Usage
49
+
50
+ Using the library is incredible easy. The first step is to register the partial method with Jinja and Flask.
51
+ Do this once at app startup:
52
+
53
+ ```python
54
+ import flask
55
+ import jinja_partials
56
+
57
+ app = flask.Flask(__name__)
58
+
59
+ jinja_partials.register_extensions(app)
60
+ # ...
61
+ ```
62
+
63
+ You can also use this library in your FastAPI (or Starlette) project!
64
+ ```python
65
+ from fastapi.templating import Jinja2Templates
66
+ # or `from starlette.templating import Jinja2Templates`
67
+
68
+ import jinja_partials
69
+
70
+ templates = Jinja2Templates("tests/test_templates")
71
+
72
+ jinja_partials.register_starlette_extensions(templates)
73
+ # ...
74
+ ```
75
+
76
+ Next, you define your main HTML (Jinja2) templates as usual. Then
77
+ define your partial templates. I recommend locating and naming them accordingly:
78
+
79
+ ```
80
+ ├── templates
81
+ │   ├── home
82
+ │   │   ├── index.html
83
+ │   │   └── listing.html
84
+ │   └── shared
85
+ │   ├── _layout.html
86
+ │   └── partials
87
+ │   ├── video_image.html
88
+ │   └── video_square.html
89
+ ```
90
+
91
+ Notice the `partials` subfolder in the `templates/shared` folder.
92
+
93
+ The templates are just HTML fragments. Here is a stand-alone one for the YouTube thumbnail from
94
+ the example app:
95
+
96
+ ```html
97
+ <img src="https://img.youtube.com/vi/{{ video.id }}/maxresdefault.jpg"
98
+ class="img img-responsive {{ ' '.join(classes) }}"
99
+ alt="{{ video.title }}"
100
+ title="{{ video.title }}">
101
+ ```
102
+
103
+ Notice that an object called `video` and list of classes are passed in as the model.
104
+
105
+ Templates can also be nested. Here is the whole single video fragment with the image as well as other info
106
+ linking out to YouTube:
107
+
108
+ ```html
109
+ <div>
110
+ <a href="https://www.youtube.com/watch?v={{ video.id }}" target="_blank">
111
+ {{ render_partial('shared/partials/video_image.html', video=video) }}
112
+ </a>
113
+ <a href="https://www.youtube.com/watch?v={{ video.id }}" target="_blank"
114
+ class="author">{{ video.author }}</a>
115
+ <div class="views">{{ "{:,}".format(video.views) }} views</div>
116
+ </div>
117
+ ```
118
+
119
+ Now you see the `render_partial()` method. It takes the subpath into the templates folder and
120
+ any model data passed in as keyword arguments.
121
+
122
+ We can finally generate the list of video blocks as follows:
123
+
124
+ ```html
125
+ {% for v in videos %}
126
+
127
+ <div class="col-md-3 video">
128
+ {{ render_partial('shared/partials/video_square.html', video=v) }}
129
+ </div>
130
+
131
+ {% endfor %}
132
+ ```
133
+
134
+ This time, we reframe each item in the list from the outer template (called `v`) as the `video` model
135
+ in the inner HTML section.
136
+
137
+
138
+ ## Why not just use `include` or `macro` from Jinja?
139
+
140
+ The short answer is they are nearly the same, but both fall short in different ways.
141
+ For a more detailed response, see the discussion on [**issue #1**](https://github.com/mikeckennedy/jinja_partials/issues/1)
142
+
@@ -0,0 +1,9 @@
1
+ LICENSE
2
+ README.md
3
+ setup.py
4
+ jinja_partials/__init__.py
5
+ jinja_partials.egg-info/PKG-INFO
6
+ jinja_partials.egg-info/SOURCES.txt
7
+ jinja_partials.egg-info/dependency_links.txt
8
+ jinja_partials.egg-info/requires.txt
9
+ jinja_partials.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ jinja_partials
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,58 @@
1
+ import io
2
+ import os
3
+ import re
4
+
5
+ from setuptools import find_packages
6
+ from setuptools import setup
7
+
8
+
9
+ def read(filename):
10
+ filename = os.path.join(os.path.dirname(__file__), filename)
11
+ text_type = type(u"")
12
+ with io.open(filename, mode="r", encoding='utf-8') as fd:
13
+ return re.sub(text_type(r':[a-z]+:`~?(.*?)`'), text_type(r'``\1``'), fd.read())
14
+
15
+
16
+ with open('./requirements.txt', 'r', encoding='utf-8') as fin:
17
+ requires_list = [line.strip() for line in fin if line and line.strip()]
18
+
19
+
20
+ def read_version():
21
+ filename = os.path.join(os.path.dirname(__file__), 'jinja_partials', '__init__.py')
22
+ with open(filename, mode="r", encoding='utf-8') as fin2:
23
+ for line in fin2:
24
+ if line and line.strip() and line.startswith('__version__'):
25
+ return line.split('=')[1].strip().strip("'").strip('"')
26
+
27
+ return "0.0.0.0"
28
+
29
+
30
+ setup(
31
+ name="jinja_partials",
32
+ version=read_version(),
33
+ url="https://github.com/mikeckennedy/jinja_partials",
34
+ license='MIT',
35
+
36
+ author="Michael Kennedy",
37
+ author_email="michael@talkpython.fm",
38
+
39
+ description="Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.",
40
+ long_description=read("README.md"),
41
+ long_description_content_type="text/markdown",
42
+
43
+ packages=find_packages(exclude=('tests', 'example', 'readme_resources', 'build', 'dist',)),
44
+
45
+ install_requires=requires_list,
46
+
47
+ classifiers=[
48
+ 'Development Status :: 2 - Pre-Alpha',
49
+ 'License :: OSI Approved :: MIT License',
50
+ 'Programming Language :: Python',
51
+ 'Programming Language :: Python :: 3',
52
+ 'Programming Language :: Python :: 3.6',
53
+ 'Programming Language :: Python :: 3.7',
54
+ 'Programming Language :: Python :: 3.8',
55
+ 'Programming Language :: Python :: 3.9',
56
+ 'Programming Language :: Python :: 3.10',
57
+ ],
58
+ )