Flask - how to NOT put your `.html` files in the `templates` directory

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 Jinja2templating 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.

2 Likes

@SnakeyKing I removed the * * from the title of the topic, it was unnecessary as the topic is already bolded.

4 Likes

Or, you know…

from flask import Flask, render_template

app = Flask(__name__, template_folder='any/path/you/want/your/html/file/to/be/in/')


@app.route('/')
def index():
    return render_template('index.html')

...

image

1 Like