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


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.


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


Or, you know…

from flask import Flask, render_template

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

def index():
    return render_template('index.html')



1 Like