liveBook · Manning

Flask blueprint

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A Flask Blueprint is a modular component that groups related routes, view functions, templates, static resources, and other application elements for registration with a Flask application.

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

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Works through package creation, blueprint configuration, route decoration, and factory registration; does not explore Flask's registration internals.
Examples
Uses home and about pages in an introductory blueprint, then an authentication blueprint with a login route.
What it assumes
Assumes familiarity with Flask route decorators and Python packages while demonstrating blueprint creation and registration step by step.
What you can do after
Build it from scratch by moving related handlers and assets into a blueprint package and registering it with a Flask application factory.
How it connects
Connects Python packages and modules with Flask routes, application factories, templates, static files, and authentication.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.