Advanced level · Module 4: Building Web APIs with Flask · Lesson 5 of 5
Structuring a Larger Flask Project
Organise a growing Flask application into an application factory and blueprints so it stays maintainable.
In this lesson you will
- Create the app with an application factory
- Group related routes into blueprints
Key concepts
application factoryblueprintconfiguration objectseparation of concerns
The lesson
A single app.py works for a few routes but becomes hard to manage. An application factory — a create_app() function — builds and configures the app, which makes it easy to create a test version with different settings.
Blueprints group related routes: products, sales and reports each get their own module, registered with the app in create_app().
Keep layers separate: routes handle HTTP, the data-access module handles SQL and validators handle rules. Each layer can then be changed and tested on its own.
Step by step
- Move app creation into create_app().
- Create a products blueprint and move its routes.
- Register the blueprint in the factory.
- Update the tests to use create_app() and run them.
Practice activity
Restructure your inventory API into a package with an application factory and two blueprints, keeping all tests passing.
Expected result: A packaged API with unchanged behaviour and a passing test suite.
Check your understanding
-
How does an application factory help testing?
Show answer
Tests can call create_app() with test settings, such as a temporary database, to get an isolated app instance.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.