Skip to main content

django-hmin

Build and publish PyPI version codecov Code style: black

Django (2.0+) oriented HTML minification function and middleware (another one).
Key feature — speed. 10x (on large html's it can be 50x, 100x, 200x or even more) time faster, than htmlmin.

Compress html code and removes html comments, but ignores conditional comments (IE) by default.
Uses cache by default (can be disabled), so minification overhead is greatly reduced.
Also it can be used as solo function.
For best expirience use it with https://github.com/django-compressor/django-compressor.

Written in modern python 3.7+ with fully typing-covered codebase.

Full support of:

Compatibility

  • Python 3.7+
  • Django 2.0+ (not required)

Install

Regular way

For install django-hmin, run on terminal:

$ pip install django-hmin

Advanced

If there is no release, or you dont want/cant use pypi, then:

  • git clone this repo
  • pipenv install to install requirements or just pip install flit
  • flit install Yes, we dont use classic setup.py, we use flit + pyproject.toml. Go bless PEP-518 + PEP-621.

Using with Django as midleware

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

MIDDLEWARE_CLASSES: tuple = (
    # 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: tuple = (
    '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: bool = 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: tuple = ('^base/', '^admin/')

Keep HTML comments

Specify settings:

HMIN_REMOVE_COMMENTS: bool = False

Cache

By default hmin middleware uses cache via django caches framework (very useful for small and middle web sites, and for big you definitely will use ngx_pagespeed or other "big" solutions). You can disable it by specify setting:

HMIN_USE_CACHE: bool = False

Also you can change time and cache backend (if you want, by default time is 3600, cache backend — "default"):

HMIN_CACHE_TIMEOUT: int = 86400
HMIN_CACHE_BACKEND: str = 'my_cache'

More using scenarios

Decorators

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

Solo

Just import function minify. Function definition: def minify(content, remove_comments=True). Example:

from hmin import html_minify


html_minify('<div>     hello</div>') # <div>hello</div>

CLI

$ python -m hmin filename.html > filename.min.html

Benchmarking (wannabe)

New data

I try to compress 1.1mb of very dense html on my i7 laptop processor (2020 edition!) and measure raw time with chrome inspector.
I got following data (this data comes from field "Waiting (TTFB)", not "Content Download"):

  • htmlmin took about 2.5 seconds!
  • this package took:
    • ~100ms (verion 0.5.2)
    • ~60ms (version 0.5.3) — i fixed some bug in regexp, now package doesnt strip spaces BEFORE open tag, cause it was incorrect behaviour. So, now speed is like previous version. Yes! :)

As you can see, hmin 0.5+ are slightly slower than 0.3, but this is comes from updated minifcation logic. In previous version minification was not so accurate, as i think. But, hmin still very much faster.
Maybe in the next releases i made some experiments with cython or numba to improve speed.

Old data (version < 0.5)

Stupid speed benchmark (1)

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.

Stupid speed benchmark (2)

Also i try to compress 2mb of html on my desktop i3 (sandy bridge).
Debug toolbar time:

  • with hmin cpu was about 220ms without cache, and 87ms with cache
  • with htmlmin cpu was about 125000ms Django overhead was about 80ms.
    Minus overhead, plain time: hmin — 140ms (7ms with cache), htmlmin — ok.

Changelog

You can check https://github.com/xfenix/django-hmin/releases release page.

Current possible problems

  • Doesnt respect CDATA

Metadata

Release files for django-hmin 0.5.4

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.5.4
File Size Uploaded
django-hmin-0.5.4.tar.gz 451.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-hmin 0.5.4
File Interpreter ABI Platform
django_hmin-0.5.4-py3-none-any.whl Python 3 none any Details

Total release size: 905.8 kB

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

Download URL django-hmin-0.5.4.tar.gz
Size 451.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8b79c4ef7fea4b03f7bc6aa0eddab0d3147e432148f30475be11d4a591f73bcd
BLAKE2b-256 checksum
How to use checksums
ca24fa3d5b42d1cc25dfd513ab7d0a6d7481661a70335e89f093dfb59e89a296
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.24.0

Release files / django_hmin-0.5.4-py3-none-any.whl

Download URL django_hmin-0.5.4-py3-none-any.whl
Size 454.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66354707e67f5c89a70267658dbf8962ce3d9fbcc03fa8ea08746070accefe1e
BLAKE2b-256 checksum
How to use checksums
2bc0a73f39571a09e7333e5c2498344aaf9f027571a4758a78249a0fecab92d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.24.0

Release history Release notifications | RSS feed

This release

0.5.4 This release

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.3.3

3 release files

0.3.2

1 release file

0.3.1

1 release file

0.3

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2

1 release file

0.1

1 release file

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