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.

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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for django-hmin-0.2.3.tar.gz
Algorithm Hash digest
SHA256 442a611f51048a62cbcf99d3efcfe35f5130b4da9206ba1dea31ade5879997c5
MD5 e3cc2a81c4b0a0ae2fb3ec1b161c8b7d
BLAKE2b-256 6245a340d64f624671174af437dabeab4995dfa3af1a897623711645798e1b10

See more details on using hashes here.

Release history Release notifications | RSS feed

0.5.4

2 files

0.5.3

2 files

0.5.2

2 files

0.5.1

2 files

0.3.3

3 files

0.3.2

1 file

0.3.1

1 file

0.3

1 file

0.2.4

1 file

This release

0.2.3 This release

1 file

0.2.2

1 file

0.2.1

1 file

0.2

1 file

0.1

1 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