quart.blueprints module
- class quart.blueprints.Blueprint(name: str, import_name: str, static_folder: Optional[str] = None, static_url_path: Optional[str] = None, template_folder: Optional[str] = None, url_prefix: Optional[str] = None, subdomain: Optional[str] = None, url_defaults: Optional[dict] = None, root_path: Optional[str] = None, cli_group: Optional[str] = Ellipsis)
Bases:
quart.scaffold.Scaffold
A blueprint is a collection of application properties.
The application properties include routes, error handlers, and before and after request functions. It is useful to produce modular code as it allows the properties to be defined in a blueprint thereby deferring the addition of these properties to the app.
- url_prefix
An additional prefix to every route rule in the blueprint.
- add_app_template_filter(func: Callable[[Any], Any], name: Optional[str] = None) None
Add an application wide template filter.
This is designed to be used on the blueprint directly, and has the same arguments as
add_template_filter()
. An example usage,def filter(): ... blueprint = Blueprint(__name__) blueprint.add_app_template_filter(filter)
- add_app_template_global(func: Callable[[Any], Any], name: Optional[str] = None) None
Add an application wide template global.
This is designed to be used on the blueprint directly, and has the same arguments as
add_template_global()
. An example usage,def global(): ... blueprint = Blueprint(__name__) blueprint.add_app_template_global(global)
- add_app_template_test(func: Callable[[Any], bool], name: Optional[str] = None) None
Add an application wide template test.
This is designed to be used on the blueprint directly, and has the same arguments as
add_template_test()
. An example usage,def test(): ... blueprint = Blueprint(__name__) blueprint.add_app_template_test(test)
- add_url_rule(rule: str, endpoint: Optional[str] = None, view_func: Optional[Union[RouteCallable, WebsocketCallable]] = None, provide_automatic_options: Optional[bool] = None, *, methods: Optional[Iterable[str]] = None, defaults: Optional[dict] = None, host: Optional[str] = None, subdomain: Optional[str] = None, is_websocket: bool = False, strict_slashes: Optional[bool] = None, merge_slashes: Optional[bool] = None) None
Add a route/url rule to the blueprint.
This is designed to be used on the blueprint directly, and has the same arguments as
add_url_rule()
. An example usage,def route(): ... blueprint = Blueprint(__name__) blueprint.add_url_rule('/', route)
- after_app_request(func: quart.blueprints.T_after_request) quart.blueprints.T_after_request
Add a after request function to the app.
This is designed to be used as a decorator, and has the same arguments as
after_request()
. It applies to all requests to the app this blueprint is registered on. An example usage,blueprint = Blueprint(__name__) @blueprint.after_app_request def after(): ...
- after_app_serving(func: quart.blueprints.T_after_serving) quart.blueprints.T_after_serving
Add an after serving function to the App.
This is designed to be used as a decorator, and has the same arguments as
after_serving()
. An example usage,blueprint = Blueprint(__name__) @blueprint.after_app_serving def after(): ...
- after_app_websocket(func: quart.blueprints.T_after_websocket) quart.blueprints.T_after_websocket
Add an after websocket function to the App.
This is designed to be used as a decorator, and has the same arguments as
after_websocket()
. It applies to all requests to the ppe this blueprint is registered on. An example usage,blueprint = Blueprint(__name__) @blueprint.after_app_websocket def after(): ...
- app_context_processor(func: quart.blueprints.T_template_context_processor) quart.blueprints.T_template_context_processor
Add a context processor function to the app.
This is designed to be used as a decorator, and has the same arguments as
context_processor()
. This will add context to all templates rendered. An example usage,blueprint = Blueprint(__name__) @blueprint.app_context_processor def processor(): ...
- app_errorhandler(error: Union[Type[Exception], int]) Callable[[quart.blueprints.T_error_handler], quart.blueprints.T_error_handler]
Add an error handler function to the App.
This is designed to be used as a decorator, and has the same arguments as
errorhandler()
. It applies only to all errors. An example usage,blueprint = Blueprint(__name__) @blueprint.app_errorhandler(404) def not_found(): ...
- app_template_filter(name: Optional[str] = None) Callable[[quart.blueprints.T_template_filter], quart.blueprints.T_template_filter]
Add an application wide template filter.
This is designed to be used as a decorator, and has the same arguments as
template_filter()
. An example usage,blueprint = Blueprint(__name__) @blueprint.app_template_filter() def filter(value): ...
- app_template_global(name: Optional[str] = None) Callable[[quart.blueprints.T_template_global], quart.blueprints.T_template_global]
Add an application wide template global.
This is designed to be used as a decorator, and has the same arguments as
template_global()
. An example usage,blueprint = Blueprint(__name__) @blueprint.app_template_global() def global(value): ...
- app_template_test(name: Optional[str] = None) Callable[[quart.blueprints.T_template_test], quart.blueprints.T_template_test]
Add an application wide template test.
This is designed to be used as a decorator, and has the same arguments as
template_test()
. An example usage,blueprint = Blueprint(__name__) @blueprint.app_template_test() def test(value): ...
- app_url_defaults(func: quart.blueprints.T_url_defaults) quart.blueprints.T_url_defaults
Add a url default preprocessor.
This is designed to be used as a decorator, and has the same arguments as
url_defaults()
. This will apply to all urls. An example usage,blueprint = Blueprint(__name__) @blueprint.app_url_defaults def default(endpoint, values): ...
- app_url_value_preprocessor(func: quart.blueprints.T_url_value_preprocessor) quart.blueprints.T_url_value_preprocessor
Add a url value preprocessor.
This is designed to be used as a decorator, and has the same arguments as
app_url_value_preprocessor()
. This will apply to all URLs. An example usage,blueprint = Blueprint(__name__) @blueprint.app_url_value_preprocessor def processor(endpoint, view_args): ...
- before_app_first_request(func: quart.blueprints.T_before_first_request) quart.blueprints.T_before_first_request
Add a before request first function to the app.
This is designed to be used as a decorator, and has the same arguments as
before_first_request()
. It is triggered before the first request to the app this blueprint is registered on. An example usage,blueprint = Blueprint(__name__) @blueprint.before_app_first_request def before_first(): ...
- before_app_request(func: quart.blueprints.T_before_request) quart.blueprints.T_before_request
Add a before request function to the app.
This is designed to be used as a decorator, and has the same arguments as
before_request()
. It applies to all requests to the app this blueprint is registered on. An example usage,blueprint = Blueprint(__name__) @blueprint.before_app_request def before(): ...
- before_app_serving(func: quart.blueprints.T_before_serving) quart.blueprints.T_before_serving
Add a before serving to the App.
This is designed to be used as a decorator, and has the same arguments as
before_serving()
. An example usage,blueprint = Blueprint(__name__) @blueprint.before_app_serving def before(): ...
- before_app_websocket(func: quart.blueprints.T_before_websocket) quart.blueprints.T_before_websocket
Add a before websocket to the App.
This is designed to be used as a decorator, and has the same arguments as
before_websocket()
. It applies to all requests to the app this blueprint is registered on. An example usage,blueprint = Blueprint(__name__) @blueprint.before_app_websocket def before(): ...
- make_setup_state(app: Quart, options: dict, first_registration: bool = False) BlueprintSetupState
Return a blueprint setup state instance.
- Parameters
first_registration – True if this is the first registration of this blueprint on the app.
options – Keyword arguments forwarded from
register_blueprint()
.first_registration – Whether this is the first time this blueprint has been registered on the application.
- record(func: Callable[[quart.blueprints.BlueprintSetupState], Callable]) None
Used to register a deferred action.
- record_once(func: Callable[[quart.blueprints.BlueprintSetupState], Callable]) None
Used to register a deferred action that happens only once.
- register(app: Quart, options: dict) None
Register this blueprint on the app given.
- Parameters
app – The application this blueprint is being registered with.
options – Keyword arguments forwarded from
register_blueprint()
.first_registration – Whether this is the first time this blueprint has been registered on the application.
- register_blueprint(blueprint: quart.blueprints.Blueprint, **options: Any) None
Register a
Blueprint
on this blueprint.Keyword arguments passed to this method will override the defaults set on the blueprint.
- teardown_app_request(func: quart.blueprints.T_teardown) quart.blueprints.T_teardown
Add a teardown request function to the app.
This is designed to be used as a decorator, and has the same arguments as
teardown_request()
. It applies to all requests to the app this blueprint is registered on. An example usage,blueprint = Blueprint(__name__) @blueprint.teardown_app_request def teardown(): ...
- teardown_app_websocket(func: quart.blueprints.T_teardown) quart.blueprints.T_teardown
Add a teardown websocket function to the app.
This is designed to be used as a decorator, and has the same arguments as
teardown_websocket()
. It applies to all requests to the app this blueprint is registered on. An example usage,blueprint = Blueprint(__name__) @blueprint.teardown_app_websocket def teardown(): ...
- warn_on_modifications = False
- while_app_serving(func: quart.blueprints.T_while_serving) quart.blueprints.T_while_serving
Add a while serving function to the App.
This is designed to be used as a decorator, and has the same arguments as
while_serving()
. An example usage,@blueprint.while_serving async def func(): ... # Startup yield ... # Shutdown
- class quart.blueprints.BlueprintSetupState(blueprint: Blueprint, app: Quart, options: dict, first_registration: bool)
Bases:
object
This setups the blueprint on the app.
When used it can apply the deferred functions on the Blueprint to the app. Override if you wish for blueprints to have be registered in different ways.
- first_registration
True if this is the first registration of this blueprint on the app.
- add_url_rule(path: str, endpoint: Optional[str] = None, view_func: Optional[Callable] = None, *, methods: Optional[Iterable[str]] = None, defaults: Optional[dict] = None, host: Optional[str] = None, subdomain: Optional[str] = None, provide_automatic_options: Optional[bool] = None, is_websocket: bool = False, strict_slashes: Optional[bool] = None, merge_slashes: Optional[bool] = None) None
- register_template_filter(func: Callable[[Any], Any], name: Optional[str]) None
- register_template_global(func: Callable, name: Optional[str]) None
- register_template_test(func: Callable, name: Optional[str]) None