Flask extension to minify html, css, js and less.
Project description
flask_minify
A Flask extension to parse request's response and minify html, javascript, css and less.
Install:
- With pip
pip install Flask-Minify
- From the source:
git clone https://github.com/mrf345/flask_minify.git
cd flask_minify
python setup.py install
Setup:
With this setup 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)
- Using a decorator instead:
You can set the extension to be passive so it will minify only the decorated routes.
from flask import Flask
from flask_minify import minify, decorators
app = Flask(__name__)
minify(app=app, passive=True)
@app.route('/')
@decorators.minify(html=True, js=True, cssless=True)
def example():
return '<h1>Example...</h1>'
Options:
def __init__(
self, app=None, html=True, js=True, cssless=True,
fail_safe=True, bypass=[], bypass_caching=[], caching_limit=2,
passive=False, static=True, script_types=[]
):
''' Extension to minify flask response for html, javascript, css and less.
Parameters
----------
app: Flask.app
Flask app instance to be passed.
js: bool
To minify the js output.
cssless: bool
To minify spaces in css.
fail_safe: bool
to avoid raising error while minifying.
bypass: list
list of endpoints to bypass minifying for. (Regex)
bypass_caching: list
list of endpoints to bypass caching for. (Regex)
caching_limit: int
to limit the number of minified response variations.
passive: bool
to disable active minifying.
static: bool
to enable minifying static files css, less and js.
script_types: list
list of script types to limit js minification to.
Notes
-----
if `caching_limit` is set to 0, we'll not cache any endpoint responses,
so if you want to disable caching just do that.
`endpoint` is the name of the function decorated with the
`@app.route()` so in the following example the endpoint will be `root`:
@app.route('/root/<id>')
def root(id):
return id
when using a `Blueprint` the decorated endpoint will be suffixed with
the blueprint name; `Blueprint('blueprint_name')` so here the endpoint
will be `blueprint_name.root`.
`bypass` and `bypass_caching` can handle regex patterns so if you want
to bypass all routes on a certain blueprint you can just pass
the pattern as such:
minify(app, bypass=['blueprint_name.*'])
when using `script_types` include '' (empty string) in the list to
include script blocks which are missing the type attribute.
'''
Development:
- Run Tests:
pytest
- Run style checks:
flake8
Project details
Release history Release notifications | RSS feed
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.27.tar.gz
(6.8 kB
view hashes)
Built Distribution
Close
Hashes for Flask_Minify-0.27-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0daca444c9f1bef52820feb18de2e492191891d01e459a5a72a193e80068c045 |
|
MD5 | 86ed3b38d14f8eb2eaaa9df075755d9c |
|
BLAKE2b-256 | 8bcd64beba7c27ba408fdccb2ff481570d86c8d2e867f67732d9d7241611e4fc |