Basic Flask Folder Structure
You'll find this post in your `_posts` directory - edit this post and re-build (or run with the `-w` switch) to see your changes! To add new posts, simply add a file in the `_posts` directory that follows the convention: YYYY-MM-DD-name-of-post.ext.
(Reposted from: https://web.archive.org/web/20161220170219/http://prettyprinted.com/basic-flask-folder-structure/)
This will be a short post on the basic Flask folder structure for when creating a project. This works great for small projects. There are three basic directories you will work with: the main project directory, a template directory, and a static directory.
The main project will host your .py files that are executed by the Python interpreter. The static directory will have unchanging files like .js and .css files. The templates directory will contain any templates that are included in your app.
/example.py /static/script.js /template/layout.html
The content of each file looks like this:
from flask import Flask, render_template app = Flask(__name__) @app.route("/", methods=["GET"]) def index(): return render_template("layout.html") if __name__ == "__main__": app.run(debug=True)
<!doctype html> <html> <head> <title>Flask Folder Structure</title> </head> <body> <h1>Layout Loaded</h1> <script src=""></script> </body> </html>
Flask will look for the layout.html file in the /templates directory. So if we didn’t have it in there, the file wouldn’t load.
In layout.html, we specify that we are looking in the static directory for a file called script.js. Once again, if we moved the script somewhere, Flask wouldn’t be able to find it.
Once we run example.py, this is the result:
The header “Layout Loaded” is shown from the template, and the “Script.js loaded” from the static file pops up in an alert.
So that’s it for basic folder structure. This should be enough to get you started in Flask.
Second Update to this post:
cd .. mkdir
cd .. mkdir