As a Flask application grows, keeping every route beside the application setup becomes unwieldy. A blueprint groups routes for a distinct area of functionality in its own Python package. This gives related URL endpoints a namespace and lets you work on one feature without crowding a single file. section 7.4 section 7.4.1
To move introductory pages into a blueprint:
- Create an `intro` package. In its `__init__.py`, instantiate `Blueprint` with a name and module location; set paths for its templates and static files. section 7.4.2
- In a separate module, import the blueprint and decorate handlers with `@intro_bp.route(...)` instead of `@app.route(...)`. section 7.4.2
- Import the package and call `app.register_blueprint(intro.intro_bp)` in the `create_app()` application factory. section 7.4.2
from flask import Blueprint #1
intro_bp = Blueprint('intro_bp', __name__, #2
static_folder="static", #2
static_url_path="/intro/static", #2
template_folder="templates" #2
) #2
from app.intro import intro #3
The blueprint definition places template and static folders beside the feature package. Its explicit static URL path avoids a conflict with the application's root static folder. section 7.4.2
Grow the feature, not the application file
The same `intro_bp` handles `/` and, after another decorated handler is added, `/about`. The about handler renders `about.html` from the blueprint's templates directory. A separate authentication package can follow the same pattern for `/login`, with both blueprints registered by the application factory. section 7.4.2 section 7.4.3 section 8.2.1
Use a blueprint when routes belong to a coherent feature, such as introductory pages or authentication. Keeping that feature together makes the application easier to extend and can make the functionality reusable in another project. section 7.4 section 7.4.1
