Skip to main content

Entrypoint and version.

Project description

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

For install django-hmin, run on terminal:

$ pip install django-hmin

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'

Another 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)

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.

Current possible problems

  • Doesnt respect CDATA

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

django-hmin-0.5.1.tar.gz (451.0 kB view details)

Uploaded Source

Built Distribution

django_hmin-0.5.1-py3-none-any.whl (453.4 kB view details)

Uploaded Python 3

File details

Details for the file django-hmin-0.5.1.tar.gz.

File metadata

  • Download URL: django-hmin-0.5.1.tar.gz
  • Upload date:
  • Size: 451.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.24.0

File hashes

Hashes for django-hmin-0.5.1.tar.gz
Algorithm Hash digest
SHA256 1ab8cd952cf4f2364a3e8cdb7e28e5e46804854ecd8355682eac0c0de5ccb2c3
MD5 3ac0ac72b3d2c66fc74cdc2ce6fa3cc3
BLAKE2b-256 36b280ea3c367aba3a638470ee52edaad36fdaf8de3d75a17609c6e751fa7e3e

See more details on using hashes here.

File details

Details for the file django_hmin-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: django_hmin-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 453.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.24.0

File hashes

Hashes for django_hmin-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a03ee6e296b559e27bd14c293a4bcdd5913a67be5e3178b4307b66fe49dff24e
MD5 429fdf3381e45e0e9a75a6db5de21eb3
BLAKE2b-256 0d3a1b7cb98a8eb401101412e1c3002946ede0aabb64484eb4c8a569b0e437cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page