Flask-Compress
A Flask extension that transparently gzip/Brotli-compresses your application's HTTP responses.
Repository Health
Technical Analysis
Flask-Compress is a small Flask extension that hooks into a Flask application’s response cycle and compresses eligible responses (gzip or Brotli) before they’re sent to the client, based on configurable size thresholds, mimetype allow-lists, and the client’s Accept-Encoding header. It exists to remove the boilerplate of manually wrapping responses in a compression middleware, making it a one-line addition (Compress(app)) to reduce bandwidth and improve load times for JSON APIs and server-rendered HTML alike.
The extension is configuration-driven: compression level, minimum response size, algorithm preference (gzip vs Brotli), and cache-control interplay are all controlled through standard Flask config keys, so it fits into existing app configuration without custom code paths.
What You Get
- Drop-in
Compress(app)initialization following the standard Flask extension pattern, including aflask_compress.compatshim for cross-version compatibility - Support for both gzip and Brotli compression, with Brotli used automatically when the
brotli/brotlicffipackage is installed and the client supports it - Configurable minimum response size (
COMPRESS_MIN_SIZE) and mimetype allow-list (COMPRESS_MIMETYPES) so small or already-compressed responses are skipped - Correct
Vary: Accept-Encodingand cache header handling so compressed responses stay cache-friendly behind CDNs and browser caches - A high-coverage test suite (94% minimum coverage enforced via
tool.coverage.report.fail_under) covering static files, templated responses, and streamed responses
Common Use Cases
- Reducing bandwidth and response latency for a JSON API served by Flask without adding a reverse-proxy compression layer
- Compressing server-rendered HTML/CSS/JS responses from a Flask web app for faster page loads
- Adding Brotli compression support to a Flask deployment for clients that support it, falling back to gzip otherwise
- Standardizing response compression config (thresholds, mimetypes) across multiple Flask services via shared app config
Under The Hood
Architecture - The core logic lives in flask_compress/flask_compress.py (~400 lines), centered on a Compress class that follows the standard Flask extension pattern (init_app), registering an after_request handler that inspects response.mimetype, response.content_length, and request.accept_encodings, then rewrites the response body and headers in place when compression criteria are met. A compat.py module isolates Flask/Werkzeug version differences behind a small compatibility layer.
Tech Stack - Pure Python with Flask as its only hard runtime dependency, and optional brotli/brotlicffi for Brotli support (falling back gracefully to gzip-only if absent). Uses setuptools_scm for version management from git tags and a pyproject.toml-driven build/test/coverage/mypy configuration.
Code Quality - tests/test_flask_compress.py plus fixture directories (tests/static, tests/templates, tests/web) exercise compression against real static files, templated responses, and a running Flask app; pyproject.toml enforces mypy --strict type checking and a 94% coverage floor, indicating disciplined maintenance for a small, focused extension. Commit cadence has slowed (the repo shows infrequent recent commits) though the project remains actively used and periodically updated.
API Design - True to Flask’s extension conventions, the entire public surface is the Compress class with __init__(app)/init_app(app), plus a handful of COMPRESS_* config keys — there’s no additional API to learn, and existing Flask apps can adopt it with a single line and zero code changes elsewhere.
Used by 2 apps in this directory
changedetection.io
Monitoring
Self-hosted website change detection with AI-powered smart alerts, browser automation, price tracking, and 85+ notification channels.
Dify
No Code Platforms · AI Development · Developer Tools
Visual LLM workflow platform with RAG pipelines, agent capabilities, and model management for building production AI applications.