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
Yummy Django API, it's the exclusive API used for the e-yummy-ke vue web app

Yummy Django API, it's the exclusive API used for the e-yummy-ke vue web app

Am.Chris_KE 1 Feb 14, 2022
Forward and backwards compatibility layer for Django 1.4, 1.7, 1.8, 1.9, 1.10, and 1.11

django-compat Forward and backwards compatibility layer for Django 1.4 , 1.7 , 1.8, 1.9, 1.10 and 1.11 Consider django-compat as an experiment based o

arteria GmbH 106 Mar 28, 2022
A Django Webapp performing CRUD operations on Library Database.

CRUD operations - Django Library Database A Django Webapp performing CRUD operations on Library Database. Tools & Technologies used: Django MongoDB HT

1 Dec 05, 2021
A pluggable Django application for integrating PayPal Payments Standard or Payments Pro

Django PayPal Django PayPal is a pluggable application that integrates with PayPal Payments Standard and Payments Pro. See https://django-paypal.readt

Luke Plant 672 Dec 22, 2022
🌟 A social media made with Django and Python and Bulma. πŸŽ‰

Vitary A simple social media made with Django Installation πŸ› οΈ Get the source code πŸ’» git clone https://github.com/foxy4096/Vitary.git Go the the dir

Aditya Priyadarshi 15 Aug 30, 2022
REST API with Django and SQLite3

REST API with Django and SQLite3

Luis QuiΓ±ones Requelme 1 Nov 07, 2021
πŸ“ŠπŸ“ˆ Serves up Pandas dataframes via the Django REST Framework for use in client-side (i.e. d3.js) visualizations and offline analysis (e.g. Excel)

Django REST Pandas Django REST Framework + pandas = A Model-driven Visualization API Django REST Pandas (DRP) provides a simple way to generate and se

wq framework 1.2k Jan 01, 2023
Developer-friendly asynchrony for Django

Django Channels Channels augments Django to bring WebSocket, long-poll HTTP, task offloading and other async support to your code, using familiar Djan

Django 5.5k Jan 06, 2023
Add infinite scroll to any django app.

django-infinite-scroll Add infinite scroll to any django app. Features - Allows to add infinite scroll to any page.

Gustavo Teixeira 1 Dec 26, 2021
Send logs to RabbitMQ from Python/Django.

python-logging-rabbitmq Logging handler to ships logs to RabbitMQ. Compatible with Django. Installation Install using pip. pip install python_logging_

Alberto Menendez Romero 38 Nov 17, 2022
Djang Referral System

Djang Referral System About | Features | Technologies | Requirements | Starting | License | Author 🎯 About I created django referral system and I wan

Alex Kotov 5 Oct 25, 2022
Domain-driven e-commerce for Django

Domain-driven e-commerce for Django Oscar is an e-commerce framework for Django designed for building domain-driven sites. It is structured such that

Oscar 5.6k Jan 01, 2023
Plug and play continuous integration with django and jenkins

django-jenkins Plug and play continuous integration with Django and Jenkins Installation From PyPI: $ pip install django-jenkins Or by downloading th

Mikhail Podgurskiy 941 Oct 22, 2022
Login System Django

Login-System-Django Login System Using Django Tech Used Django Python Html Run Locally Clone project git clone https://link-to-project Get project for

Nandini Chhajed 6 Dec 12, 2021
An API was build with Django to store and retrieve information about various musical instruments.

The project is meant to be a starting point, an experimentation or a basic example of a way to develop an API with Django. It is an exercise on using Django and various python technologies and design

Kostas Ziovas 2 Dec 25, 2021
The little ASGI framework that shines. 🌟

✨ The little ASGI framework that shines. ✨ Documentation: https://www.starlette.io/ Community: https://discuss.encode.io/c/starlette Starlette Starlet

Encode 7.7k Dec 31, 2022
A pickled object field for Django

django-picklefield About django-picklefield provides an implementation of a pickled object field. Such fields can contain any picklable objects. The i

Gintautas Miliauskas 167 Oct 18, 2022
Packs a bunch of smaller CSS files together from 1 folder.

Packs a bunch of smaller CSS files together from 1 folder.

1 Dec 09, 2021
Django datatables and widgets, both AJAX and traditional. Display-only ModelForms.

Django datatables and widgets, both AJAX and traditional. Display-only ModelForms. ModelForms / inline formsets with AJAX submit and validation. Works with Django templates.

Dmitriy Sintsov 132 Dec 14, 2022
This is raw connection between redis server and django python app

Django_Redis This repository contains the code for this blogpost. Running the Application Clone the repository git clone https://github.com/xxl4tomxu9

Tom Xu 1 Sep 15, 2022