Flask Blueprint Object API

Blueprint is used to split an application into reusable modules. Each blueprint can have its own routes, templates, static files, and hooks.

Creation method:

bp = Blueprint("auth", __name__)

Constructor parameters

Parameter Type Default value Description
name str Required Blueprint name, used for the endpoint prefix in url_for
import_name str Required The module name where the blueprint resides, used to locate resources. Usually pass __name__.
static_folder str None Blueprint static file folder
static_url_path str None The URL path at which static files are exposed.
template_folder str None Blueprint template folder
url_prefix str None A common prefix for all routes in the blueprint, e.g., "/auth".
subdomain str None Subdomain matched by blueprint routes
url_defaults dict None Default URL parameters for blueprint routes
root_path str None Blueprint root path, usually auto-discovered
cli_group str None The CLI command group name. Uses the blueprint name by default.

Core attributes

Property Type Description
name str Blueprint name, used as the endpoint prefix after registration
cli AppGroup The blueprint-specific CLI command group, where custom commands can be added.
has_static_folder bool Whether a static folder is configured

Route methods

The usage of blueprints is the same as a Flask application, exceptappReplace withbp:

Methods Description
route(rule, **options) Decorator that binds a URL to a view function.
get / post / put / delete / patch(rule, **options) HTTP method shortcut decorators
add_url_rule(rule, endpoint, view_func, **options) Adding URL rules programmatically

Hook decorator

The blueprint's hooks only affect routes within the blueprint:

Methods Description
before_request Execute before all requests within the blueprint
after_request Execute after the request response within the blueprint
teardown_request Execute when the request context is destroyed within the blueprint
errorhandler(code_or_exception) Error handlers within the blueprint
url_value_preprocessor URL value preprocessors within the blueprint
url_defaults Default URL parameters within the blueprint
template_filter / template_test / template_global Blueprint template filters / tests / global functions.

Blueprint-specific methods

Methods Description
register(app, options) Register the blueprint to the Flask application. Called internally by app.register_blueprint()
record(func) Register a callback function that is called when the blueprint is registered to the application
record_once(func) Same as record, but executed only once when the blueprint is registered multiple times
make_setup_state(app, options) Create blueprint registration state object
send_static_file(filename) Send a file from the blueprint's static folder
open_resource(resource, mode="rb") Open a resource file under the blueprint's root path

Options parameters when registering the blueprint

Callapp.register_blueprint(bp, **options)When calling, you can pass:

Parameter Description
url_prefix Override the blueprint's URL prefix
subdomain Override the blueprint's subdomain
url_defaults Override the blueprint's URL defaults
name Rename the blueprint (the same blueprint can be registered multiple times, each time with a different name)

Code Examples

Example

from flask import Blueprint, render_template

# Create blueprint
bp = Blueprint("blog", __name__,
               url_prefix="/blog",
               template_folder="templates",
               static_folder="static")

# Register routes
@bp.route("/")
def index():
    return render_template("blog/index.html")

@bp.route("/<int:post_id>")
def show(post_id):
    return f"Show post {post_id}"

# Blueprint hooks
@bp.before_request
def check_login():
    # Only applies to /blog/* routes
    pass

# Register in the app
# app.register_blueprint(bp)
other extensions