Long Haul

Basic Flask Folder Structure

December 13, 2014 | 2 Minute Read

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:

/example.py:

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)

/templates/layout.html

<!doctype html>
<html>
  <head>
    <title>Flask Folder Structure</title>
  </head>
  <body>
    <h1>Layout Loaded</h1>
    <script src=""></script>
  </body>
</html>

/static/script.js

alert("Script.js loaded")

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:

https://www.youtube.com/watch?v=_5OXmXvkU_E&t=50s https://prettyprinted.com/blog/577442/flask-basic-app-organization

Also mention:

  • Blueprint
cd .. 
mkdir

or

cd .. 
mkdir

Check out the Jekyll docs for more info on how to get the most out of Jekyll. File all bugs/feature requests at Jekyll’s GitHub repo.