Package: django-hmin
django-hminDjango (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 withhttps://github.com/django-compressor/django-compressor.Written in modern python 3.7+ with fully typing-covered codebase.Full support of:https://www.python.org/dev/peps/pep-0526/https://www.python.org/dev/peps/pep-0484/https://www.python.org/dev/peps/pep-0008/https://www.python.org/dev/peps/pep-0257/https://www.python.org/dev/peps/pep-0518/https://www.python.org/dev/peps/pep-0585/CompatibilityPython 3.7+Django 2.0+ (not required)InstallRegular wayFor install django-hmin, run on terminal:$pipinstalldjango-hminAdvancedIf there is no release, or you dont want/cant use pypi, then:git clonethis repopipenv installto install requirements or justpip install flitflit installYes, we dont use classicsetup.py, we use flit +pyproject.toml. Go bless PEP-518 + PEP-621.Using with Django as midlewareAll you need to do is add two middlewares to yourMIDDLEWARE_CLASSES:MIDDLEWARE_CLASSES:tuple=(# other middleware classes'hmin.middleware.MinMiddleware','hmin.middleware.MarkMiddleware',)If you're using Django's caching middleware,MarkMiddlewareshould go afterFetchFromCacheMiddleware, andMinMiddlewareshould go afterUpdateCacheMiddleware:MIDDLEWARE_CLASSES:tuple=('django.middleware.cache.UpdateCacheMiddleware','hmin.middleware.MinMiddleware',# other middleware classes'django.middleware.cache.FetchFromCacheMiddleware','hmin.middleware.MarkMiddleware',)You can optionally specify theHTML_MINIFYsetting:HTML_MINIFY:bool=TrueThe default value for theHTML_MINIFYsetting isnot DEBUG. You only
need to set it toTrueif you want to minify your HTML code whenDEBUGis enabled.URL excludeSpecify setting:HMIN_EXCLUDE:tuple=('^base/','^admin/')Keep HTML commentsSpecify settings:HMIN_REMOVE_COMMENTS:bool=FalseCacheBy 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=FalseAlso you can change time and cache backend (if you want, by default time is 3600, cache backend — "default"):HMIN_CACHE_TIMEOUT:int=86400HMIN_CACHE_BACKEND:str='my_cache'More using scenariosDecoratorsJust import decorator minify_plain:from hmin.decorators import minify_plain, than you can minify any function you want:@minify_plain()defmy_cool_func():<...>returnsome_plain_htmlOr, if you want to keep html comments:@minify_plain(False)defmy_cool_func():<...>returnsome_plain_htmlSoloJust import function minify. Function definition:def minify(content, remove_comments=True). Example:fromhminimporthtml_minifyhtml_minify('<div>     hello</div>')# <div>hello</div>CLI$python-mhminfilename.html>filename.min.htmlBenchmarking (wannabe)New dataI 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 about2.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 about60ms(min)with htmlmin cpu was about1200ms(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 about220mswithout cache, and87mswith cachewith htmlmin cpu was about125000msDjango overhead was about80ms.Minus overhead, plain time: hmin —140ms(7mswith cache), htmlmin — ok.ChangelogYou can checkhttps://github.com/xfenix/django-hmin/releasesrelease page.Current possible problemsDoesnt respect CDATA