Common folder/file structure in Flask app
Question:
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
?
Answers:
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.
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
You could get inspired by the cookiecutter templates here to jumpstart your app development
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
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
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!"
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.
good idea is to goole for sceletons/template projects on github
for example you can look at this
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.
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/
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
.
For larger projects here’s what my directory structure looks like
app_name(root folder)
│ .env
│ .gitignore
│ .gitattributes
│ README.MD
│ pyproject.toml
│
└── app(source code)
│ │ __init__.py
│ │ __main__.py
│ │ db.py
│ │ auth.py
│ │ forms.py
│ │ utils.py
│ │ config.py
│ │
│ └─── routes
│ __init__.py
│ views.py
│ api.py
│ auth.py
│
└─ resources
│ |─── static
│ │ css
│ │ js
│ │ images
│ │
│ └─── templates
│ base
| components
│
└─ tests
└─ venv
└─ docs
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
?
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.
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
You could get inspired by the cookiecutter templates here to jumpstart your app development
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
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
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!"
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.
good idea is to goole for sceletons/template projects on github
for example you can look at this
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.
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/
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
.
For larger projects here’s what my directory structure looks like
app_name(root folder)
│ .env
│ .gitignore
│ .gitattributes
│ README.MD
│ pyproject.toml
│
└── app(source code)
│ │ __init__.py
│ │ __main__.py
│ │ db.py
│ │ auth.py
│ │ forms.py
│ │ utils.py
│ │ config.py
│ │
│ └─── routes
│ __init__.py
│ views.py
│ api.py
│ auth.py
│
└─ resources
│ |─── static
│ │ css
│ │ js
│ │ images
│ │
│ └─── templates
│ base
| components
│
└─ tests
└─ venv
└─ docs