I'm building my first Flask app and I can't figure out a good, clean Pythonic way of organizing my application. I don't want to have everything in a single .py file as in their example. I would like to have each part of my app in a separate module. What would be a good way to organize things?
-
4I quite like the layout explained here: http://flask.pocoo.org/docs/patterns/packages/ – obmarg Feb 22 '12 at 13:19
-
1There they are using circular imports which i would like to avoid. – daniels Feb 22 '12 at 13:21
-
True, but I don't think it's avoidable if you want to use the app.route decorator. – obmarg Feb 22 '12 at 13:28
6 Answers
I have created a Flask boilerplate project called "Fbone", please feel free to check it out and fork :)
Fbone (Flask bone) is a Flask (Python microframework) template/bootstrap/boilerplate application.
Overview
- Well designed for big project using blueprint.
- Integrate with hottest frontend framework: jQuery / html5boilerplate / bootstrap.
- Backed by the famous SQLalchemy.
- Implement tricky "remember me" by flask-login.
- Handle web forms by flask-wtform.
- Unit testing with flask-testing and nose.
- Easily deploy via fabric and mod_wsgi (example included).
- i18n by flask-babel
btw, I just found this wiki on building a large project with Flask useful, pls check it!

- 2,942
- 24
- 25
-
-
Before you run 'fab d' as detailed in the README, you need to activate your virtual environment with 'source env/bin/activate' – JRG Jun 15 '13 at 18:54
-
1
-
1@imwilsonxu I couldn't tell where you put multiple models. Do you put models in different files like a php framework does? – johnny Jan 29 '15 at 22:12
-
(fbone)MacBook-Pro-de-Pyderman:flask-boilerplate Pyderman$ `fab setup` `Warning: Command(s) not found: setup` – Pyderman Feb 16 '16 at 14:26
-
@imwilsonxu Can the api part in the fbone project be used by mobile apps? So that the application based on the fbone project can be accessed from web and by mobile native clients? – Nurjan Dec 05 '16 at 03:53
Make sure to read Matt Wright's wonderful post on the subject.
The post features:
A description of a structure for large flask projects
A description of best design practices in general when it comes to large web apps, like the MVC pattern, App factories, Services and Data Migration to name a few (most interesting feature IMHO).
Flask 0.7 implements Blueprints. They are great for using the route
decorator without importing the main application object.

- 4,132
- 1
- 24
- 25
-
1
-
1Technically, a blueprint is an instance of the Blueprint class. They can span one or more modules, or several blueprints can coexist in the same module. There is some magic behind the scenes to figure out a blueprint's module or package, to find its `templates` and `static` folders. – Alex Morega Feb 23 '12 at 06:43
-
1I've integrate blueprints in my flask bone project, see my answer pls. – imwilsonxu Dec 02 '12 at 11:08
I'm working on a (by my standards) big Flask project (5000 lines of Python code and it's only half-finished). The customer wants the project to be modular, so I took this apporach:
My folder structure looks like this:
├── __init__.py
├── modules.yml
├── config
├── controllers
│ └── ...
├── lib: Common functions I use often
│ └── ...
├── models
│ └── ...
├── static: All static files
│ ├── css
│ ├── img
│ └── js
└── templates: Jinja2 templates
└── ...
In modules.yml
I define my modules including name and URL. This way the customer is able to enable/disable modules without touching a single Python file. In addition, I generate the menus based on the modules list. By convention every module has it its own Python-module in controllers/
that will load its model
from models/
. Every controller defines a Blueprint
stored as the controller's name. E.g. for a user
module, I have in controllers/user.py
:
# Module name is 'user', thus save Blueprint as 'user' variable
user = Blueprint('user', __name__)
@user.route('/user/')
def index():
pass
This way, I can read the modules.yml
in my __init__.py
and load and register all enabled modules dynamically:
# Import modules
for module in modules:
# Get module name from 'url' setting, exculde leading slash
modname = module['url'][1:]
try:
# from project.controllers.<modname> import <modname>
mod = __import__(
'project.controllers.' + modname, None, None, modname
)
except Exception as e:
# Log exceptions here
# [...]
mod = getattr(mod, modname) # Get blueprint from module
app.register_blueprint(mod, url_prefix=module['url'])
I hope, this can be some inspiration for you :)

- 2,223
- 1
- 28
- 38
-
-
1The structure described above is actually a subdirectory with the project name (`project_name/`). There also is a `tests/` folder and a `scripts/` folder (for executable scripts). For `tests/` it would be clever to use the same structure as in the answer: `tests/models/` for model tests, `tests/controllers/` for controller tests, you name it. That would require some overhead for maintaining the structure but would make it very simple to find the files you need. – msiemens Dec 05 '14 at 23:05
I worked on a social network built on top of Flask. The special thing about my project was that the server is purely serving API endpoints and the frontend is a one-page Backbone app. The Flask structure I took is the following:
├── app
│ ├── api
│ │ ├── auth.py
│ │ └── ...
│ ├── app.py
│ ├── common
│ │ ├── constants.py
│ │ ├── helpers.py
│ │ ├── response.py
│ │ └── ...
│ ├── config.py
│ ├── extensions.py
│ ├── frontend
│ │ └── controllers.py
│ ├── static
│ │ └── ...
│ ├── templates
│ │ ├── app.html
│ │ └── ...
│ └── users
│ ├── UserConstants.py
│ ├── UserForms.py
│ ├── UserHelpers.py
│ ├── UserModels.py
│ └── __init__.py
├── alembic
| ├── version
│ └── ...
├── tests
│ └── ...
You can read the more in-depth post I wrote on the topic here. I found it to be much more intuitive to separate different functional areas to its own folder.
I worked on the code a while ago and open sourced it completely! You can check it out on github.

- 405
- 6
- 11
I have created a Flask app yapper from scratch and integrated it with gulp for both frontend and backend development. It is a simple blog engine but can be easily modified for developing according to requirements. It is well structured using Blueprints.
Checkout the project page yapper

- 182
- 3
- 10