class documentation

class Blueprint(Scaffold):

View In Hierarchy

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 as ~flask.Flask, but defers the need for an application by recording them for later registration.

Decorating a function with a blueprint creates a deferred function that is called with ~flask.blueprints.BlueprintSetupState when the blueprint is registered on an application.

See :doc:`/blueprints` for more information.

Changed in version 1.1.0: Blueprints have a cli group to register nested CLI commands. The cli_group parameter controls the name of the group under the flask command.
New in version 0.7.
Parameters
nameThe name of the blueprint. Will be prepended to each endpoint name.
import​_nameThe name of the blueprint package, usually __name__. This helps locate the root_path for the blueprint.
static​_folderA 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.
static​_url​_pathThe 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, and the blueprint's static files won't be accessible.
template​_folderA 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. Blueprint templates have a lower precedence than those in the app's templates folder.
url​_prefixA path to prepend to all of the blueprint's URLs, to make them distinct from the rest of the app's routes.
subdomainA subdomain that blueprint routes will match on by default.
url​_defaultsA dict of default values that blueprint routes will receive by default.
root​_pathBy 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.
Method __init__ Undocumented
Method ​_is​_setup​_finished Undocumented
Method add​_app​_template​_filter Register a custom template filter, available application wide. Like Flask.add_template_filter but for a blueprint. Works exactly like the app_template_filter decorator.
Method add​_app​_template​_global Register a custom template global, available application wide. Like Flask.add_template_global but for a blueprint. Works exactly like the app_template_global decorator.
Method add​_app​_template​_test Register a custom template test, available application wide. Like Flask.add_template_test but for a blueprint. Works exactly like the app_template_test decorator.
Method add​_url​_rule Like Flask.add_url_rule but for a blueprint. The endpoint for the url_for function is prefixed with the name of the blueprint.
Method after​_app​_request Like Flask.after_request but for a blueprint. Such a function is executed after each request, even if outside of the blueprint.
Method app​_context​_processor Like Flask.context_processor but for a blueprint. Such a function is executed each request, even if outside of the blueprint.
Method app​_errorhandler Like Flask.errorhandler but for a blueprint. This handler is used for all requests, even if outside of the blueprint.
Method app​_template​_filter Register a custom template filter, available application wide. Like Flask.template_filter but for a blueprint.
Method app​_template​_global Register a custom template global, available application wide. Like Flask.template_global but for a blueprint.
Method app​_template​_test Register a custom template test, available application wide. Like Flask.template_test but for a blueprint.
Method app​_url​_defaults Same as url_defaults but application wide.
Method app​_url​_value​_preprocessor Same as url_value_preprocessor but application wide.
Method before​_app​_first​_request Like Flask.before_first_request. Such a function is executed before the first request to the application.
Method before​_app​_request Like Flask.before_request. Such a function is executed before each request, even if outside of a blueprint.
Method make​_setup​_state Creates an instance of ~flask.blueprints.BlueprintSetupState object that is later passed to the register callback functions. Subclasses can override this to return a subclass of the setup state.
Method record No summary
Method record​_once No summary
Method register No summary
Method register​_blueprint Register a ~flask.Blueprint on this blueprint. Keyword arguments passed to this method will override the defaults set on the blueprint.
Method teardown​_app​_request Like Flask.teardown_request but for a blueprint. Such a function is executed when tearing down each request, even if outside of the blueprint.
Class Variable json​_decoder Undocumented
Class Variable json​_encoder Undocumented
Class Variable warn​_on​_modifications Undocumented
Instance Variable ​_blueprints Undocumented
Instance Variable ​_got​_registered​_once Undocumented
Instance Variable cli​_group Undocumented
Instance Variable deferred​_functions Undocumented
Instance Variable name Undocumented
Instance Variable subdomain Undocumented
Instance Variable url​_prefix Undocumented
Instance Variable url​_values​_defaults Undocumented

Inherited from Scaffold:

Static Method ​_get​_exc​_class​_and​_code Get the exception class being handled. For HTTP status codes or HTTPException subclasses, return both the exception and status code.
Method __repr__ Undocumented
Method ​_method​_route Undocumented
Method after​_request Register a function to run after each request to this object.
Method before​_request Register a function to run before each request.
Method context​_processor Registers a template context processor function.
Method delete Shortcut for route with methods=["DELETE"].
Method endpoint Decorate a view function to register it for the given endpoint. Used if a rule is added without a view_func with add_url_rule.
Method errorhandler Register a function to handle errors by code or exception class.
Method get Shortcut for route with methods=["GET"].
Method get​_send​_file​_max​_age Used by send_file to determine the max_age cache value for a given file path if it wasn't passed.
Method open​_resource Open a resource file relative to root_path for reading.
Method patch Shortcut for route with methods=["PATCH"].
Method post Shortcut for route with methods=["POST"].
Method put Shortcut for route with methods=["PUT"].
Method register​_error​_handler Alternative error attach function to the errorhandler decorator that is more straightforward to use for non decorator usage.
Method route Decorate a view function to register it with the given URL rule and options. Calls add_url_rule, which has more details about the implementation.
Method send​_static​_file No summary
Method static​_folder.setter Undocumented
Method static​_url​_path.setter Undocumented
Method teardown​_request No summary
Method url​_defaults Callback function for URL defaults for all view functions of the application. It's called with the endpoint and values and should update the values passed in place.
Method url​_value​_preprocessor Register a URL value preprocessor function for all view functions in the application. These functions will be called before the before_request functions.
Instance Variable ​_static​_folder Undocumented
Instance Variable ​_static​_url​_path Undocumented
Instance Variable after​_request​_funcs Undocumented
Instance Variable before​_request​_funcs Undocumented
Instance Variable cli Undocumented
Instance Variable error​_handler​_spec Undocumented
Instance Variable import​_name Undocumented
Instance Variable root​_path Undocumented
Instance Variable teardown​_request​_funcs Undocumented
Instance Variable template​_context​_processors Undocumented
Instance Variable template​_folder Undocumented
Instance Variable url​_default​_functions Undocumented
Instance Variable url​_value​_preprocessors Undocumented
Instance Variable view​_functions Undocumented
Property has​_static​_folder True if static_folder is set.
Property jinja​_loader The Jinja loader for this object's templates. By default this is a class jinja2.loaders.FileSystemLoader to template_folder if it is set.
Property static​_folder The absolute path to the configured static folder. None if no static folder is set.
Property static​_url​_path The URL prefix that the static route will be accessible from.
def __init__(self, name, import_name, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None, cli_group=_sentinel):

Undocumented

Parameters
name:strUndocumented
import​_name:strUndocumented
static​_folder:t.Optional[t.Union[str, os.PathLike]]Undocumented
static​_url​_path:t.Optional[str]Undocumented
template​_folder:t.Optional[str]Undocumented
url​_prefix:t.Optional[str]Undocumented
subdomain:t.Optional[str]Undocumented
url​_defaults:t.Optional[dict]Undocumented
root​_path:t.Optional[str]Undocumented
cli​_group:t.Optional[str]Undocumented
def _is_setup_finished(self):

Undocumented

Returns
boolUndocumented
def add_app_template_filter(self, f, name=None):
Register a custom template filter, available application wide. Like Flask.add_template_filter but for a blueprint. Works exactly like the app_template_filter decorator.
Parameters
f:TemplateFilterCallableUndocumented
name:t.Optional[str]the optional name of the filter, otherwise the function name will be used.
def add_app_template_global(self, f, name=None):

Register a custom template global, available application wide. Like Flask.add_template_global but for a blueprint. Works exactly like the app_template_global decorator.

New in version 0.10.
Parameters
f:TemplateGlobalCallableUndocumented
name:t.Optional[str]the optional name of the global, otherwise the function name will be used.
def add_app_template_test(self, f, name=None):

Register a custom template test, available application wide. Like Flask.add_template_test but for a blueprint. Works exactly like the app_template_test decorator.

New in version 0.10.
Parameters
f:TemplateTestCallableUndocumented
name:t.Optional[str]the optional name of the test, otherwise the function name will be used.
def add_url_rule(self, rule, endpoint=None, view_func=None, provide_automatic_options=None, **options):
Like Flask.add_url_rule but for a blueprint. The endpoint for the url_for function is prefixed with the name of the blueprint.
Parameters
rule:strUndocumented
endpoint:t.Optional[str]Undocumented
view​_func:t.Optional[t.Callable]Undocumented
provide​_automatic​_options:t.Optional[bool]Undocumented
**options:t.AnyUndocumented
def after_app_request(self, f):
Like Flask.after_request but for a blueprint. Such a function is executed after each request, even if outside of the blueprint.
Parameters
f:AfterRequestCallableUndocumented
Returns
AfterRequestCallableUndocumented
def app_context_processor(self, f):
Like Flask.context_processor but for a blueprint. Such a function is executed each request, even if outside of the blueprint.
Parameters
f:TemplateContextProcessorCallableUndocumented
Returns
TemplateContextProcessorCallableUndocumented
def app_errorhandler(self, code):
Like Flask.errorhandler but for a blueprint. This handler is used for all requests, even if outside of the blueprint.
Parameters
code:t.Union[t.Type[Exception], int]Undocumented
Returns
t.CallableUndocumented
def app_template_filter(self, name=None):
Register a custom template filter, available application wide. Like Flask.template_filter but for a blueprint.
Parameters
name:t.Optional[str]the optional name of the filter, otherwise the function name will be used.
Returns
t.Callable[[TemplateFilterCallable], TemplateFilterCallable]Undocumented
def app_template_global(self, name=None):

Register a custom template global, available application wide. Like Flask.template_global but for a blueprint.

New in version 0.10.
Parameters
name:t.Optional[str]the optional name of the global, otherwise the function name will be used.
Returns
t.Callable[[TemplateGlobalCallable], TemplateGlobalCallable]Undocumented
def app_template_test(self, name=None):

Register a custom template test, available application wide. Like Flask.template_test but for a blueprint.

New in version 0.10.
Parameters
name:t.Optional[str]the optional name of the test, otherwise the function name will be used.
Returns
t.Callable[[TemplateTestCallable], TemplateTestCallable]Undocumented
def app_url_defaults(self, f):
Same as url_defaults but application wide.
Parameters
f:URLDefaultCallableUndocumented
Returns
URLDefaultCallableUndocumented
def app_url_value_preprocessor(self, f):
Same as url_value_preprocessor but application wide.
Parameters
f:URLValuePreprocessorCallableUndocumented
Returns
URLValuePreprocessorCallableUndocumented
def before_app_first_request(self, f):
Like Flask.before_first_request. Such a function is executed before the first request to the application.
Parameters
f:BeforeFirstRequestCallableUndocumented
Returns
BeforeFirstRequestCallableUndocumented
def before_app_request(self, f):
Like Flask.before_request. Such a function is executed before each request, even if outside of a blueprint.
Parameters
f:BeforeRequestCallableUndocumented
Returns
BeforeRequestCallableUndocumented
def make_setup_state(self, app, options, first_registration=False):
Creates an instance of ~flask.blueprints.BlueprintSetupState object that is later passed to the register callback functions. Subclasses can override this to return a subclass of the setup state.
Parameters
app:FlaskUndocumented
options:dictUndocumented
first​_registration:boolUndocumented
Returns
BlueprintSetupStateUndocumented
def record(self, func):
Registers a function that is called when the blueprint is registered on the application. This function is called with the state as argument as returned by the make_setup_state method.
Parameters
func:t.CallableUndocumented
def record_once(self, func):
Works like record but wraps the function in another function that will ensure the function is only called once. If the blueprint is registered a second time on the application, the function passed is not called.
Parameters
func:t.CallableUndocumented
def register(self, app, options):

Called by Flask.register_blueprint to register all views and callbacks registered on the blueprint with the application. Creates a .BlueprintSetupState and calls each record callback with it.

Changed in version 2.0.1: Nested blueprints are registered with their dotted name. This allows different blueprints with the same name to be nested at different locations.
Changed in version 2.0.1: The name option can be used to change the (pre-dotted) name the blueprint is registered with. This allows the same blueprint to be registered multiple times with unique names for url_for.
Changed in version 2.0.1: Registering the same blueprint with the same name multiple times is deprecated and will become an error in Flask 2.1.
Parameters
app:FlaskThe application this blueprint is being registered with.
options:dictKeyword arguments forwarded from ~Flask.register_blueprint.
def register_blueprint(self, blueprint, **options):

Register a ~flask.Blueprint on this blueprint. Keyword arguments passed to this method will override the defaults set on the blueprint.

Changed in version 2.0.1: The name option can be used to change the (pre-dotted) name the blueprint is registered with. This allows the same blueprint to be registered multiple times with unique names for url_for.
New in version 2.0.
Parameters
blueprint:BlueprintUndocumented
**options:t.AnyUndocumented
def teardown_app_request(self, f):
Like Flask.teardown_request but for a blueprint. Such a function is executed when tearing down each request, even if outside of the blueprint.
Parameters
f:TeardownCallableUndocumented
Returns
TeardownCallableUndocumented
json_decoder =

Undocumented

json_encoder =

Undocumented

warn_on_modifications: bool =

Undocumented

_blueprints: t.List[t.Tuple[Blueprint, dict]] =

Undocumented

_got_registered_once: bool =

Undocumented

cli_group =

Undocumented

deferred_functions: t.List[DeferredSetupFunction] =

Undocumented

name =

Undocumented

subdomain =

Undocumented

url_prefix =

Undocumented

url_values_defaults =

Undocumented