Blueprint
Represents a blueprint, a collection of routes and other app-related functions that can be registered on a real application later. A blueprint is an object that allows defining application functions without requiring an application object ahead of time. It uses the same decorators asFlask, but defers the need for an application by recording them for later registration.
Constructor
The name of the blueprint. Will be prepended to each endpoint name.
The name of the blueprint package, usually
__name__. This helps locate the root_path for the blueprint.A folder with static files that should be served by the blueprint’s static route. The path is relative to the blueprint’s root path. Blueprint static files are disabled by default.
The url to serve static files from. Defaults to
static_folder. If the blueprint does not have a url_prefix, the app’s static route will take precedence.A folder with templates that should be added to the app’s template search path. The path is relative to the blueprint’s root path. Blueprint templates are disabled by default.
A path to prepend to all of the blueprint’s URLs, to make them distinct from the rest of the app’s routes.
A subdomain that blueprint routes will match on by default.
A dict of default values that blueprint routes will receive by default.
By default, the blueprint will automatically set this based on
import_name. In certain situations this automatic detection can fail, so the path can be specified manually instead.The name of the CLI group for commands registered with this blueprint.
Example
Routing Methods
route()
Decorate a view function to register it with the given URL rule and options.The URL rule string.
A list of HTTP methods this rule should handle.
The endpoint name for the route. Defaults to the name of the view function.
Example
add_url_rule()
Register a rule for routing incoming requests and building URLs.HTTP Method Shortcuts
Blueprints provide shortcuts for common HTTP methods:Example
Request Handling Hooks
before_request()
Register a function to run before each request that the blueprint handles.Example
before_app_request()
Register a function to run before every request, not just those handled by this blueprint.after_request()
Register a function to run after each request that the blueprint handles.The response object that will be sent to the client.
Example
after_app_request()
Register a function to run after every request, not just those handled by this blueprint.teardown_request()
Register a function to be called when the request context is popped for requests handled by this blueprint.teardown_app_request()
Register a function to be called when the request context is popped for all requests.Error Handling
errorhandler()
Register a function to handle errors for routes in this blueprint.The HTTP status code as an integer or an exception class.
Example
app_errorhandler()
Register a function to handle errors for all requests, not just those handled by this blueprint.register_error_handler()
Non-decorator alternative toerrorhandler().
Template Context
context_processor()
Register a template context processor function for templates rendered from this blueprint’s views.Example
app_context_processor()
Register a template context processor function for all templates.app_template_filter()
Register a custom template filter available in all templates.app_template_global()
Register a custom template global function available in all templates.app_template_test()
Register a custom template test available in all templates.URL Building
url_defaults()
Register a function to modify keyword arguments when generating URLs for this blueprint.app_url_defaults()
Register a function to modify keyword arguments when generating all URLs.url_value_preprocessor()
Register a URL value preprocessor function for this blueprint.app_url_value_preprocessor()
Register a URL value preprocessor function for all requests.Registration
register_blueprint()
Register a nested blueprint on this blueprint.The blueprint to register.
Override the nested blueprint’s
url_prefix.Example
record()
Register a function that is called when the blueprint is registered on the application.record_once()
Likerecord() but the function is only called once, even if the blueprint is registered multiple times.
Other Utility Methods
open_resource()
Open a resource file relative toroot_path for reading.
Path to the resource relative to
root_path.Open the file in this mode. Only reading is supported.
Open the file with this encoding when opening in text mode.
