Skip to main content

django-hmin

Django HTML minification middleware (another one). Key feature - speed. Up to 60x time faster, than htmlmin. Compress html, removes comments, respects (dont remove) conditional comments (IE) by default.

Installing

For install django-hmin, run on terminal:

$ pip install django-hmin

Extra install options

If you want extraspeed, you can install re2 (very fast regular expressions) library (pip install re2 + reqs). Hmin will use it instead of re.

Using the midleware

All you need to do is add two middlewares to your MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
    # other middleware classes
    'hmin.middleware.MinMiddleware',
    'hmin.middleware.MarkMiddleware',
)

If you’re using Django’s caching middleware, MarkMiddleware should go after FetchFromCacheMiddleware, and MinMiddleware should go after UpdateCacheMiddleware:

MIDDLEWARE_CLASSES = (
  'django.middleware.cache.UpdateCacheMiddleware',
  'hmin.middleware.MinMiddleware',
  # other middleware classes
  'django.middleware.cache.FetchFromCacheMiddleware',
  'hmin.middleware.MarkMiddleware',
)

You can optionally specify the HTML_MINIFY setting:

HTML_MINIFY = True

The default value for the HTML_MINIFY setting is not DEBUG. You only need to set it to True if you want to minify your HTML code when DEBUG is enabled.

URL exclude

Specify setting:

HMIN_EXCLUDE = ('^base/', '^admin/')

Keep html comments

Specify settings:

HMIN_REMOVE_COMMENTS = False

Using the function

Just import function minify: from hmin import minify, than you can use it with any html you want. Definition of minify function: def minify(content, remove_comments=True)

Using the decorator

Just import decorator minify_plain: from hmin.decorators import minify_plain, than you can minify any function you want:

@minify_plain()
def my_cool_func():
    <...>
    return some_plain_html

Or, if you want to keep html comments:

@minify_plain(False)
def my_cool_func():
    <...>
    return some_plain_html

About speed

I try to compress 1mb of html (i think, your usual html is slightly thiner) on my i7 laptop processor and measure time with django-debug-toolbar.

Django overhead took about 40ms (all), this is time without minification, just plain html, django, etc.

Then i install hmin and htmlmin and just look at the debug toolbar numbers (this is very silly and simple “benchmark”): - with hmin cpu was about 60ms (min) - with htmlmin cpu was about 1200ms (min)

Minus overhead, plain time: hmin - 20ms, htmlmin - 1160ms.

Probably, you can get other numbers. But hmin really faster than htmlmin.

License

Who cares? Use it whatever you want.

Metadata

Release files for django-hmin 0.2.2

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

Source distribution (sdist)

Source distribution for django-hmin 0.2.2
File Size Uploaded Provenance Actions
django-hmin-0.2.2.tar.gz 3.4 kB Without provenance Details for django-hmin-0.2.2.tar.gz

Release files / django-hmin-0.2.2.tar.gz

Download URL django-hmin-0.2.2.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8c56489837ac0a0affad5e9cead25b506c6d3bf13381cab2a935b4e603914569
BLAKE2b-256 checksum
How to use checksums
a9919919ff69a8b665ef285dcf80a0efd3f993facbad390bcba144415f25f302
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-hmin 0.2.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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