Flask-Compress

A Flask extension that transparently gzip/Brotli-compresses your application's HTTP responses.

Library
PyPI
v1.24
129stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
54/100Fair
Development Activity52
Maintenance8
Community76
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
75/100Good
Architecture70
Code Quality78
Innovation60
Learning Curve90

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 a flask_compress.compat shim for cross-version compatibility
  • Support for both gzip and Brotli compression, with Brotli used automatically when the brotli/brotlicffi package 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-Encoding and 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search