Skip to main content

Flask extension to minify html, css, js and less.

Project description

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:

  • Tests: make test
  • Style check: make lint
  • Format code: make 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-Minify-0.47.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_Minify-0.47-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Minify-0.47.tar.gz.

File metadata

  • Download URL: Flask-Minify-0.47.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.19

File hashes

Hashes for Flask-Minify-0.47.tar.gz
Algorithm Hash digest
SHA256 e0e36190295ee6a4fa90d7c832dc68557b46e198e547ade0e5cc93d0c91597b1
MD5 db19e71c35db6dec938937a68b4c3703
BLAKE2b-256 7229c76ffbebf5a532543cb976b8efb61189407086cdf8976e92b3c785f0e66d

See more details on using hashes here.

File details

Details for the file Flask_Minify-0.47-py3-none-any.whl.

File metadata

  • Download URL: Flask_Minify-0.47-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.19

File hashes

Hashes for Flask_Minify-0.47-py3-none-any.whl
Algorithm Hash digest
SHA256 b1f04ed113e9bb30d37c86ba978456898c42539e57a91bbbe2b472be31b9e2b2
MD5 22fddeb2f952c68f7aac7e897220808d
BLAKE2b-256 0e7ef081cfe233ff23632ec35c2d4483462c7f662a31b5617dfef39d946fb79a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page