Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flask-squeeze

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flask-squeeze

Compress and minify Flask responses!

  • 3.0.9
  • PyPI
  • Socket score

Maintainers
1

Logo

Downloads Tests Coverage

Flask-Squeeze is a Flask extension that automatically:

  • Minifies repsonses with the mimetypes javascript and css
  • Compresses all responses with brotli if the browser supports it, or gzip if the browser supports it!
  • Protects against the BREACH exploit
  • Caches static files so that they don't have to be re-compressed. The cache will be cleared each time Flask restarts. Files are considered static if the the substring "/static/" is in their request path.

Compatibility

  • Tested with Python 3.7, 3.8, 3.9, 3.10, 3.11 and 3.12

Installation

pip install Flask-Squeeze

Usage

Initialize Flask-Squeeze BEFORE all other extensions and after_request handlers! Flask executes after_request handlers in reverse order of declaration, and the compression should be the last step before sending the response.

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:

General options

You can configure Flask-Squeeze with the following options in your Flask config:

OptionDefaultDescription
SQUEEZE_COMPRESSTrueEnables or disables compression
SQUEEZE_MIN_SIZE500Defines the minimum file size in bytes to activate the compression
SQUEEZE_VERBOSE_LOGGINGFalseEnable or disable verbose logging. If enabled, Flask-Squeeze will print what it does into the terminal in a highlighted color
SQUEEZE_ADD_DEBUG_HEADERSFalseAdd debug infos into the response headers, like call durations and cache hit infos. ONLY USE THIS IN DEVELOPMENT.

Minification options

OptionDefaultDescription
SQUEEZE_MINIFY_CSSTrueEnable or disable css minification using rcssmin
SQUEEZE_MINIFY_JSTrueEnable or disable js minification using rjsmin

Compression level options

Static files are chached, so they only have to be compressed once. Dynamic files like generated HTML files will not be cached, so they will be compressed for each response.

OptionDefaultDescription
SQUEEZE_LEVEL_BROTLI_STATICdefault=11, min=0 , max=11Defines the compression level of brotli for static files
SQUEEZE_LEVEL_BROTLI_DYNAMICdefault=1, min=0, max=11Defines the compression level of brotli for dynamic files
SQUEEZE_LEVEL_DEFLATE_STATICdefault=9, min=-1 , max=9Defines the compression level of deflate for static files
SQUEEZE_LEVEL_DEFLATE_DYNAMICdefault=1, min=-1, max=9Defines the compression level of deflate for dynamic files
SQUEEZE_LEVEL_GZIP_STATICdefault=9, min=0 , max=9Defines the compression level of gzip for static files
SQUEEZE_LEVEL_GZIP_DYNAMICdefault=1, min=0, max=9Defines the compression level of gzip for dynamic files

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc