Minify HTML output from Flask application with Jinja2 templates

前端 未结 7 939
鱼传尺愫
鱼传尺愫 2021-02-02 11:24

Is there a Flask or Jinja2 configuration flag / extension to automatically minify the HTML output after rendering the template?

相关标签:
7条回答
  • 2021-02-02 11:59

    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)
    
    0 讨论(0)
提交回复
热议问题