A Powerful HTML white space remover for Django

Overview

HTML Whitespace remover for Django

Pepy.tech Badge PyPi Version Badge PyPI - Python Version PyPI - License Code Style

Introduction :

A powerful tool to optimize Django rendered templates

Why use "django_stip_whitespace" ?

  • Adds line break to InlineJS.
  • It can automagically minify inline CSS, JS.
  • Removes <!--prettier-ignore--> from HTML.
  • It speeds up website by reducing the HTML size.
  • Drop in replacement for django.gzip.GzipMiddleware.
  • It compiles regex at runtime. So it's blazing fast.
  • Its mostly based on C ( gzip ) and Rust ( minify-html ) libraries.
  • Significantly lower bytes transferred when working with frameworks like AlpineJs ( Almost fully working & Please open a issue in the Issue Tracker if you encounter any bug) & Petite Vue.
  • Is very customizable. ( You can configure lower level minify-html rust bindings and also the lower level python bindings from settings.py )

Why souldn't you use "django_stip_whitespace" ?

  • You don't like having unnecessary ';;' in your HTML. ( If you know any regex to fix this please put a pull request )
  • Although I tried my best to use Compiled Language for Optimizations. It can still be sub miliseconds ( > 0.001 ) slower compared to normal Django Rendering. ( If you know any way to improve performance, please put a pull request )

Requirements :

  • python-strip-whitespace
  • Django > 3 ( Should work with version 2? )
  • Python 3 ( Should work with all version? )
  • Brotli ( or BrotliPy ) | ( Optional )
  • ZSTD ( Optional )

User guide :

Installation :

Install with pip from pypi (No extra dependencies):

$ python -m pip install django_strip_whitespace

Install with pip with Brotli support:

$ python -m pip install django_strip_whitespace[brotli]

Same but with Zstandard support:

$ python -m pip install django_strip_whitespace[zstd]

Install with pip from github ( Development | Not Recommended for Production ):

$ python -m pip install https://codeload.github.com/baseplate-admin/django_strip_whitespace/zip/refs/heads/main

Then include it in your django project:

MIDDLEWARE = [
   ...
   "strip_whitespace.middlewares.HtmlStripWhiteSpaceMiddleware.html_strip_whitespace",
]

Or if you like:

MIDDLEWARE += "strip_whitespace.middlewares.HtmlStripWhiteSpaceMiddleware.html_strip_whitespace"

Customization :

Change Lower Level Bindings :

Rust :

The module allows rust minifier options to be changed from Django's settings.py file. If you would like to change any settings, refer to minify-html's source code.

The bindings are ( by default set to True ):

STRIP_WHITESPACE_RUST_DO_NOT_MINIFY_DOCTYPE, # passes do_not_minify_doctype to minify-html
STRIP_WHITESPACE_RUST_ENSURE_SPEC_CONPLIANT_UNQUOTED_ATTRIBUTE_VALUES, # passes ensure_spec_compliant_unquoted_attribute_values to minify-html
STRIP_WHITESPACE_RUST_KEEP_CLOSING_TAGS, # passes keep_closing_tags to minify-html
STRIP_WHITESPACE_RUST_KEEP_COMMENTS, # passes keep_comments to minify-html
STRIP_WHITESPACE_RUST_KEEP_HTML_AND_HEAD_OPENING_TAGS, # passes keep_html_and_head_opening_tags to minify-html
STRIP_WHITESPACE_RUST_KEEP_SPACES_BETWEEN_ATTRIBUTES, # passes keep_spaces_between_attributes to minify-html
STRIP_WHITESPACE_RUST_MINIFY_CSS, # passes minify_css to minify-html
STRIP_WHITESPACE_RUST_MINIFY_JS, # passes minify_js to minify-html
STRIP_WHITESPACE_RUST_REMOVE_BANGS, # passes remove_bangs to minify-html
STRIP_WHITESPACE_RUST_REMOVE_PROCESSING_INSTRUCTIONS, # passes remove_processing_instructions to minify-html

If you would like to change any of the above variables, simply put them in settings.py ( Please note that every variable here is a python boolean ).

For example:

# settings.py

STRIP_WHITESPACE_RUST_DO_NOT_MINIFY_DOCTYPE = False

Python :

The module allows python minifier options to be changed from Django's settings.py file. If you would like to change any settings, refer to python-module's source code.

The bindings are ( by default set to a sane value ):

STRIP_WHITESPACE_PYTHON_REMOVE_COMMENTS, # False | removes comments from HTML using python ( not recommended cause rust can do that just fine and fast )
STRIP_WHITESPACE_PYTHON_CONDENSE_STYLE_FROM_HTML, # True | replaces '<style text/css>' -> '<style>'
STRIP_WHITESPACE_PYTHON_CONDENSE_SCRIPT_FROM_HTML, # True | replaces '<script text/javascript>' -> '<script>'
STRIP_WHITESPACE_PYTHON_CLEAN_UNNEEDED_HTML_TAGS, # True | removes some unnecessary tags
STRIP_WHITESPACE_PYTHON_CONDENSE_HTML_WHITESPACE, # True | This is where the magic happens.
STRIP_WHITESPACE_PYTHON_UNQUOTE_HTML_ATTRIBUTES, # True | This is also a magic module.

If you would like to change any of the above variables, simply put them in settings.py ( Please note that every variable here is a python boolean )

For example:

# settings.py

STRIP_WHITESPACE_PYTHON_REMOVE_COMMENTS = True

Change Ignored Paths :

This module allows dynamic ignored path allocation. So for example if your sitemap.xml is at url '/sitemap.xml' and you want to avoid minifying it ( Because this module in lower level is meant to minify HTML not XML ). Then you can add it to ignored path. ( By default it ignores '/sitemap.xml' )

To customize ignored path:

# settings.py

STRIP_WHITESPACE_MINIFY_IGNORED_PATHS.append("/robots.txt") # Note that STRIP_WHITESPACE_MINIFY_IGNORED_PATHS is a Python List

Change NBSP Mangle Character :

This module first replaces the &nbsp; character from html with a character. For example &nbsp; becomes 'āĻ…' ( I picked 'āĻ…' because its a foreign character and not many sites use the character like this 'āĻ…' ). If for some reason this character is causing problem in your HTML. You can change this from settings.py .

To change &nbsp; mangle character:

# settings.py

# Keep the string as  short as possible.
# If you make it long,
# the python str.replace() method will use more CPU and RAM thus slowing your site down.

STRIP_WHITESPACE_NBSP_MANGLE_CHARACTER = 'ga' # Note that STRIP_WHITESPACE_NBSP_MANGLE_CHARACTER is a python string

Change Compression Settings :

This module can do the work of django.gzip middleware. ( It can also do brotli, zstd 👀 )

To change the compression algorithm ( by default using 'gzip' because it's a python stdlib):

# settings.py
STRIP_WHITESPACE_COMPRESSION_ALGORITHM = "gzip" or "br" or "zstd" or "plain"

To use this module with django.gzip middleware ( or django_brotli middleware ):

# settings.py
STRIP_WHITESPACE_COMPRESSION_TYPE = 'compressed'

Contributing :

If you like this project add a star. If you have problems or suggestions please put them in the Issue Tracker. If you like to add features. Fork this repo and submit a Pull Request. 😛

Updates ?? :

This repository is freezed. It will automatically install latest python-strip-whitespace

Special Thanks to :

  • alfonsrv : For making me realize that this module can be used without django gzip middleware
You might also like...
Meta package to combine turbo-django and stimulus-django

Hotwire + Django This repository aims to help you integrate Hotwire with Django 🚀 Inspiration might be taken from @hotwired/hotwire-rails. We are sti

django-reversion is an extension to the Django web framework that provides version control for model instances.

django-reversion django-reversion is an extension to the Django web framework that provides version control for model instances. Requirements Python 3

Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.

Django-environ django-environ allows you to use Twelve-factor methodology to configure your Django application with environment variables. import envi

Rosetta is a Django application that eases the translation process of your Django projects
Rosetta is a Django application that eases the translation process of your Django projects

Rosetta Rosetta is a Django application that facilitates the translation process of your Django projects. Because it doesn't export any models, Rosett

Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.
Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.

Cookiecutter Django Powered by Cookiecutter, Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly. Documentati

Django project starter on steroids: quickly create a Django app AND generate source code for data models + REST/GraphQL APIs (the generated code is auto-linted and has 100% test coverage).

Create Django App 💛 We're a Django project starter on steroids! One-line command to create a Django app with all the dependencies auto-installed AND

django-quill-editor makes Quill.js easy to use on Django Forms and admin sites
django-quill-editor makes Quill.js easy to use on Django Forms and admin sites

django-quill-editor django-quill-editor makes Quill.js easy to use on Django Forms and admin sites No configuration required for static files! The ent

A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, celery and redis.

Django Channels Websocket Chatbot A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, c

A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.
A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.

Django Sage Painless The django-sage-painless is a valuable package based on Django Web Framework & Django Rest Framework for high-level and rapid web

Use minify-html, the extremely fast HTML + JS + CSS minifier, with Django.

django-minify-html Use minify-html, the extremely fast HTML + JS + CSS minifier, with Django. Requirements Python 3.8 to 3.10 supported. Django 2.2 to

Adam Johnson 60 Dec 28, 2022
django-tables2 - An app for creating HTML tables

django-tables2 - An app for creating HTML tables django-tables2 simplifies the task of turning sets of data into HTML tables. It has native support fo

Jan Pieter Waagmeester 1.6k Jan 3, 2023
The best way to have DRY Django forms. The app provides a tag and filter that lets you quickly render forms in a div format while providing an enormous amount of capability to configure and control the rendered HTML.

django-crispy-forms The best way to have Django DRY forms. Build programmatic reusable layouts out of components, having full control of the rendered

null 4.6k Jan 7, 2023
Fully reponsive Chat Application built with django, javascript, materialUi, bootstrap4, html and css.

Chat app (Full Stack Frameworks with Django Project) Fully reponsive Chat Application built with django, javascript, materialUi, bootstrap4, html and

null 1 Jan 19, 2022
This "I P L Team Project" is developed by Prasanta Kumar Mohanty using Python with Django web framework, HTML & CSS.

I-P-L-Team-Project This "I P L Team Project" is developed by Prasanta Kumar Mohanty using Python with Django web framework, HTML & CSS. Screenshots HO

null 1 Dec 15, 2021
An app that allows you to add recipes from the dashboard made using DJango, JQuery, JScript and HTMl.

An app that allows you to add recipes from the dashboard. Then visitors filter based on different categories also each ingredient has a unique page with their related recipes.

Pablo Sagredo 1 Jan 31, 2022
Simple yet powerful and really extendable application for managing a blog within your Django Web site.

Django Blog Zinnia Simple yet powerful and really extendable application for managing a blog within your Django Web site. Zinnia has been made for pub

Julien Fache 2.1k Dec 24, 2022
Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project

Django URL Shortener Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project Install this package to your Dja

Rishav Sinha 4 Nov 18, 2021
Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.

django-widget-tweaks Tweak the form field rendering in templates, not in python-level form definitions. Altering CSS classes and HTML attributes is su

Jazzband 1.8k Jan 2, 2023
Bleach is an allowed-list-based HTML sanitizing library that escapes or strips markup and attributes

Bleach Bleach is an allowed-list-based HTML sanitizing library that escapes or strips markup and attributes. Bleach can also linkify text safely, appl

Mozilla 2.5k Dec 29, 2022