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.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 jinja2-htmlmin 2.0.1
File Size Uploaded Provenance Actions
jinja2_htmlmin-2.0.1.tar.gz 12.4 kB With provenance Details for jinja2_htmlmin-2.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 18.3 kB

Release files / jinja2_htmlmin-2.0.1.tar.gz

Download URL jinja2_htmlmin-2.0.1.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ebb30d8763e6e0728cefbcb62efb3561338c8ad1e3a73636517f3320f2871e15
BLAKE2b-256 checksum
How to use checksums
ae7f189c27c1e58e55969b06422f1aa2800672b5420d38d388a3878dd4defc4f
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 Nov 28, 2026.

Transparency log

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

Download URL jinja2_htmlmin-2.0.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
87c8a1e69e68482cfd7f2263e16e1901d8b923ea553184a27288e4106eb41059
BLAKE2b-256 checksum
How to use checksums
48785b3ab1eed101795fd32d1a2bb60de15c583d47a733c651720869c30c07eb
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 Nov 28, 2026.

Transparency log

Security for jinja2-htmlmin 2.0.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.

Full provenance

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

Select from 2 files

Attestation for jinja2_htmlmin-2.0.1.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 Nov 28, 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