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?
Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol. A common choice for that is Gunicorn—a Python WSGI HTTP server.
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!
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'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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With