Organizing a Flask Project
Beyond a single app.py, real Flask projects typically adopt either the "application factory" pattern (a create_app() function that builds and configures the Flask instance) or a simple package layout with separate modules for models, routes, and config, which avoids circular imports and makes the app testable by allowing multiple app instances with different configs (e.g., testing vs production) to be created.
Cricket analogy: The application factory pattern is like a ground curator preparing a different pitch profile — spin-friendly for a Test in Chennai, pace-friendly for a Test in Perth — from the same underlying groundwork, rather than a single fixed pitch for every match.
Blueprints for Modularity
A Blueprint (from flask import Blueprint) groups related routes, templates, and static files under a common prefix and namespace — for example, a Blueprint("auth", __name__, url_prefix="/auth") registers /auth/login and /auth/logout — and is registered onto the main app with app.register_blueprint(auth_bp); this lets large applications split into independently maintainable features (auth, admin, api) instead of one monolithic routes file.
Cricket analogy: A Blueprint grouping /auth/login and /auth/logout under one namespace is like a franchise organizing its entire bowling attack, pace unit, spin unit, under separate but coordinated sub-units within one squad.
Templates, Static Files, and Config
By convention Flask looks for HTML templates in a templates/ folder (rendered via render_template("index.html", **context) using Jinja2) and for CSS/JS/images in a static/ folder (served at /static/... automatically); configuration is typically layered using classes (class ProductionConfig(Config): DEBUG = False) loaded with app.config.from_object(), so secrets like database URLs come from environment variables rather than being hardcoded into source files.
Cricket analogy: Flask's convention of a fixed templates/ and static/ folder is like the standard fielding positions (slip, gully, mid-on) that every team recognizes by name, so any new player instantly knows where to stand without custom instructions.
myapp/
├── app/
│ ├── __init__.py # create_app() factory
│ ├── auth/
│ │ ├── __init__.py
│ │ └── routes.py # Blueprint("auth", __name__, url_prefix="/auth")
│ ├── templates/
│ │ └── index.html
│ └── static/
│ └── style.css
├── config.py # Config, ProductionConfig, TestingConfig
└── run.py # from app import create_app; app = create_app()The application factory pattern (create_app()) is essential for testing: pytest fixtures can call create_app('testing') to spin up an isolated app instance with a separate test database for each test run.
- The application factory pattern (create_app()) allows multiple, differently configured app instances.
- Blueprints group related routes, templates, and static files under a shared prefix and namespace.
- app.register_blueprint() attaches a Blueprint's routes to the main Flask app.
- templates/ and static/ are Flask's conventional folders for Jinja2 templates and static assets.
- render_template() merges a Jinja2 template with context data to produce HTML.
- Config is best layered with classes (Config, ProductionConfig) loaded via app.config.from_object().
- Secrets like database URLs should come from environment variables, never hardcoded.
Practice what you learned
1. What is the main benefit of the application factory pattern?
2. What does Blueprint("auth", __name__, url_prefix="/auth") do?
3. How is a Blueprint attached to the main Flask app?
4. By default, where does Flask look for Jinja2 templates?
5. Why should secrets like database URLs be loaded from environment variables instead of hardcoded?
Was this page helpful?
You May Also Like
Routing in Flask
Understand how @app.route binds URLs to view functions, how dynamic URL converters validate input, and how to handle HTTP methods and generate URLs with url_for.
Creating a Flask Application
Learn to set up a Flask project with a virtual environment, understand the central application object, and run the development server safely.
What Is Flask?
Flask is a lightweight WSGI micro web framework for Python that provides routing, request handling, and templating while leaving choices like ORM and auth up to the developer.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics
Web DevelopmentNext.js Study Notes
JavaScript · 30 topics