Common folder/file structure in Flask app
PythonFlaskPython Problem Overview
I have just created a flask application and so far I have a router for my "Hello world!" template.
I would like to add a little (a lot) more functionality, but I wonder how I should structure the app directory.
What's the most common way of structuring a Flask app?
For instance, should I create a routes.py
for all my routes?
Where does the SQLAlchemy stuff go?
Should models be in models.py
?
Python Solutions
Solution 1 - Python
You should check out the Larger Applications page in the Patterns section of the Flask docs: http://flask.pocoo.org/docs/patterns/packages/. It seems to be the model that most people follow when their application calls for a package instead of a module.
I believe views.py
is what you are calling routes.py
. After that, models would go in models.py
, forms would go in forms.py
, etc.
Solution 2 - Python
An example of a FlaskApp directory:
/yourapp
/run.py
/config.py
/app
/__init__.py
/views.py
/models.py
/static/
/main.css
/templates/
/base.html
/requirements.txt
/yourappenv
run.py
- contains the actual python code that will import the app and start the development server.
config.py
- stores configurations for your app.
__init__.py
- initializes your application creating a Flask app instance.
views.py
- this is where routes
are defined.
models.py
- this is where you define models for your application.
static
- contains static files i.e. CSS, Javascript, images
templates
- this is where you store your html
templates i.e. index.html
, layout.html
requirements.txt
- this is where you store your package dependancies, you can use pip
yourappenv
- your virtual environment for development
Solution 3 - Python
I would say if you split the application use divisional rather than functional structure. I advocate this because you are more likely to work on 1 of these divisional components at any one time.
This type of structure lends itself well on marketplace or SaaS apps where different user groups use a different type of views. API only flask app I might use functional splitting.
Here are examples from Flask Blueprints. Blueprints are essentially documented advice how to split Flask application for more manageable pieces. More on this at : http://exploreflask.com/en/latest/blueprints.html
Here is an example of divisional splitting. See how each feature is grouped together.
yourapp/
__init__.py
admin/
__init__.py
views.py
static/
templates/
home/
__init__.py
views.py
static/
templates/
control_panel/
__init__.py
views.py
static/
templates/
models.py
Here is the functional example >
yourapp/
__init__.py
static/
templates/
home/
control_panel/
admin/
views/
__init__.py
home.py
control_panel.py
admin.py
models.py
Solution 4 - Python
I think flask is micro framework and now you must decide how create files and folders.
i use this way :
- flask folders and files structure -> https://gist.github.com/4545740
this is near Django structure
i suggest you see some project to give you what you want
- danjac / newsmeme — Bitbucket -> https://bitbucket.org/danjac/newsmeme/overview
- sean-/flask-skeleton · GitHub -> https://github.com/sean-/flask-skeleton
Solution 5 - Python
Anyone looking for a simple beginner-friendly structure for the flask project may find this helpful:
|__movies
|__run.py
|__app
├── templates
│ └── index.html
│ └── signup.html
└── __init__.py
└── routes.py
Here 'movies' is the name given for the main application. It contains 'run.py' and a folder called 'app'. 'app' folder contains all necessary flask files such as 'templates' folder, '__init __.py', and 'routes.py'.
Contents of:
run.py:
from app import app
init.py:
from flask import Flask
app = Flask(__name__)
from app import routes
app.run(debug=True)
routes.py:
from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
Solution 6 - Python
Beauty of flask lies in its flexibility. You can build django like project-structure easily. Django popularized abstraction of features in apps and making them reusable but it can be a overkill for many projects.
But with flask you can go either way. Write reusable apps or write simple apps. Check these cookiecutter skeletons -
myproject
├── config.py
├── instance
│ └── config.py
├── myproject
│ ├── commands.py
│ ├── controllers.py
│ ├── extensions.py
│ ├── forms.py
│ ├── __init__.py
│ ├── models.py
│ ├── routes.py
│ └── ui
│ ├── static
│ │ ├── css
│ │ │ └── styles.css
│ │ └── js
│ │ └── custom.js
│ └── templates
│ └── index.html
├── README.md
├── requirements.txt
└── wsgi.py
myproject
├── config.py
├── development.py
├── instance
│ └── config.py
├── myproject
│ ├── auth
│ │ ├── controllers.py
│ │ ├── forms.py
│ │ ├── __init__.py
│ │ ├── models.py
│ │ └── routes.py
│ ├── helpers
│ │ ├── controllers.py
│ │ ├── __init__.py
│ │ └── models.py
│ ├── __init__.py
│ └── ui
│ └── templates
│ ├── 404.html
│ ├── 500.html
│ └── base.html
├── README.md
├── requirements.txt
├── tests
│ ├── auth
│ │ ├── __init__.py
│ │ └── test_controllers.py
│ └── __init__.py
└── wsgi.py
This is an excellent article on this.
Solution 7 - Python
You could get inspired by the cookiecutter templates here to jumpstart your app development
Solution 8 - Python
Here is the basic file structure for flask I use regularly.
yourapp/
static/
js
css
img
templates/
home.html
index.html
app.py
static folder contains all the static files of the website. The templates folder contains all the HTML pages. and app.py contains your python views.
Solution 9 - Python
good idea is to goole for sceletons/template projects on github
for example you can look at this
Solution 10 - Python
In flask we can maintain the mvc structure like as separate all thinks
for example
1 Templets folder contains all html files
2 Static folder contains all css and boostrap related files
3 User defined db folder for db connection and crud operations
4 User defined Model folder for accessing/ binding the data from Templets/front- end to db/back-end connectivity
and after the main activity file
for reference flask file structure link as follow
https://flask.palletsprojects.com/en/1.1.x/tutorial/layout/
Solution 11 - Python
I decided to nest the source code folder under src/
.
my_flask_app (project folder)
├── app.py
├── setup.py
├── src/
│ ├── my_flask_app/ (source code folder)
│ │ ├── config.py
│ │ ├── errors.py
│ │ ├── forms.py
│ │ ├── __init__.py
│ │ ├── models.py
│ │ ├── routes.py
│ │ ├── static/
│ │ └── templates/
│ └── my_flask_app.egg-info/
|
└── tests/ (test folder)
Because of this, I added package_dir={'': 'src'}
in setup.py
.