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:
bp = Blueprint("auth", __name__)
Constructor Parameters
| Parameters | Type | Default value | Description |
|---|---|---|---|
| name | str | Required | Blueprint name, used as the endpoint prefix in url_for |
| import_name | str | Required | The name of the module where the blueprint resides, used to locate resources. Usually passed name |
| static_folder | str | None | The blueprint's static files folder |
| static_url_path | str | None | The URL path where static files are exposed externally |
| template_folder | str | None | The blueprint's template folder |
| url_prefix | str | None | A unified prefix for all blueprint routes, such as "/auth" |
| subdomain | str | None | The subdomain that blueprint routes match |
| url_defaults | dict | None | Default URL parameters for blueprint routes |
| root_path | str | None | The blueprint root path, usually auto-detected |
| cli_group | str | None | CLI command group name. Defaults to the blueprint name |
Core Attributes
| property | Type | Description |
|---|---|---|
| name | str | Blueprint name, used as the endpoint prefix after registration |
| cli | AppGroup | The blueprint's dedicated CLI command group, to which custom commands can be added |
| has_static_folder | bool | Whether a static folder is configured |
Route methods
A blueprint is used just like a Flask application, except app is replaced with bp:
| Method | Description |
|---|---|
| route(rule, **options) | Decorator that binds a URL to a view function |
| get / post / put / delete / patch(rule, **options) | Shortcut decorators for HTTP methods |
| add_url_rule(rule, endpoint, view_func, **options) | Add a URL rule programmatically |
Hook decorators
A blueprint's hooks only affect routes within that blueprint:
| Method | Description |
|---|---|
| before_request | Executed before all requests within the blueprint |
| after_request | Executed after a request's response within the blueprint |
| teardown_request | Executed when the request context is torn down 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
| Method | Description |
|---|---|
| register(app, options) | Register the blueprint with a Flask application. Called internally by app.register_blueprint() |
| record(func) | Register a callback function to be called when the blueprint is registered with the application |
| record_once(func) | Same as record, but executed only once even when the blueprint is registered multiple times |
| make_setup_state(app, options) | Create a 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 |
The options parameters when registering a blueprint
The following can be passed when calling app.register_blueprint(bp, **options):
| Parameters | Description |
|---|---|
| url_prefix | Override the blueprint's URL prefix |
| subdomain | Override the blueprint's subdomain |
| url_defaults | Override the blueprint's URL default values |
| name | Rename the blueprint (the same blueprint can be registered multiple times, each with a different name) |
Code Example
Example
from
flask
import
Blueprint
,
render_template
Create a blueprint
bp
=
Blueprint
(
"blog"
,
name
,
url_prefix
=
"/blog"
,
template_folder
=
"templates"
,
static_folder
=
"static"
)
Registration route
@
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}"
Hooks within the blueprint
@
bp.
before_request
def
check_login
(
)
:
Only takes effect for /blog/* paths
pass