Is there a Flask or Jinja2 configuration flag / extension to automatically minify the HTML output after rendering the template?
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.
setup.py
file still requires Django. –
Dachshund Found a better way to do this. You can minify all your pages with this method:
from flask import Flask
from htmlmin.main import minify
app = Flask(__name__)
@app.after_request
def 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.
setup.py
file still requires Django. –
Dachshund I use the following decorators
import bs4
import functools
import htmlmin
def prettify(route_function):
@functools.wraps(route_function)
def wrapped(*args, **kwargs):
yielded_html = route_function(*args, **kwargs)
soup = bs4.BeautifulSoup(yielded_html, 'html.parser')
return soup.prettify()
return wrapped
def uglify(route_function):
@functools.wraps(route_function)
def wrapped(*args, **kwargs):
yielded_html = route_function(*args, **kwargs)
minified_html = htmlmin.minify(yielded_html)
return minified_html
return wrapped
And simply wrapped the default render_template function like so
if app.debug:
flask.render_template = prettify(flask.render_template)
else:
flask.render_template = uglify(flask.render_template)
This has the added benefit of being auto added to the cache, since we don't actually touch app.route
htmlmin
? –
Preoccupied I've written a flask extension to achieve that purpose. You can install it using pip install flask-htmlmin
and the source is available at https://github.com/hamidfzm/Flask-HTMLmin . Hope it will be useful.
Use the decorator.
from htmlmin.decorator import htmlmin
@htmlmin
def home():
...
Or you can just use:
re.sub(r'>\s+<', '><', '<tag> </tag>') # results '<tag></tag>'
a <b>big</b> <i>fat</i> <s>edit</s> here
results in the output a <b>big</b><i>fat</i><s>edit</s> here
-- the spaces have been clobbered. –
Padlock To extend the usefulness of the answer from @olly_uk and the comment by @Alexander, it appears that the django-htmlmin extension is now designed to be used with frameworks other than Django.
From the docs here, you can manually use the html_minify function in Flask views, like so:
from flask import Flask
from htmlmin.minify import html_minify
app = Flask(__name__)
@app.route('/')
def home():
rendered_html = render_template('home.html')
return html_minify(rendered_html)
re
module: return sub(r'\s{2,}|[\r\n]', '', render_template('blah.html'))
... What makes minify special? –
Assume Modifying @Bletch answer for the latest version of htmlmin.
from flask import Flask
import htmlmin
app = Flask(__name__)
@app.route('/')
def home():
rendered_html = render_template('home.html')
return htmlmin.minify(rendered_html)
https://htmlmin.readthedocs.io/en/latest/quickstart.html
The minified html will still have some spaces between the tags. If we want to remove that, then remove_empty_space =True
attribute needs to be added while the template is rendered.
return htmlmin.minify(rendered_html, remove_empty_space =True)
In 2022, I have found better success using the minify-html package: https://github.com/wilsonzlin/minify-html
minify_html.minify(
"""
html here
""",
minify_js=True,
minify_css=True,
remove_processing_instructions=True,
)
© 2022 - 2024 — McMap. All rights reserved.