Compress responses in your Flask app with gzip, deflate, brotli or zstandard.
Maintained. Last release 5 weeks ago, commits in 6 of the last 26 weeks. Weaker on contributor spread and security practices.
pip install flask-compressFlask-Compress
Flask-Compress allows you to easily compress your Flask application's responses with gzip, deflate, brotli or zstd. It originally started as a fork of Flask-gzip. Supported Python versions are 3.9 and newer.
The preferred solution is to have a server (like Nginx) automatically compress the static files for you. If you don't have that option Flask-Compress will solve the problem for you.
How it works
Flask-Compress both adds the various headers required for a compressed response and compresses the response data. This makes serving compressed static files extremely easy.
Internally, every time a request is made the extension will check if it matches one of the compressible MIME types and whether the client and the server use some common compression algorithm, and will automatically attach the appropriate headers.
To determine the compression algorithm, the Accept-Encoding request header is inspected, respecting the
quality factor as described in MDN docs.
If no requested compression algorithm is supported by the server, we don't compress the response. If, on the other
hand, multiple suitable algorithms are found and are requested with the same quality factor, we choose the first one
defined in the COMPRESS_ALGORITHM option (see below).
Some CPython distributions are built without optional compression modules such as
gzip,zlib, orzstd. Flask-Compress detects the algorithms available in the current Python environment, logs a warning for any missing optional compression module, and defaultsCOMPRESS_ALGORITHMandCOMPRESS_ALGORITHM_STREAMINGto the algorithms available in that environment. If either option is explicitly configured with an unavailable algorithm, initialization raises a .