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
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 Dec 29, 2022
This is a basic Todo Application API using Django Rest Framework

Todo Application This is a basic Todo Application API using Django Rest Framework. Todo Section - User can View his previously added todo items, creat

Atharva Parkhe 1 Aug 09, 2022
Django app for building dashboards using raw SQL queries

django-sql-dashboard Django app for building dashboards using raw SQL queries Brings a useful subset of Datasette to Django. Currently only works with

Simon Willison 383 Jan 06, 2023
Bootstrap 3 integration with Django.

django-bootstrap3 Bootstrap 3 integration for Django. Goal The goal of this project is to seamlessly blend Django and Bootstrap 3. Want to use Bootstr

Zostera B.V. 2.3k Jan 03, 2023
Full-featured django project start tool.

django-start-tool Introduction django-start-tool is a full-featured replacement for django-admin startproject which provides cli for creating the same

Georgy Gnezdilov 0 Aug 30, 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
Buckshot++ is a new algorithm that finds highly stable clusters efficiently.

Buckshot++: An Outlier-Resistant and Scalable Clustering Algorithm. (Inspired by the Buckshot Algorithm.) Here, we introduce a new algorithm, which we

John Jung 1 Jul 02, 2022
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

lhy 139 Dec 05, 2022
A CTF leaderboard for the submission of flags during a CTF challenge. Built using Django.

🚩 CTF Leaderboard The goal of this project is to provide a simple web page to allow the participants of an CTF to enter their found flags. Also the l

Maurice Bauer 2 Jan 17, 2022
Django-Text-to-HTML-converter - The simple Text to HTML Converter using Django framework

Django-Text-to-HTML-converter This is the simple Text to HTML Converter using Dj

Nikit Singh Kanyal 6 Oct 09, 2022
Django backend of Helium's planner application

Helium Platform Project Prerequisites Python (= 3.6) Pip (= 9.0) MySQL (= 5.7) Redis (= 3.2) Getting Started The Platform is developed using Pytho

Helium Edu 17 Dec 14, 2022
Django Fett is an incomplete code generator used on several projects

Django Fett Django Fett is an incomplete code generator used on several projects. This is an attempt to clean it up and make it public for consumption

Jeff Triplett 6 Dec 31, 2021
Streamlining Django forms to provide all the wins of single-page-applications without the pain.

nango Streamlining Django forms to provide all the wins of single-page-applications without the pain. Key features Available to all Django deployments

Nick Farrell 107 Dec 12, 2022
Duckiter will Automatically dockerize your Django projects.

Duckiter Duckiter will Automatically dockerize your Django projects. Requirements : - python version : python version 3.6 or upper version - OS :

soroush safari 23 Sep 16, 2021
Send push notifications to mobile devices through GCM or APNS in Django.

django-push-notifications A minimal Django app that implements Device models that can send messages through APNS, FCM/GCM and WNS. The app implements

Jazzband 2k Dec 26, 2022
Python port of Google's libphonenumber

phonenumbers Python Library This is a Python port of Google's libphonenumber library It supports Python 2.5-2.7 and Python 3.x (in the same codebase,

David Drysdale 3.1k Jan 08, 2023
Inject an ID into every log message from a Django request. ASGI compatible, integrates with Sentry, and works with Celery

Django GUID Now with ASGI support! Django GUID attaches a unique correlation ID/request ID to all your log outputs for every request. In other words,

snok 300 Dec 29, 2022
this is a simple backend for instagram with python and django

simple_instagram_backend this is a simple backend for instagram with python and django it has simple realations and api in 4 diffrent apps: 1-users: a

2 Oct 20, 2021
A Django web application that allows you to be in the loop about everything happening in your neighborhood.

A Django web application that allows you to be in the loop about everything happening in your neighborhood. From contact information of different handyman to meeting announcements or even alerts.

Kennedy Ngugi Mwaura 3 Dec 11, 2022
Utilities for implementing a modified pre-order traversal tree in django.

django-mptt Utilities for implementing Modified Preorder Tree Traversal with your Django Models and working with trees of Model instances. Project hom

2.7k Jan 01, 2023