Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return raw HTML (using Flask)

How can I make a Flask view publish just the raw HTML (unrendered) that was generated by app?

like image 550
Matt Parrilla Avatar asked Apr 22 '13 15:04

Matt Parrilla


People also ask

How does flask render HTML?

Flask uses the Jinja template library to render templates. In your application, you will use templates to render HTML which will display in the user's browser. In Flask, Jinja is configured to autoescape any data that is rendered in HTML templates.

Can I use HTML with flask?

Flask uses the Jinja template engine to dynamically build HTML pages using familiar Python concepts such as variables, loops, lists, and so on.


1 Answers

Try setting the mime type of the response to text/plain using make_response:

from flask import app, make_response, render_template

app = Flask(__name__)

@app.route('/')
def index():
    resp = make_response(render_template('template.html'))
    resp.mimetype = 'text/plain'
    return resp
like image 90
DazWorrall Avatar answered Sep 19 '22 14:09

DazWorrall