Skip to main content

A lightweight html minifier for all Python web frameworks and WSGI apps.

Project description

A lightweight HTML minifier for all Python web frameworks and WSGI apps.

Features

  • Using a web framework, like django, flask, and pyramid? We got you covered.

  • Or you’re feeling adventurous and you’re building your own wsgi app? We got you covered there too. This will work with any program that complies with the WSGI specification

  • Using an encoding that is not UTF-8? Just pass an argument,and we’ll take it from there. 😉

  • Mixing Javascript and html? We’ll try to minify that too, without judging you too much. (No promises though😜).

  • No external dependencies.

Installation

With pip

$ pip install htminify

With poetry

$ poetry add htminify

Usage

For Django

The middleware goes in your wsgi.py file. An example wsgi.py will look like this.

# wsgi.py
import os

from django.core.wsgi import get_wsgi_application
from htminify.wsgi import StripWhitespaceMiddleware # add this!
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')

application = get_wsgi_application()
application = StripWhitespaceMiddleware(application) # add this too!

For Flask

Flask provides access to its wsgi app, which you can pass as an argument to the middleware. You are essentially wrapping the middleware around the wsgi application. An example flask file would be like this.

# app.py
from flask import Flask
from htminify.wsgi import StripWhitespaceMiddleware # add this!

app = Flask(__name__)
app.wsgi_app = StripWhitespaceMiddleware(app.wsgi_app) # add this too!

@app.route('/')
def hello():
    return "Hello, world."

if __name__=="__main__":
    app.run()

Note that we are wrapping the app.wsgi_app object and not the app object.

For any other wsgi framework

A similar procedure can be followed to integrate the middleware with other wsgi-Python web frameworks. Just wrap the middleware around the wsgi app.

# app.py
from htminify.wsgi import StripWhitespaceMiddleware # add this!
wsgi_app = StripWhitespaceMiddleware(wsgi_app) # wrap around

Configuration

if you don’t want to minify when debug is true

You can do something like this

# app.py
if not debug:
    wsgi_app = StripWhitespaceMiddleware(wsgi_app)

If you’re using encoding other than UTF-8

Pass the encoding-type to the middleware when wrapping the app.

# app.py
from htminify.wsgi import StripWhitespaceMiddleware # add this!
wsgi_app = StripWhitespaceMiddleware(wsgi_app, "UTF-16") # pass the encoding

TODO

New Features

  1. Minify Json content.

  2. Add ASGI support.

Documentation

  • Generate Documentation and push to read the docs.

  • Add information for contributing.

Testing

  • Improve test suite for wsgi middleware.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

htminify-0.1.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

htminify-0.1.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file htminify-0.1.2.tar.gz.

File metadata

  • Download URL: htminify-0.1.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.9 Windows/10

File hashes

Hashes for htminify-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6f3bd5ea631010316e2457f896e2854fd8fdcb9332d855bcf91c961871b9ff8c
MD5 92c8aedf173203c58a9abc10e6fdf5c0
BLAKE2b-256 e144f13d2f296afa98deca2a6d53a36ea1b27d0a2bf638914cd4339bd0e587f8

See more details on using hashes here.

File details

Details for the file htminify-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: htminify-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.9 Windows/10

File hashes

Hashes for htminify-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c53c577a4c20045f592a221f7481c8af21fd31cde6c07e9ce99bc2d4e27da59d
MD5 ace688874ad8f78b47d0774fd3de07ef
BLAKE2b-256 cd99de1b110c20730b9e8477dfddfc7a1043ee7840d587e6b5d14354444fe12d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page