Skip to main content

Compress and minify Flask responses!

Project description

Logo

Downloads Tests Coverage

Flask-Squeeze is a Flask extension that automatically:

  • Minifies responses with JavaScript, CSS, and HTML content
  • Compresses all responses with brotli (preferred), gzip, or deflate compression based on browser support
  • Protects against the BREACH exploit by adding random padding to compressed responses
  • Caches static files so they don't need to be re-compressed, with both in-memory and persistent disk caching options
  • Optimizes performance with intelligent compression levels for static vs. dynamic content
  • Works out-of-the-box - no changes needed to your existing Flask routes or templates

Files are considered static if the substring "/static/" is in their request path.

Table of Contents

Compatibility

  • Works with Python 3.9 to 3.14

Installation

pip install Flask-Squeeze

Quick Start

from flask import Flask
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:

Basic Options

Option Default Description
SQUEEZE_COMPRESS True Enable/disable compression
SQUEEZE_MIN_SIZE 500 Minimum file size (bytes) to compress
SQUEEZE_CACHE_DIR None Directory for persistent cache (None = in-memory only)
SQUEEZE_VERBOSE_LOGGING False Enable debug output

Minification Options

Option Default Description
SQUEEZE_MINIFY_CSS True Enable CSS minification
SQUEEZE_MINIFY_JS True Enable JavaScript minification
SQUEEZE_MINIFY_HTML True Enable HTML minification

Compression Levels

Option Default Range Description
SQUEEZE_LEVEL_BROTLI_STATIC 11 0-11 Brotli level for static files
SQUEEZE_LEVEL_BROTLI_DYNAMIC 1 0-11 Brotli level for dynamic content
SQUEEZE_LEVEL_GZIP_STATIC 9 0-9 Gzip level for static files
SQUEEZE_LEVEL_GZIP_DYNAMIC 1 0-9 Gzip level for dynamic content

Example Configuration

app.config.update({
    'SQUEEZE_CACHE_DIR': './cache/flask_squeeze/',  # Enable persistent caching
    'SQUEEZE_MIN_SIZE': 1000,  # Only compress files > 1KB
    'SQUEEZE_VERBOSE_LOGGING': True,  # Debug mode
})

Contributing

  1. Report bugs by opening an issue
  2. Submit pull requests with improvements
  3. Improve documentation

Development Setup

git clone https://github.com/mkrd/Flask-Squeeze.git
cd Flask-Squeeze
uv sync
just test  # Run tests
just run-test-app  # Run test app

License

MIT License - see LICENSE file for details.

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.5.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_squeeze-3.5.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_squeeze-3.5.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for flask_squeeze-3.5.0.tar.gz
Algorithm Hash digest
SHA256 65cec427d6e24d65b4fa398d0094842e85a4b3b3d30fb325591fceafefcc8bc2
MD5 10e34b60e61741c836fb4cd1e56faa34
BLAKE2b-256 57e0916e7ccfed6828a43751609ec1421292fff0803e94989783c6611e48580f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_squeeze-3.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30fd2efbeddd68a5ed87390451b070371a176527c221ef3ff7762e7e6d3f950c
MD5 12ae77bf9309511f3f79d9ac727bfdca
BLAKE2b-256 91ab629a6e1a8c542f98aebe333b937d5bf01df6ee3624dadf4eb12fe0757c99

See more details on using hashes here.

Supported by

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