Registry / web-framework / flask-compress

flask-compress

JSON →
library1.24pypypi✓ verified 25d ago

Flask-Compress is a Flask extension that allows you to easily compress your Flask application's responses using various algorithms like gzip, deflate, brotli, or zstandard. It helps reduce bandwidth usage and improve page load times. The library is actively maintained, with the current version being 1.24, and it typically sees releases for bug fixes, performance improvements, and feature enhancements.

pip install flask-compress
INSTALL
IMPORT
SIG · FLASK-COMPRESS
F
flask-compress
web-frameworkpythonv1.24
Install
2.7s avg
Import
506ms
Disk
27MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.24 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.528s · 28MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.7s · import 0.484s · 30MB
27MB installed
● package 27MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Compress
from flask_compress import Compress

This quickstart demonstrates how to initialize Flask-Compress with your Flask application. Once initialized, Flask-Compress automatically compresses responses for specified MIME types, such as HTML or JSON, without further configuration. The `/large-data` endpoint serves a large JSON payload which will be compressed.

from flask import Flask, request, jsonify from flask_compress import Compress import os app = Flask(__name__) Compress(app) @app.route('/') def hello_world(): return 'Hello, Compressed World!' @app.route('/large-data') def large_data(): # Simulate a large JSON response data = {'items': [{'id': i, 'name': f'Item {i}', 'description': 'This is a long description for item'} for i in range(1000)]} return jsonify(data) if __name__ == '__main__': app.run(debug=True)
Debug
Known issues
gotchaEnabling streaming compression for static content served directly by Flask might not be ideal. Flask-Compress will compress static content chunk-by-chunk, which can be less efficient than having a web server (like Nginx) handle static file compression.
fix
For static files, consider configuring your front-end web server (e.g., Nginx, Apache) to pre-compress or dynamically compress them, rather than relying on Flask-Compress.
affects: >=1.21
gotchaETag support is disabled by default for streaming responses because it typically requires buffering the entire response in memory to compute the ETag, which can negate the benefits of streaming. If you enable ETag support for streaming endpoints, be aware of the potential memory impact.
fix
To enable ETag for streaming, set `COMPRESS_EVALUATE_CONDITIONAL_REQUEST` to `True` and optionally `COMPRESS_STREAMING_ENDPOINT_CONDITIONAL` for specific endpoints. Evaluate memory usage carefully.
affects: >=1.20
gotchaWhile compression improves network efficiency, it increases CPU usage on the server. For applications with very high traffic or limited CPU resources, this trade-off should be carefully considered and monitored.
fix
Monitor server CPU usage after enabling compression. Adjust `COMPRESS_LEVEL` (for gzip/deflate) or `COMPRESS_BROTLI_QUALITY` to find an optimal balance between compression ratio and CPU load. If CPU becomes a bottleneck, offload compression to a reverse proxy or CDN.
affects: *
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'flask_compress'
The 'flask-compress' package has not been installed in your Python environment or is not accessible to your application.
fix
Install the package using pip: `pip install flask-compress`
AttributeError: 'Compress' object has no attribute 'init_app'
This error typically occurs when attempting to call 'init_app' on an uninstantiated 'Compress' class, or if the Flask application instance is not passed correctly during initialization, especially in an application factory pattern.
fix
Ensure you instantiate the 'Compress' object before calling 'init_app' and pass the Flask application instance: 
```python
from flask import Flask
from flask_compress import Compress

compress = Compress()

def create_app():
    app = Flask(__name__)
    compress.init_app(app)
    return app

# Or, if 'app' is immediately available:
# app = Flask(__name__)
# Compress(app)
```
ImportError: No module named 'brotli'
This error arises when Flask-Compress attempts to use Brotli compression, but the underlying 'brotli' Python package is not installed as a dependency.
fix
Install the 'brotli' package using pip: `pip install brotli`
Upgrade
Version history
1.24latest on PyPI · released Mar 31, 2026
Audit
Dependencies
FlaskrequiredCore web framework for which this is an extension.
brotlioptionalRequired for Brotli compression.
python-zstandardoptionalRequired for Zstandard compression.
Agent activity
6 hits · last 30 days
node
4
Amazon
1
Resources
flask-compress — pip install flask-compress · libregistry