Flask Project Template
Flask is very flexible when it comes to define the project layout, as a result, there are several different approaches, which can be confusing if you're building your first application.
Follow this template if you want an application that meets these requirements:
- Use SQLAlchemy as ORM.
- Use pytest as testing framework (instead of unittest).
- Sets a robust foundation for application growth.
- Set a clear defined project structure that can be used for frontend applications as well as backend APIs.
- Microservice friendly.
I've crafted this template after studying the following projects:
- Miguel's Flask mega tutorial (code).
- Greb Obinna Flask-RESTPlus tutorial (code).
- Abhinav Suri Flask tutorial (code).
- Patrick's software blog project layout and pytest definition (code).
- Jaime Buelta Hands On Docker for Microservices with Python (code).
Each has it's strengths and weaknesses:
Project | Alembic | Pytest | Complex app | Friendly layout | Strong points |
---|---|---|---|---|---|
Miguel | True | False | True | False | Has a book explaining the code |
Greb | False | False | False | False | flask-restplus |
Abhinav | True | False | True | True | flask-base |
Patrick | False | True | False | True | pytest |
Jaime | False | True | True | False | Microservices, CI, Kubernetes, logging, metrics |
I'm going to start with Abhinav base layout as it's the most clear and complete. Furthermore, it's based in flask-base, a simple Flask boilerplate app with SQLAlchemy, Redis, User Authentication, and more. Which can be used directly to start a frontend flask project. I won't use it for a backend API though.
With that base layout, I'm going to take Patrick's pytest layout to configure the tests using pytest-flask
, Greb flask-restplus
code to create the API and Miguel's book to glue everything together.
Finally, I'll follow Jaime's book to merge the different microservices into an integrated project. As well as defining the deployment process, CI definition, logging, metrics and integration with Kubernetes.