Skip to main content

Automatic HTML minification for Jinja2 templates

Project description

jinja2-htmlmin

PyPI - Python Version Liberapay Patrons GitHub Sponsors

Automatic HTML minification for Jinja2 templates.

Why Use This?

  • Zero runtime overhead: Minification happens once at load time, not on every render
  • Smaller HTML: Reduced bandwidth and faster page loads
  • Drop-in compatibility: Works with any Jinja2 loader (Flask, Django, FastAPI, etc.)
  • Broad Python support: Compatible with Python 3.9+
  • Semantic Versioning: Predictable, reliable updates
  • Zero-Clause BSD: Public domain, use freely anywhere

Installation

pip install jinja2-htmlmin

Quick Start

from jinja2 import Environment, FileSystemLoader
from jinja2_htmlmin import minify_loader

# Wrap any Jinja2 loader
# See https://htmlmin.readthedocs.io/en/latest/reference.html for options
env = Environment(
    loader=minify_loader(
        FileSystemLoader("templates"),
        remove_comments=True,
        remove_empty_space=True,
        remove_all_empty_space=True,
        reduce_boolean_attributes=True,
    )
)

# Rendered HTML is automatically minified
html = env.get_template('index.html').render(title='My Page')

How It Works

  1. Template loads normally through your existing loader
  2. Jinja2 syntax is temporarily protected during minification
  3. HTML content is minified using htmlmin2
  4. Jinja2 syntax is restored and template compiles as usual
  5. Jinja2's built-in caching means this happens only once per template

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

jinja2_htmlmin-1.0.1.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

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

jinja2_htmlmin-1.0.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jinja2_htmlmin-1.0.1.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for jinja2_htmlmin-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c53398ff12799a79ab75e471e9c26be9c837d1c10103da308280bd96c45588af
MD5 39a9bedae93f0ef86b14e192c930695d
BLAKE2b-256 1aefd642475f0cc66c89f3347924a23a173751dc160ba44269e76db48a89f2a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for jinja2_htmlmin-1.0.1.tar.gz:

Publisher: workflow.yaml on Zaczero/jinja2-htmlmin

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

File details

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

File metadata

  • Download URL: jinja2_htmlmin-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for jinja2_htmlmin-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b9c6c43e88443a238e3796054c4a54cd5bd755dfc3c65b775910813988a89da3
MD5 0f7ed91587ce67eddb425034b1ec0c9b
BLAKE2b-256 136f89b2029eb311bcab200d875599ab6594e186d865c181d08b686f60d3d26e

See more details on using hashes here.

Provenance

The following attestation bundles were made for jinja2_htmlmin-1.0.1-py3-none-any.whl:

Publisher: workflow.yaml on Zaczero/jinja2-htmlmin

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