My little Flask app keeps growing and the more features I add, the more of a mess it gets. Is there some help to structure the application in a more manageable way?
This post is part of my journey to learn Python. You can find the other parts of this series here.
No clear guidance
If you google for ways to structure your Flask application, you will find many different approaches:
- Flask: Project Layout (for smaller applications)
- Flask: Larger Applications
- Kelvin Mwinuka: Structuring Large Applications In Flask Using Blueprints
- Explore Flask: Organizing your project
- Arash Soheili: Structuring a Large Production Flask Application
These approaches are just a selection of all the possible ways I found. While they all address the problem, none feels like a clear winner for me.
A structure I like
The structure I favour at the moment is from Michael Kennedy as he explains it in his course Building Data-Driven Web Apps with Flask and SQLAlchemy. Here you have a place for all the things you may need. While I try to follow the YAGNI principle, I prefer to have the same structure in all the applications I build – even when they currently have no need for a specific folder. This way I can switch between applications without remembering where I may have put a specific function.
With this approach, you have inside your Git repository these folders:
Path | Purpose |
---|---|
/myproject | your web application |
/myproject/bin | utilities and other executables |
/myproject/data | the models that represent your database tables |
/myproject/db | your SQLite database file (if you use SQLite) |
/myproject/infrastructure | things like cookie authentication or decorators |
/myproject/services | reusable parts like database query abstractions |
/myproject/static | static resources (no Python code) |
/myproject/static/css | CSS files |
/myproject/static/img | Images |
/myproject/static/js | JavaScript files |
/myproject/templates | reusable HTML templates |
/myproject/templates/topic/ | HTML templates for a specific topic |
/myproject/viewmodels/ | reusable ViewModels |
/myproject/viewmodels/topic/ | ViewModels for a specific topic |
/myproject/views/ | reusable view functions |
/myproject/views/topic.py | view functions for a specific topic |
/myproject/app.py | your Flask application starting file |
/server/ | everything you need to run your application on a web server |
/tests/ | all your unit and integration tests |
/venv/ | the virtual environment for your application |
/requirements.txt | the dependencies for your application |
I especially like at this structure that the ViewModels for a specific topic (like user authentication, products, etc) are in a folder with the same name as the templates or the view functions. This helps me a lot to see what belongs together without the need to read the whole file.
Create the structure
With this script for the Windows command prompt you can create this structure for your project. You only need to replace myproject with the name of your project (be aware that there are no spaces or other characters around the project name):
1 2 3 4 5 6 7 |
SET myproj=myproject mkdir %myproj% %myproj%\bin %myproj%\data %myproj%\db %myproj%\infrastructure %myproj%\services %myproj%\static %myproj%\static\css %myproj%\static\img %myproj%\static\js %myproj%\templates %myproj%\templates\topic %myproj%\viewmodels %myproj%\viewmodels\topic %myproj%\views server test venv type NUL > requirements.txt type NUL > %myproj%\views\topic.py type NUL > %myproj%\app.py |
Next
Having the structure in place is a good start, but we are not done yet. Before we can move our application to that new structure, we need to have a look at Blueprint. Otherwise all our effort will end in an error message.
3 thoughts on “Python Friday #41: Structure Your Flask App”