Skip to main content

A lightweight tool and CLI for rendering Jinja2 templates with JSON or Python data, ideal for scripting and automation workflows.

Project description

Escapist: Simplify Jinja2 Template Rendering

Escapist is a lightweight Python tool designed to render Jinja2 templates using JSON or dict data and flexible environment settings. It can be used both as a CLI for quick template rendering tasks and as a Python library for programmatic integration.

For detailed usage and documentation, visit: Escapist Documentation


Features

  • Customizable Jinja2 environment: Configure block delimiters, autoescaping, whitespace trimming, and more through JSON or dict settings.
  • Supports JSON data: Provide template data via JSON files or Python dictionaries.
  • CLI & API: Use via command line or import as a Python library.
  • Batch rendering: Render multiple templates in one command.
  • Cross-platform: Works on Windows, macOS, and Linux.

Installation

To install Escapist, run:

pipx install escapist

Usage

Command-Line Interface (CLI)

Escapist provides simple commands to render Jinja2 templates with JSON data and custom settings.

Render a Single Template

escapist render TEMPLATE_PATH \
  --data DATA_JSON_FILE \
  --settings SETTINGS_JSON_FILE \
  --output OUTPUT_FILE_PATH
  • TEMPLATE_PATH: Path to your Jinja2 template file.
  • --data: (Optional) Path to a JSON file containing data for rendering.
  • --settings: (Optional) Path to a JSON file with Jinja environment settings.
  • --output: (Optional) Path to save the rendered output. If omitted, output is printed to stdout.

Render Multiple Templates in Batch

escapist batch TEMPLATE_DIR \
  --pattern '*.html' \
  --data DATA_JSON_FILE \
  --settings SETTINGS_JSON_FILE \
  --output-dir OUTPUT_DIRECTORY \
  --force
  • TEMPLATE_DIR: Directory containing your templates.
  • --pattern: (Optional) File pattern to match templates, e.g., *.html.
  • --data: (Optional) JSON data file for all templates.
  • --settings: (Optional) Jinja environment settings file.
  • --output-dir: Directory to save rendered templates.
  • --force: (Optional) Overwrite existing output files.

Python Library Usage

You can also use Escapist as a Python library for programmatic rendering.

from escapist.core import JinjaRenderer

# Initialize renderer with optional settings
renderer = JinjaRenderer(settings="path/to/settings.json")

# Load template (file path or template string)
renderer.load_template("path/to/template.jinja")

# Render with data (dict or path to JSON file)
output = renderer.render(data={"name": "Alice", "version": "1.0.0"})

print(output)

Settings Format

Escapist supports flexible Jinja2 environment configuration via JSON settings files. Below is an example structure of a settings JSON file you can use to customize the rendering environment:

{% raw %}

{
  "environment": {
    "block_start_string": "{%",
    "block_end_string": "%}",
    "variable_start_string": "{{",
    "variable_end_string": "}}",
    "comment_start_string": "{#",
    "comment_end_string": "#}",
    "line_statement_prefix": null,
    "line_comment_prefix": null,
    "trim_blocks": false,
    "lstrip_blocks": false,
    "newline_sequence": "\n",
    "keep_trailing_newline": false
  },
  "globals": {
    "app_name": "Escapist",
    "version": "0.0.1"
  },
  "autoescape": {
    "enabled_extensions": ["html", "htm", "xml"],
    "disabled_extensions": [],
    "default_for_string": true,
    "default": false
  },
  "escape_sequences": {
    ">": ">",
    "<": "&lt;",
    "&": "&amp;",
    "\"": "&quot;",
    "'": "&#39;"
  },
  "treat_missing_variable_as_missing": true
}

{% endraw %}

  • environment: Configures Jinja2 delimiters, whitespace trimming, newline behavior, and other environment options.
  • globals: Defines default variables accessible in templates; if a variable is missing from provided data, the value here is used.
  • autoescape: Controls autoescaping of output based on file extensions. Can also be a boolean (true or false). Default is false. Enable to prevent HTML/XSS injection.
  • escape_sequences: Maps characters to their escaped representations (e.g., replacing > with &gt;).
  • treat_missing_variable_as_missing: Determines how missing variables in the template data are handled (e.g., treat as missing instead of erroring).

License

escapist is distributed under the terms of the MIT license.


Contact

Created by Jitesh Sahani (JD) Email: jitesh.sahani@outlook.com

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

escapist-1.0.1.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

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

escapist-1.0.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file escapist-1.0.1.tar.gz.

File metadata

  • Download URL: escapist-1.0.1.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for escapist-1.0.1.tar.gz
Algorithm Hash digest
SHA256 28fe3e1974cf49c1e36233f7c39cc46d4185acf0a3bae84fe6b8d9a53e92461b
MD5 90d822bdcbaf05207a14846e93b645d6
BLAKE2b-256 5b9dc565128c5eb59c63ede5ce1601a09938acaaa248b009ae089831331a2352

See more details on using hashes here.

File details

Details for the file escapist-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: escapist-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for escapist-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f3feffe1a1f87a8b493925edd7ec36b31d48bf9e63c4ffa6dd83af9225463d9
MD5 30cdb335b9e1301ec10ec70b94bafe94
BLAKE2b-256 08b988d37a559af9013a070819eb188638420fd258108d2e972df6a933c8487c

See more details on using hashes here.

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