Skip to main content

YAML Jinja2 expressions

Renders Jinja2 expressions inside YAML file.

Install

pip install yaml-expressions

Examples

Load template from file

cfg/cfg.yml:

cfg:
  test: "{{test}}"
from yex import Yex

yex = Yex('./cfg/')

print(yex.render('cfg.yml', test='passed'))

Output: {'cfg': {'test': 'passed'}}

Load template from string

from yex import Yex

result = Yex('test: {{test}}')(test='passed')
# or
result = {'test':'passed'} > Yex('test: {{test}}')
# or
result = Yex('test: {{test}}').render(test='passed')

print(result)

Output: {'test': 'passed'}

Another examples

Describe SEO checklist, generate report

tests_tpl = """
tests:
    title: {{ 20 < (title | length) < 70 }}
    description: {{ 20 < (description | length) < 160 }}"""

report_tpl = """
report: >
    Test results:
      Title: {{ 'ok' if tests.title else 'fail' }}
        ({{ title[:10] }}...)
      Description: {{ 'ok' if tests.description else 'fail' }}
        ({{ description[:10] }}...)
"""

data = {
    'title': 't' * 130,
    'description': 'd' * 120,
}

results = Yex.render_text(tests_tpl, data)
report = Yex.render_text(report_tpl, **results, **data)

print(report.get('report'))

Output:

Test results:
  Title: fail
    (tttttttttt...)
  Description: ok
    (dddddddddd...)

Render HTML pages from prepared config

template = """
meta:
{% for k, v in meta.items() %}
  {{ k }}: >-
    {{ v }}
{% endfor %}
data:
{% for k, v in data.items() %}
  {{ k }}: >-
    {{ v }}
{% endfor %}
"""
data = [
    {
        'meta': {
            'title': 'Hello, world 1!',
        },
        'data': {
            'content': """<h1>Wellcome to our site!</h1>
        <p>Glad to see you!</p>
        """
        }
    },
    {
        'meta': {
            'title': 'Hello, world 2!',
        },
        'data': {
            'content': """<h1>Wellcome to our site!</h1>
        <p>Glad to see you!</p>
        """
        }
    },
]
content_page_template = """<title>{{meta.title}}</title>
<div>{{data.content}}</div>"""
content_page = Yex(content_page_template)
for cfg in Yex(template).generate(data):
    print(content_page.render_content(cfg))

Output:

<title>Hello, world 1!</title>
    <div><h1>Wellcome to our site!</h1>
    <p>Glad to see you!</p>
    </div>
<title>Hello, world 2!</title>
    <div><h1>Wellcome to our site!</h1>
    <p>Glad to see you!</p>
    </div>

Metadata

Release files for yaml-expressions 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yaml-expressions 0.2.1
File Size Uploaded Provenance
yaml-expressions-0.2.1.tar.gz 3.1 kB Without provenance Details

Release files / yaml-expressions-0.2.1.tar.gz

Download URL yaml-expressions-0.2.1.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c4be33c86bdfc10c2ba2fe4539aa937e3cbcac9bc0045aca01a1e03d113a0833
BLAKE2b-256 checksum
How to use checksums
edaa0df1a1bb10d54fffc582d0573a42bb14f4f18122f4e678dc066a096dfa59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

Security for yaml-expressions 0.2.1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page