Skip to main content

django-hmin

Build Status

Django (1.6+) HTML minification 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, removes comments, respects (dont remove) conditional comments (IE) by default. Uses cache (can be disabled), that greatly reduce html minification overhead.

Compability

Django 1.6 - Django 1.10

Installing

For install django-hmin, run on terminal:

$ pip install django-hmin

Extra install options

Optionally, you can install re2 (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

Cache

By default hmin middleware uses cache (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 = False

Also you can change time and cache backend (if you want, by default time is 3600, cache backend - “default”):

HMIN_CACHE_TIMEOUT = 86400
HMIN_CACHE_BACKEND = 'my_cache'

Using the function

Just import function minify: from hmin import minify, than you can use it with any html you want. Function definition: 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

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 problems

  • Doesnt respect CDATA

License

Who cares? Use it whatever you want.

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.3.3.tar.gz (4.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

django_hmin-0.3.3-py2-none-any.whl (8.7 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: django-hmin-0.3.3.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-hmin-0.3.3.tar.gz
Algorithm Hash digest
SHA256 9df3fc5f6d38f7dc51b7ee76d1daff2bc6c1a1b7050571b47cb9b52238eda745
MD5 750aab7a8de92becfb707920374eb020
BLAKE2b-256 5f9e8c6c8060e5b62221bd5539878a4eae6e12ff978a6844995bea7be0524b63

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_hmin-0.3.3-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.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 27bf49c55a676ad5c71860878727b50ab59dc7933b694e4f7cd6f5c9d04793c7
MD5 fc47d3d7cd24ab4b3bc22173677c5bd5
BLAKE2b-256 dc11bc7205180db54486240da8de430381a453d6dc774b0da2d348608479a680

See more details on using hashes here.

File details

Details for the file django_hmin-0.3.3-py2-none-any.whl.

File metadata

File hashes

Hashes for django_hmin-0.3.3-py2-none-any.whl
Algorithm Hash digest
SHA256 49be1cb3716caae2acefa6928104638ed7222e3e3280cbf6b820bd9b11eebd20
MD5 655ac4bc482c2e2770f4a5651f43cdbf
BLAKE2b-256 c90ca9f0928afb803182e291a5cd4db166e22cb6b7b9d170f3602dfe02b9d703

See more details on using hashes here.

Supported by

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