Skip to main content

Compress and minify Flask responses!

Project description

Logo

Downloads Tests Coverage

Flask-Squeeze is a Flask extension that automatically:

  • Minifies repsonses with the mimetypes javascript and css
  • Compresses all responses with brotli if the browser supports it, or gzip if the browser supports it!
  • Protects against the BREACH exploit
  • Caches static files so that they don't have to be re-compressed. The cache will be cleared each time Flask restarts. Files are considered static if the the substring "/static/" is in their request path.

Compatibility

  • Tested with Python 3.7, 3.8, 3.9, 3.10, 3.11 and 3.12

Installation

pip install Flask-Squeeze

Usage

Initialize Flask-Squeeze BEFORE all other extensions and after_request handlers! Flask executes after_request handlers in reverse order of declaration, and the compression should be the last step before sending the response.

from flask_squeeze import Squeeze
squeeze = Squeeze()

def create_app():
    app = Flask(__name__)

    # Init Flask-Squeeze
    squeeze.init_app(app)

    # Init all other extensions
    # AFTER Flask-Squeeze

    return app

Thats it! The responses of your Flask app will now get minified and compressed, if the browser supports it. To control how Flask-Squeeze behaves, the following options exist:

General options

You can configure Flask-Squeeze with the following options in your Flask config:

Option Default Description
SQUEEZE_COMPRESS True Enables or disables compression
SQUEEZE_MIN_SIZE 500 Defines the minimum file size in bytes to activate the compression
SQUEEZE_VERBOSE_LOGGING False Enable or disable verbose logging. If enabled, Flask-Squeeze will print what it does into the terminal in a highlighted color
SQUEEZE_ADD_DEBUG_HEADERS False Add debug infos into the response headers, like call durations and cache hit infos. ONLY USE THIS IN DEVELOPMENT.

Minification options

Option Default Description
SQUEEZE_MINIFY_CSS True Enable or disable css minification using rcssmin
SQUEEZE_MINIFY_JS True Enable or disable js minification using rjsmin

Compression level options

Static files are chached, so they only have to be compressed once. Dynamic files like generated HTML files will not be cached, so they will be compressed for each response.

Option Default Description
SQUEEZE_LEVEL_BROTLI_STATIC default=11, min=0 , max=11 Defines the compression level of brotli for static files
SQUEEZE_LEVEL_BROTLI_DYNAMIC default=1, min=0, max=11 Defines the compression level of brotli for dynamic files
SQUEEZE_LEVEL_DEFLATE_STATIC default=9, min=-1 , max=9 Defines the compression level of deflate for static files
SQUEEZE_LEVEL_DEFLATE_DYNAMIC default=1, min=-1, max=9 Defines the compression level of deflate for dynamic files
SQUEEZE_LEVEL_GZIP_STATIC default=9, min=0 , max=9 Defines the compression level of gzip for static files
SQUEEZE_LEVEL_GZIP_DYNAMIC default=1, min=0, max=9 Defines the compression level of gzip for dynamic files

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

flask_squeeze-3.0.9.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

flask_squeeze-3.0.9-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file flask_squeeze-3.0.9.tar.gz.

File metadata

  • Download URL: flask_squeeze-3.0.9.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/23.2.0

File hashes

Hashes for flask_squeeze-3.0.9.tar.gz
Algorithm Hash digest
SHA256 ce6c451780ee4e89bed2a2a20b86c8c2461c530a95b58c2160f58fcf85fcce17
MD5 1c3fc376a74a2ebf923713b12fc1d2f3
BLAKE2b-256 ab65bdd51dca67bdf5a6af1339e7f16c7732fbc9a31ca0db81302584221e6d82

See more details on using hashes here.

File details

Details for the file flask_squeeze-3.0.9-py3-none-any.whl.

File metadata

  • Download URL: flask_squeeze-3.0.9-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/23.2.0

File hashes

Hashes for flask_squeeze-3.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 1673db843d594d945fbaf65030c3de7fa409be12361dc64d84614d1c55083821
MD5 f0fff93454b3409ee9368622cb070dc9
BLAKE2b-256 a8fc9b9276ac6b6a03410d2e370e2812c2ddf98e25fb2eaa5c3cc6035a71ccaf

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