Skip to main content

Python command-line tool to generates text files based on Jinja2 template and YAML data file.

Project description

Python

JINJACRAFT

JinjaCraft is a simple Python command-line tool which can generate text files based on a Jinja2 template and a YAML or JSON data file. It can also generate a model data file from a Jinja2 template.

Requirements

Python 3.9 or higher

Installation

Using pip:

pip install jinjacraft

Using pipx (recommended for CLI tools):

pipx install jinjacraft

Usage

usage: jinjacraft [-h] [-g TEMPLATE] [-f] [-o OUTPUT_FILE] [--format {yaml,json}]
                  [data_file] [template_file]

Generate text files from Jinja2 templates and YAML/JSON data

positional arguments:
  data_file             Data file path (YAML or JSON)
  template_file         Jinja2 template file path

options:
  -h, --help            show this help message and exit
  -g TEMPLATE, --generate-model TEMPLATE
                        Generate a model data file from a Jinja2 template
  -f, --force           Overwrite existing output file
  -o OUTPUT_FILE, --output_file OUTPUT_FILE
                        Output file path
  --format {yaml,json}  Data file format (default: yaml)

Features

Data Validation

JinjaCraft validates your data (YAML or JSON) against the template before rendering:

  • Missing variables: If a variable is used in the template but not defined in the data file, an error is returned and rendering is aborted.
  • Unused variables: If a variable is defined in the data file but not used in the template, a warning is displayed but rendering continues.

Model Generation

Generate a model data file from a Jinja2 template to see which variables are expected:

jinjacraft -g template.jinja2

This creates template.yaml with placeholders and type comments:

title: "<title>"  # string
tasks:  # list
  - name: "<name>"  # string
    completed: "<completed>"  # truthy value (boolean, string, number)

Use --format json to generate a JSON model instead:

jinjacraft -g template.jinja2 --format json

This creates template.json:

{
  "title": "<title>",
  "tasks": [
    {
      "name": "<name>",
      "completed": "<completed>"
    }
  ]
}

Use -o to specify a custom output file, and -f to overwrite an existing file:

jinjacraft -g template.jinja2 -o model.yaml -f

Examples

Using YAML data

YAML file

title: Hello World
tasks:
  - name: First task
    completed: True
  - name: Second task
    completed: False

Template file

Document: {{ title }}
Tasks:
{% for task in tasks %}- {{ task.name }} ({% if task.completed %}completed{% else %}not completed{% endif %})
{% endfor %}

Command line

jinjacraft data.yaml template.jinja2 -o outputfile.txt

Output

Document: Hello World
Tasks:
- First task (completed)
- Second task (not completed)

Using JSON data

JSON file

{
  "title": "Hello World",
  "tasks": [
    {"name": "First task", "completed": true},
    {"name": "Second task", "completed": false}
  ]
}

Command line

jinjacraft data.json template.jinja2 --format json -o outputfile.txt

The output is the same as with YAML data.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinjacraft-1.5.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jinjacraft-1.5.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file jinjacraft-1.5.1.tar.gz.

File metadata

  • Download URL: jinjacraft-1.5.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for jinjacraft-1.5.1.tar.gz
Algorithm Hash digest
SHA256 41e986bdd4ed1abc7629f0e9ac533c83ef62567dc94c8a8cc8cbf57593980f4a
MD5 29e707c1149a7e49a940698a9a62afd0
BLAKE2b-256 39a25298dfb9df309b9628ca0d0b5d01340a13cdb52a56cf5d33e3cf1dc14cf6

See more details on using hashes here.

Provenance

The following attestation bundles were made for jinjacraft-1.5.1.tar.gz:

Publisher: release.yml on sdejongh/jinjacraft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jinjacraft-1.5.1-py3-none-any.whl.

File metadata

  • Download URL: jinjacraft-1.5.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for jinjacraft-1.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 efd33cc932b65dac836f7054e3c97a366a36275a34b0af41978cd0feadef0f43
MD5 af9fbb4fd4625196ef56e441c56be367
BLAKE2b-256 3d8202e2dd31aa79c554557d8de76db9be68907a90c26d89c644f223d9fc7057

See more details on using hashes here.

Provenance

The following attestation bundles were made for jinjacraft-1.5.1-py3-none-any.whl:

Publisher: release.yml on sdejongh/jinjacraft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page