jinjyaml
Application specific tag of Jinja2 template in PyYAML.
It may be useful if you only want to render special tag nodes in the document, instead of whole YAML string as a template.
Usage
Example 1
-
Add
Jinja2template constructor for tag"!j2"import yaml import jinjyaml as jy ctor = jy.Constructor() yaml.add_constructor("!j2", ctor, yaml.SafeLoader)
-
create
YAMLfile1.yml, with such contents:array: !j2 | {% for i in range(n) %} - sub{{i}}: {{loop.index}} {% endfor %}
-
load and render the
YAMLfilewith open("1.yml") as fp: data = yaml.load(fp, Loader=yaml.SafeLoader) # or for the short: # data = yaml.safe_load(fp) jy.extract(data, context={"n": 3}, inplace=True) print(data)
We'll get:
{"array": [{"sub0": 1}, {"sub1": 2}, {"sub2": 3}]}
Example 2
We have such YAML files:
-
sub-1.yml:"1.1": one "1.2": two
-
sub-2.yml:"2.1": "2.1.1": three "2.1.2": four
-
main.yml:foo: !j2 | {% filter indent %} {% include "sub-1.yml" %} {% endfilter %} {% filter indent %} {% include "sub-2.yml" %} {% endfilter %}
execute python code:
from pprint import pprint
import jinja2
import jinjyaml as jy
import yaml
env = jinja2.Environment(loader=jinja2.FileSystemLoader("."))
ctor = jy.Constructor()
yaml.add_constructor("!j2", ctor, yaml.SafeLoader)
with open("main.yml") as fp:
doc = yaml.safe_load(fp)
obj = jy.extract(doc, env)
pprint(obj)
We'll get:
{"foo": {"1.1": "one",
"1.2": "two",
"2.1": {"2.1.1": "three", "2.1.2": "four"}}}
NOTE:
Since Jinja2's
includeandindentdo not work very nice with indention languages like Python or YAML, it's not advised to use the feature in a complex case.
Metadata
Release files for jinjyaml 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jinjyaml-0.4.tar.gz | 7.7 kB | Without provenance | Details for jinjyaml-0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jinjyaml-0.4-py3-none-any.whl | Python 3 | none | any | Details for jinjyaml-0.4-py3-none-any.whl |
Total release size: 16.2 kB
Release files / jinjyaml-0.4.tar.gz
| Download URL | jinjyaml-0.4.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2d72eefe36b8005a6a2b11d9a268bbcff18daf67120590db11062c13f40668be
|
|
BLAKE2b-256 checksum How to use checksums |
2f279a486de3702acb4c349604c14d8baa0c5604ccda32a0b57d92a28978869b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Release files / jinjyaml-0.4-py3-none-any.whl
| Download URL | jinjyaml-0.4-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
816a5fea4ecb913e26dbd879374df4d701a8b55cec9aeb2037d20bbb07c9ed45
|
|
BLAKE2b-256 checksum How to use checksums |
a96d0142d79dbc715bb9f425eaacbf9167927f6597e40dcd0d16b4aef933a864
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Security for jinjyaml 0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.