Skip to main content

jinja2-htmlmin

Minify HTML template source before Jinja compiles it. For Python 3.11+ applications using source-backed Jinja loaders.

Source minification removes selected HTML comments, spacing, and attribute syntax before compilation. Repeated renders of Jinja's cached templates reuse that work.

Installation

pip install jinja2-htmlmin

Quick Start

from jinja2 import DictLoader, Environment
from jinja2_htmlmin import minify_loader

loader = DictLoader({
    "index.html": "<!-- greeting --><p>{{ title }}</p>",
})
env = Environment(
    loader=minify_loader(loader, remove_comments=True),
    autoescape=True,
)
html = env.get_template("index.html").render(title="<Hello>")
assert html == "<p>&lt;Hello&gt;</p>"
print(html)

Next, replace DictLoader with FileSystemLoader pointing at your template directory. Keep Jinja autoescaping configured for your application; minification does not escape or sanitize HTML.

Loader and Cache Behavior

minify_loader mutates the supplied loader's get_source method and returns that same object. Configure it before loading templates. DictLoader, FileSystemLoader, PackageLoader, and FunctionLoader use this source boundary. Jinja's ChoiceLoader and PrefixLoader delegate loading directly to child loaders: wrap each source-backed child, rather than the composite. For example:

from jinja2 import DictLoader, Environment, PrefixLoader
from jinja2_htmlmin import minify_loader

loader = PrefixLoader({
    "pages": minify_loader(
        DictLoader({"index.html": "<!-- removed --><p>{{ name }}</p>"}),
        remove_comments=True,
    ),
})
env = Environment(loader=loader, autoescape=True)
assert env.get_template("pages/index.html").render(name="Ada") == "<p>Ada</p>"

Precompiled ModuleLoader templates bypass get_source and are not minified by this wrapper. Environment.from_string also bypasses loaders.

Cached templates render without repeating minification. Reloading a changed template minifies its new source.

Minification Options and Limits

Jinja's lexer protects expressions, statements, comments, raw blocks, and configured custom delimiters while htmlmin2 minifies the surrounding HTML. Raw-block content is preserved. Values inserted during rendering are not subsequently minified.

Pass htmlmin2 options as keyword arguments to minify_loader. The function reference lists the options; htmlmin2's Minifier reference describes their defaults and behavior:

  • remove_comments, remove_empty_space, and remove_all_empty_space control comment and whitespace removal.
  • reduce_empty_attributes, reduce_boolean_attributes, and remove_optional_attribute_quotes control attribute reduction.
  • convert_charrefs controls character-reference conversion.
  • keep_pre, pre_tags, and pre_attr control whitespace preservation. pre_tags is captured when the wrapper is configured.
  • cls selects an htmlmin.parser.HTMLMinParser subclass; each source load constructs a new instance with the configured options.

Whitespace removal can change inline text spacing, and comment removal can remove application-specific markers. Choose options for your templates and check rendered output.

Support and Reference

See the public function reference and Jinja loader documentation. The package uses the Zero-Clause BSD license.

Issues and questions · Private security reports · Premium support

Metadata

Release files for jinja2-htmlmin 2.0.0

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

Source distribution (sdist)

Source distribution for jinja2-htmlmin 2.0.0
File Size Uploaded Provenance Actions
jinja2_htmlmin-2.0.0.tar.gz 12.5 kB With provenance Details for jinja2_htmlmin-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jinja2-htmlmin 2.0.0
File Interpreter ABI Platform Actions
jinja2_htmlmin-2.0.0-py3-none-any.whl Python 3 none any Details for jinja2_htmlmin-2.0.0-py3-none-any.whl

Total release size: 18.5 kB

Release files / jinja2_htmlmin-2.0.0.tar.gz

Download URL jinja2_htmlmin-2.0.0.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0de12eef7265f7665ff8dc1bee9a9e6060bc9d0effdd270172644c344681de34
BLAKE2b-256 checksum
How to use checksums
1c6f72e7fd962a40bc89f6fd4c0162079db661a3d555bb63e5899cd0189fefa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.

Transparency log

Release files / jinja2_htmlmin-2.0.0-py3-none-any.whl

Download URL jinja2_htmlmin-2.0.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
62476e6bb1f50fd98b36d9015359233742936d3262cd910e0370c08c9f4391fd
BLAKE2b-256 checksum
How to use checksums
f13081998793a304da74bc39483526d324ea5eda8c861a91801398e91bb311fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.

Transparency log

Security for jinja2-htmlmin 2.0.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for jinja2_htmlmin-2.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.

Transparency log

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