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
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.
Flask 0.7 implements Blueprints. They are great for using the route
decorator without importing the main application object.
Make sure to read Matt Wright's wonderful post on the subject.
The post features:
A description of a structure for large flask projects
An example application on Github
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).
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
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
btw, I just found this wiki on building a large project with Flask useful, pls check it!
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 :)