jinja_partials 0.1.0__py3-none-any.whl

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,31 @@
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.0'
6
+ __author__ = 'Michael Kennedy <michael@talkpython.fm>'
7
+ __all__ = ['render_partial']
8
+
9
+ import flask as __flask
10
+ from jinja2 import Markup as Markup
11
+
12
+ has_registered_extensions = False
13
+
14
+
15
+ class PartialsException(Exception):
16
+ pass
17
+
18
+
19
+ def render_partial(template_name: str, **data: dict) -> Markup:
20
+ if not has_registered_extensions:
21
+ raise PartialsException("You must call register_extensions() before this function can be used.")
22
+
23
+ html = __flask.render_template(template_name, **data)
24
+ return Markup(html)
25
+
26
+
27
+ def register_extensions(app: __flask.Flask):
28
+ global has_registered_extensions
29
+
30
+ has_registered_extensions = True
31
+ app.jinja_env.globals.update(render_partial=render_partial)
@@ -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,125 @@
1
+ Metadata-Version: 2.1
2
+ Name: jinja-partials
3
+ Version: 0.1.0
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
+ Requires-Dist: jinja2
22
+ Requires-Dist: flask
23
+
24
+ # Jinja Partials
25
+
26
+ Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.
27
+
28
+ ## Overview
29
+
30
+ When building real-world web apps with Flask + Jinja2, it's easy to end up with repeated HTML fragments.
31
+ Just like organizing code for reuse, it would be ideal to reuse smaller sections of HTML template code.
32
+ That's what this library is all about.
33
+
34
+ ## Example
35
+
36
+ This project comes with a sample flask application (see the `example` folder). This app displays videos
37
+ that can be played on YouTube. The image, subtitle of author and view count are reused throughout the
38
+ app. Here's a visual:
39
+
40
+ ![](https://raw.githubusercontent.com/mikeckennedy/jinja_partials/main/readme_resources/reused-html-visual.png)
41
+
42
+ Check out the [**demo / example application**](https://github.com/mikeckennedy/jinja_partials/tree/main/example)
43
+ to see it in action.
44
+
45
+ ## Installation
46
+
47
+ It's just `pip install jinja-partials` and you're all set with this pure Python package.
48
+
49
+ ## Usage
50
+
51
+ Using the library is incredible easy. The first step is to register the partial method with Jinja and Flask.
52
+ Do this once at app startup:
53
+
54
+ ```python
55
+ import flask
56
+ import jinja_partials
57
+
58
+ app = flask.Flask(__name__)
59
+
60
+ jinja_partials.register_extensions(app)
61
+ # ...
62
+ ```
63
+
64
+ Next, you define your main HTML (Jinja2) templates as usual. Then
65
+ define your partial templates. I recommend locating and naming them accordingly:
66
+
67
+ ```
68
+ ├── templates
69
+ │   ├── home
70
+ │   │   ├── index.html
71
+ │   │   └── listing.html
72
+ │   └── shared
73
+ │   ├── _layout.html
74
+ │   └── partials
75
+ │   ├── partial_video_image.html
76
+ │   └── partial_video_square.html
77
+ ```
78
+
79
+ Notice the `partials` subfolder in the `templates` folder.
80
+
81
+ The templates are just HTML fragments. Here is a stand-alone one for the YouTube thumbnail from
82
+ the example app:
83
+
84
+ ```html
85
+ <img src="https://img.youtube.com/vi/{{ video.id }}/maxresdefault.jpg"
86
+ class="img img-responsive {{ ' '.join(classes) }}"
87
+ alt="{{ video.title }}"
88
+ title="{{ video.title }}">
89
+ ```
90
+
91
+ Notice that an object called `video` and list of classes are passed in as the model.
92
+
93
+ Templates can also be nested. Here is the whole single video fragment with the image as well as other info
94
+ linking out to YouTube:
95
+
96
+ ```html
97
+ <div>
98
+ <a href="https://www.youtube.com/watch?v={{ video.id }}" target="_blank">
99
+ {{ render_partial('shared/partials/partial_video_image.html', video=video) }}
100
+ </a>
101
+ <a href="https://www.youtube.com/watch?v={{ video.id }}" target="_blank"
102
+ class="author">{{ video.author }}</a>
103
+ <div class="views">{{ "{:,}".format(video.views) }} views</div>
104
+ </div>
105
+ ```
106
+
107
+ Now you see the `render_partial()` method. It takes the subpath into the templates folder and
108
+ any model data passed in as keyword arguments.
109
+
110
+ We can finally generate the single video blocks as follows:
111
+
112
+ ```html
113
+ {% for v in videos %}
114
+
115
+ <div class="col-md-3 video">
116
+ {{ render_partial('shared/partials/partial_video_square.html', video=v) }}
117
+ </div>
118
+
119
+ {% endfor %}
120
+ ```
121
+
122
+ This time, we reframe each item in the list from the outer template (called `v`) as the `video` model
123
+ in the outer HTML section.
124
+
125
+
@@ -0,0 +1,6 @@
1
+ jinja_partials/__init__.py,sha256=GLl2x6ycvX9Mm0bnPhX8Afi1lvHNvXUuNfYMVRfpmas,855
2
+ jinja_partials-0.1.0.dist-info/LICENSE,sha256=B_XBSvJtJYaSzXE0o9ptgql9kUId_iA0d1_XlrYfcaM,1072
3
+ jinja_partials-0.1.0.dist-info/METADATA,sha256=i6nNj2HdXOYlSeILCQeRokJvivxocwBYZDrGClI2epQ,4085
4
+ jinja_partials-0.1.0.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
5
+ jinja_partials-0.1.0.dist-info/top_level.txt,sha256=8ZW7w6eR_CzS9ZjcYGhRpjTn4Py9WwLeje0KuM5N7Ew,15
6
+ jinja_partials-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.36.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ jinja_partials