Skip to main content

flask_minify

Latest Release
Supported versions
Coverage Percentage security: bandit Code Style Black

Flask extension to parse and minify html, javascript, css and less.

Install:

With pip

  • pip install Flask-Minify

for better performance (almost two times) you can use the optional go dependency [only Linux supported]

  • pip install Flask-Minify[go]

With setup-tools

  • git clone https://github.com/mrf345/flask_minify.git
  • cd flask_minify
  • python setup.py install

Setup:

In this example the extension will minify every HTML request, unless it's explicitly bypassed.

from flask import Flask
from flask_minify import Minify

app = Flask(__name__)
Minify(app=app, html=True, js=True, cssless=True)

Another approach is using decorators, you can set the extension to be passive so will only minify the decorated routes

from flask import Flask
from flask_minify import Minify, decorators as minify_decorators

app = Flask(__name__)
Minify(app=app, passive=True)

@app.route('/')
@minify_decorators.minify(html=True, js=True, cssless=True)
def example():
  return '<h1>Example...</h1>'

Options:

Option type Description
app object Flask app instance to be passed (default: None)
html bool minify HTML (default: True)
js bool minify JavaScript output (default: True)
cssless bool minify CSS or Less. (default: True)
fail_safe bool avoid raising error while minifying (default: True)
bypass list endpoints to bypass minifying for, supports Regex (default: [])
bypass_caching list endpoints to bypass caching for, supports Regex (default: [])
caching_limit int limit the number of cached response variations (default: 2).
passive bool disable active minifying, to use decorators instead (default: False)
static bool enable minifying static files css, less and js (default: True)
script_types list script types to limit js minification to (default: [])
parsers dict parsers to handle minifying specific tags, mainly for advanced customization (default: {})
go bool prefer go minifier, if optional go dependency is installed (default: True)

- bypass and bypass_caching

endpoint in this context is the name of the function decorated with @app.route so in the following example the endpoint will be root:

@app.route('/root/<id>')
def root(id):
    return id

both options can handle regex patterns as input so for example, if you want to bypass all routes on a certain blueprint you can just pass the pattern as such:

Minify(app, bypass=['blueprint_name.*'])

- caching_limit

if the option is set to 0, we'll not cache any response, so if you want to disable caching just do that.

- script_types

when using the option include '' (empty string) in the list to include script blocks which are missing the type attribute.

- parsers

allows passing tag specific options to the module responsible for the minification, as well as replacing the default parser with another included option or your own custom one.

In the following example will replace the default style (handles CSS) parser rcssmin with lesscpy:

from flask_minify import Minify, parsers as minify_parsers

parsers = {'style': minify_parsers.Lesscpy}

Minify(app=app, parsers=parsers)

you can override the default parser runtime options as well, as shown in the following example:

from flask_minify import Minify, parsers as minify_parsers

class CustomCssParser(minify_parsers.Lesscpy):
    runtime_options = {
        **minify_parsers.Lesscpy.runtime_options,
        "xminify": False,
    }

parsers = {'style': CustomCssParser}

Minify(app=app, parsers=parsers)

the default parsers are set to {"html": Html, "script": Jsmin, "style": Rcssmin} check out the code for more insight.

Development:

Make sure you have nox installed.

  • Tests: nox -s test
  • Style check: nox -s lint
  • Format code: nox -s format

Breaking changes

0.44

Introduced more performant parsers that will be enabled by default, if you're running Linux and the optional Go dependency is installed tdewolff-minify. You can disable that behavior using minify(go=False).

0.40

Due to a future deprecation in Flask 2.3, the extension is no longer going to fallback to Flask._app_ctx_stack, it will raise an exception instead (flask_minify.exceptions.MissingApp)

0.33

introduces a breaking change to the expected output, in this release lesscpy will be replaced by cssmin as the default css minifier so no more less compiling by default. in case you don't want that, follow this example.

Release files for Flask-Minify 0.50

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Minify 0.50
File Size Uploaded
flask_minify-0.50.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Minify 0.50
File Interpreter ABI Platform
Flask_Minify-0.50-py3-none-any.whl Python 3 none any Details

Total release size: 22.7 kB

Release files / flask_minify-0.50.tar.gz

Download URL flask_minify-0.50.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c1fd3e82cb41ed7507d3756008b74d1e56d5c9ddf81983a1a4d70874ffaeaa6d
BLAKE2b-256 checksum
How to use checksums
2de9c4f799758e7e0e2eb0e50e1d56b7d082e3f278fffb8813e70cd792fab8e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / Flask_Minify-0.50-py3-none-any.whl

Download URL Flask_Minify-0.50-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
682d7555ff961fdb0ce43cc2ed2a755df5c1306e74cd3a0ecc80ba9ebfb7e7b4
BLAKE2b-256 checksum
How to use checksums
54dbdec3f8c08c6e4850933b1d3c3c21fa0321fb6660c2155e0abce81de20acb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

0.50 This release

2 release files

0.49

2 release files

0.48

2 release files

0.47

2 release files

0.46

2 release files

0.45

2 release files

0.44

2 release files

0.43

2 release files

0.42

2 release files

0.41

2 release files

0.40

2 release files

0.39

2 release files

0.38

2 release files

0.37

2 release files

0.36

2 release files

0.35

2 release files

0.34

2 release files

0.33

2 release files

0.32

2 release files

0.31

2 release files

0.30

2 release files

0.29

2 release files

0.28

2 release files

0.27

2 release files

0.26

2 release files

0.25

2 release files

0.24

2 release files

0.23

2 release files

0.22

2 release files

0.21

2 release files

0.20

2 release files

0.19

2 release files

0.18

2 release files

0.17

2 release files

0.16

2 release files

0.15

2 release files

0.13

2 release files

0.12

1 release file

0.11

2 release files

0.10

1 release file

0.9

1 release file

0.8

1 release file

0.7

1 release file

0.6

1 release file

0.5.0

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page