class documentation

class Blueprint(Scaffold): (source)

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 :class:`~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 :class:`~flask.blueprints.BlueprintSetupState` when the blueprint is registered on an application. See :doc:`/blueprints` for more information. :param name: The name of the blueprint. Will be prepended to each endpoint name. :param import_name: The name of the blueprint package, usually ``__name__``. This helps locate the ``root_path`` for the blueprint. :param static_folder: 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. :param static_url_path: 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, and the blueprint's static files won't be accessible. :param template_folder: 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. Blueprint templates have a lower precedence than those in the app's templates folder. :param url_prefix: A path to prepend to all of the blueprint's URLs, to make them distinct from the rest of the app's routes. :param subdomain: A subdomain that blueprint routes will match on by default. :param url_defaults: A dict of default values that blueprint routes will receive by default. :param root_path: 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. .. versionchanged:: 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. .. versionadded:: 0.7

Method __init__ Undocumented
Method add_app_template_filter Register a template filter, available in any template rendered by the application. Works like the :meth:`app_template_filter` decorator. Equivalent to :meth:`.Flask.add_template_filter`.
Method add_app_template_global Register a template global, available in any template rendered by the application. Works like the :meth:`app_template_global` decorator. Equivalent to :meth:`.Flask.add_template_global`.
Method add_app_template_test Register a template test, available in any template rendered by the application. Works like the :meth:`app_template_test` decorator. Equivalent to :meth:`.Flask.add_template_test`.
Method add_url_rule Register a URL rule with the blueprint. See :meth:`.Flask.add_url_rule` for full documentation.
Method after_app_request Like :meth:`after_request`, but after every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.after_request`.
Method app_context_processor Like :meth:`context_processor`, but for templates rendered by every view, not only by the blueprint. Equivalent to :meth:`.Flask.context_processor`.
Method app_errorhandler Like :meth:`errorhandler`, but for every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.errorhandler`.
Method app_template_filter Register a template filter, available in any template rendered by the application. Equivalent to :meth:`.Flask.template_filter`.
Method app_template_global Register a template global, available in any template rendered by the application. Equivalent to :meth:`.Flask.template_global`.
Method app_template_test Register a template test, available in any template rendered by the application. Equivalent to :meth:`.Flask.template_test`.
Method app_url_defaults Like :meth:`url_defaults`, but for every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.url_defaults`.
Method app_url_value_preprocessor Like :meth:`url_value_preprocessor`, but for every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.url_value_preprocessor`.
Method before_app_first_request Register a function to run before the first request to the application is handled by the worker. Equivalent to :meth:`.Flask.before_first_request`.
Method before_app_request Like :meth:`before_request`, but before every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.before_request`.
Method json_decoder.setter Undocumented
Method json_encoder.setter Undocumented
Method make_setup_state Creates an instance of :meth:`~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 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 :meth:`make_setup_state` method.
Method record_once Works like :meth:`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.
Method register Called by :meth:`Flask.register_blueprint` to register all views and callbacks registered on the blueprint with the application. Creates a :class:`.BlueprintSetupState` and calls each :meth:`record` callback with it.
Method register_blueprint Register a :class:`~flask.Blueprint` on this blueprint. Keyword arguments passed to this method will override the defaults set on the blueprint.
Method teardown_app_request Like :meth:`teardown_request`, but after every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.teardown_request`.
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
Property json_decoder Blueprint-local JSON decoder class to use. Set to ``None`` to use the app's.
Property json_encoder Blueprint-local JSON encoder class to use. Set to ``None`` to use the app's.
Method _check_setup_finished Undocumented
Instance Variable _blueprints Undocumented
Instance Variable _got_registered_once Undocumented
Instance Variable _json_decoder Undocumented
Instance Variable _json_encoder Undocumented

Inherited from Scaffold:

Method __repr__ 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. These functions run before rendering a template. The keys of the returned dict are added as variables available in the template.
Method delete Shortcut for :meth:`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 :meth:`add_url_rule`.
Method errorhandler Register a function to handle errors by code or exception class.
Method get Shortcut for :meth:`route` with ``methods=["GET"]``.
Method get_send_file_max_age Used by :func:`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 :attr:`root_path` for reading.
Method patch Shortcut for :meth:`route` with ``methods=["PATCH"]``.
Method post Shortcut for :meth:`route` with ``methods=["POST"]``.
Method put Shortcut for :meth:`route` with ``methods=["PUT"]``.
Method register_error_handler Alternative error attach function to the :meth:`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 :meth:`add_url_rule`, which has more details about the implementation.
Method send_static_file The view function used to serve files from :attr:`static_folder`. A route is automatically registered for this view at :attr:`static_url_path` if :attr:`static_folder` is set.
Method static_folder.setter Undocumented
Method static_url_path.setter Undocumented
Method teardown_request Register a function to be called when the request context is popped. Typically this happens at the end of each request, but contexts may be pushed manually as well during testing.
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 :meth:`before_request` functions.
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 :attr:`static_folder` is set.
Property jinja_loader The Jinja loader for this object's templates. By default this is a class :class:`jinja2.loaders.FileSystemLoader` to :attr:`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.
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 _method_route Undocumented
Instance Variable _static_folder Undocumented
Instance Variable _static_url_path Undocumented
def __init__(self, name: str, import_name: str, static_folder: t.Optional[t.Union[str, os.PathLike]] = None, static_url_path: t.Optional[str] = None, template_folder: t.Optional[t.Union[str, os.PathLike]] = None, url_prefix: t.Optional[str] = None, subdomain: t.Optional[str] = None, url_defaults: t.Optional[dict] = None, root_path: t.Optional[str] = None, cli_group: t.Optional[str] = _sentinel): (source)

Undocumented

@setupmethod
def add_app_template_filter(self, f: ft.TemplateFilterCallable, name: t.Optional[str] = None): (source)

Register a template filter, available in any template rendered by the application. Works like the :meth:`app_template_filter` decorator. Equivalent to :meth:`.Flask.add_template_filter`. :param name: the optional name of the filter, otherwise the function name will be used.

@setupmethod
def add_app_template_global(self, f: ft.TemplateGlobalCallable, name: t.Optional[str] = None): (source)

Register a template global, available in any template rendered by the application. Works like the :meth:`app_template_global` decorator. Equivalent to :meth:`.Flask.add_template_global`. .. versionadded:: 0.10 :param name: the optional name of the global, otherwise the function name will be used.

@setupmethod
def add_app_template_test(self, f: ft.TemplateTestCallable, name: t.Optional[str] = None): (source)

Register a template test, available in any template rendered by the application. Works like the :meth:`app_template_test` decorator. Equivalent to :meth:`.Flask.add_template_test`. .. versionadded:: 0.10 :param name: the optional name of the test, otherwise the function name will be used.

@setupmethod
def add_url_rule(self, rule: str, endpoint: t.Optional[str] = None, view_func: t.Optional[ft.RouteCallable] = None, provide_automatic_options: t.Optional[bool] = None, **options: t.Any): (source)

Register a URL rule with the blueprint. See :meth:`.Flask.add_url_rule` for full documentation. The URL rule is prefixed with the blueprint's URL prefix. The endpoint name, used with :func:`url_for`, is prefixed with the blueprint's name.

@setupmethod
def after_app_request(self, f: T_after_request) -> T_after_request: (source)

Like :meth:`after_request`, but after every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.after_request`.

Like :meth:`context_processor`, but for templates rendered by every view, not only by the blueprint. Equivalent to :meth:`.Flask.context_processor`.

Like :meth:`errorhandler`, but for every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.errorhandler`.

Register a template filter, available in any template rendered by the application. Equivalent to :meth:`.Flask.template_filter`. :param name: the optional name of the filter, otherwise the function name will be used.

Register a template global, available in any template rendered by the application. Equivalent to :meth:`.Flask.template_global`. .. versionadded:: 0.10 :param name: the optional name of the global, otherwise the function name will be used.

Register a template test, available in any template rendered by the application. Equivalent to :meth:`.Flask.template_test`. .. versionadded:: 0.10 :param name: the optional name of the test, otherwise the function name will be used.

@setupmethod
def app_url_defaults(self, f: T_url_defaults) -> T_url_defaults: (source)

Like :meth:`url_defaults`, but for every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.url_defaults`.

Like :meth:`url_value_preprocessor`, but for every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.url_value_preprocessor`.

@setupmethod
def before_app_first_request(self, f: T_before_first_request) -> T_before_first_request: (source)

Register a function to run before the first request to the application is handled by the worker. Equivalent to :meth:`.Flask.before_first_request`. .. deprecated:: 2.2 Will be removed in Flask 2.3. Run setup code when creating the application instead.

@setupmethod
def before_app_request(self, f: T_before_request) -> T_before_request: (source)

Like :meth:`before_request`, but before every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.before_request`.

Undocumented

Undocumented

def make_setup_state(self, app: Flask, options: dict, first_registration: bool = False) -> BlueprintSetupState: (source)

Creates an instance of :meth:`~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.

@setupmethod
def record(self, func: t.Callable): (source)

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 :meth:`make_setup_state` method.

@setupmethod
def record_once(self, func: t.Callable): (source)

Works like :meth:`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.

def register(self, app: Flask, options: dict): (source)

Called by :meth:`Flask.register_blueprint` to register all views and callbacks registered on the blueprint with the application. Creates a :class:`.BlueprintSetupState` and calls each :meth:`record` callback with it. :param app: The application this blueprint is being registered with. :param options: Keyword arguments forwarded from :meth:`~Flask.register_blueprint`. .. versionchanged:: 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. .. versionchanged:: 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``. .. versionchanged:: 2.0.1 Registering the same blueprint with the same name multiple times is deprecated and will become an error in Flask 2.1.

@setupmethod
def register_blueprint(self, blueprint: Blueprint, **options: t.Any): (source)

Register a :class:`~flask.Blueprint` on this blueprint. Keyword arguments passed to this method will override the defaults set on the blueprint. .. versionchanged:: 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``. .. versionadded:: 2.0

@setupmethod
def teardown_app_request(self, f: T_teardown) -> T_teardown: (source)

Like :meth:`teardown_request`, but after every request, not only those handled by the blueprint. Equivalent to :meth:`.Flask.teardown_request`.

cli_group = (source)

Undocumented

Undocumented

subdomain = (source)

Undocumented

url_prefix = (source)

Undocumented

url_values_defaults = (source)

Undocumented

Blueprint-local JSON decoder class to use. Set to ``None`` to use the app's. .. deprecated:: 2.2 Will be removed in Flask 2.3. Customize :attr:`json_provider_class` instead. .. versionadded:: 0.10

Blueprint-local JSON encoder class to use. Set to ``None`` to use the app's. .. deprecated:: 2.2 Will be removed in Flask 2.3. Customize :attr:`json_provider_class` instead. .. versionadded:: 0.10

def _check_setup_finished(self, f_name: str): (source)

Undocumented

_got_registered_once: bool = (source)

Undocumented

_json_decoder = (source)

Undocumented

_json_encoder = (source)

Undocumented