Minify HTML output from Flask application with Jinja2 templates Minify HTML output from Flask application with Jinja2 templates python python

Minify HTML output from Flask application with Jinja2 templates


Found a better way to do this. You can minify all your pages with this method:

from flask import Flaskfrom htmlmin.main import minifyapp = Flask(__name__)@app.after_requestdef response_minify(response):    """    minify html response to decrease site traffic    """    if response.content_type == u'text/html; charset=utf-8':        response.set_data(            minify(response.get_data(as_text=True))        )        return response    return response


Have a look here https://github.com/cobrateam/django-htmlmin#using-the-html_minify-function

I realise it is mainly used for django but the example shows how to use this projects code to do what you want with a flask view, i think.


Use the decorator.

from htmlmin.decorator import htmlmin@htmlmindef home():...

Or you can just use:

re.sub(r'>\s+<', '><', '<tag>   </tag>') # results '<tag></tag>'