Jinja2 environment supporting Python comprehensions
Project description
jinja-comprehensions
Jinja2 environments providing support for list/dict comprehensions, set literals/comprehensions, generator expressions, and list/dict spreading.
{{ {'set', 'set', 'literal', 'literal'} }}
{'literal', 'set'}
list: {{ [n // 2 for n in range(10)] }}
set: {{ {n // 2 for n in range(10)} }}
dict: {{ {n: n // 2 for n in range(10)} }}
list: [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
set: {0, 1, 2, 3, 4}
dict: {0: 0, 1: 0, 2: 1, 3: 1, 4: 2, 5: 2, 6: 3, 7: 3, 8: 4, 9: 4}
{{ (n // 2 for n in range(10)) | join(', ') }}
0, 0, 1, 1, 2, 2, 3, 3, 4, 4
{% set stuff = {'b': 98, 'c': 99, 'd': 100} -%}
{{ {'a': 97, **stuff, 'e': 101} }}
{{ [97, *stuff.values(), 101] }}
{'a': 97, 'b': 98, 'c': 99, 'd': 100, 'e': 101}
[97, 98, 99, 100, 101]
Environments are provided for both sync and async contexts, as well as native variants, too.
Quickstart
pip install jinja-comprehensions
And use a ComprehensionEnvironment
(or NativeComprehensionEnvironment
) instead of the vanilla jinja2.Environment
to compile your templates:
# For rendering to strings
from jinja_comprehensions import ComprehensionEnvironment
jinja_env = ComprehensionEnvironment()
# For rendering to native types
from jinja_comprehensions import NativeComprehensionEnvironment
jinja_env = NativeComprehensionEnvironment()
# For rendering to native types with async enabled
# (This env avoids some pitfalls with vanilla Jinja2 native envs and Awaitable return values)
from jinja_comprehensions import NoLiteralEvalComprehensionNativeEnvironment
jinja_env = NoLiteralEvalComprehensionNativeEnvironment()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
jinja_comprehensions-0.1.0.tar.gz
(12.5 kB
view hashes)
Built Distribution
Close
Hashes for jinja_comprehensions-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5290f1b4bd491a64a570ab912322baa1402a328ea77603636e7636ac238beb9c |
|
MD5 | 84368f9e385779b298f5f882e0761ee7 |
|
BLAKE2b-256 | 5f2cd938233b942e7435fb6b98f35df9bcb7106d92fefcf8924cb0f319e4c55b |
Close
Hashes for jinja_comprehensions-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a9c7822e2f17fae39435ff54d67d44b471cdc699668c2e7d5d0085f6ad00df8 |
|
MD5 | 21a3f9e1c425694f37568bff0bef19cd |
|
BLAKE2b-256 | ecb61aff4fbe23aa6e63e3d2c402cd7bf6f3ad2f28b521003a6fd9676adbf398 |