Flask - how to NOT put your .html
files in the templates
directory
What does that mean?
Well, you could just not put them in the templates
directory and then read them, but then you can’t us Jinja2
templating on your .html
files.
So how can we achieve this?
Well, simple.
Here’s the code:
from flask import ..., render_template_string
...
@app.route('/')
def index():
with ('any/path/you/want/your/html/file/to/be/in.html', 'r') as content:
return render_template_string(content, ...)
...
So, what happened? Well, first I read the file and then passed it contents to a render_template_string
function.