Today I was facing a problem, I didn’t know how to change Flask root directory. Flask by default look for templates and static files under the root directory (
/), how can we change that?
Here’s my directory structure:
│ ├── static
│ │ └── style.css
│ └── templates
│ └── index.html
3 directories, 5 files
I want Flask to be able to process the folders
templates inside the
api_files folder, my main Flask app is
api.py which is outside the
api_files folder. Let’s open it up and change that behavior so it can process the templates and static files inside that folder.
from flask import Flask, render_template
This way, Flask will no longer look for templates and static files under the project root (
/) path, but inside the folder
There’s a lot of solutions out there about changing jinja2 configuration, I think this is a way more easier and cleaner approach.
You can read more about variables and the Flask API here.