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, root_path: Optional[str] = None)

Bases: quart.static.PackageStatic

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 defering the addition of these properties to the app.

json_decoder

The decoder to use for routes in this blueprint, the default, None, indicates that the app encoder should be used.

json_encoder

The encoder to use for routes in this blueprint, the default, None, indicates that the app encoder should be used.

url_prefix

An additional prefix to every route rule in the blueprint.

add_app_template_filter(func: Callable, 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, 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, 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(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: bool = True) → 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)
add_websocket(path: str, endpoint: Optional[str] = None, view_func: Optional[Callable] = None, defaults: Optional[dict] = None, host: Optional[str] = None, subdomain: Optional[str] = None, *, strict_slashes: bool = True) → None

Add a websocket rule to the blueprint.

This is designed to be used on the blueprint directly, and has the same arguments as add_websocket(). An example usage,

def route():
    ...

blueprint = Blueprint(__name__)
blueprint.add_websocket('/', route)
after_app_request(func: Callable) → Callable

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_websocket(func: Callable) → Callable

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 registerd on. An example usage,

blueprint = Blueprint(__name__)
@blueprint.after_app_websocket
def after():
    ...
after_request(func: Callable) → Callable

Add an after request function to the Blueprint.

This is designed to be used as a decorator, and has the same arguments as after_request(). It applies only to requests that are routed to an endpoint in this blueprint. An example usage,

blueprint = Blueprint(__name__)
@blueprint.after_request
def after():
    ...
after_websocket(func: Callable) → Callable

Add an after websocket function to the Blueprint.

This is designed to be used as a decorator, and has the same arguments as after_websocket(). It applies only to requests that are routed to an endpoint in this blueprint. An example usage,

blueprint = Blueprint(__name__)
@blueprint.after_websocket
def after():
    ...
app_context_processor(func: Callable) → Callable

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

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

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

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

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: Callable) → Callable

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: Callable) → Callable

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: Callable) → Callable

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: Callable) → Callable

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_websocket(func: Callable) → Callable

Add a before request 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():
    ...
before_request(func: Callable) → Callable

Add a before request function to the Blueprint.

This is designed to be used as a decorator, and has the same arguments as before_request(). It applies only to requests that are routed to an endpoint in this blueprint. An example usage,

blueprint = Blueprint(__name__)
@blueprint.before_request
def before():
    ...
before_websocket(func: Callable) → Callable

Add a before request websocket to the Blueprint.

This is designed to be used as a decorator, and has the same arguments as before_websocket(). It applies only to requests that are routed to an endpoint in this blueprint. An example usage,

blueprint = Blueprint(__name__)
@blueprint.before_websocket
def before():
    ...
context_processor(func: Callable) → Callable

Add a context processor function to this blueprint.

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 in this blueprint’s routes. An example usage,

blueprint = Blueprint(__name__)
@blueprint.context_processor
def processor():
    ...
endpoint(endpoint: str) → Callable

Add an endpoint to the blueprint.

This is designed to be used as a decorator, and has the same arguments as endpoint(). An example usage,

blueprint = Blueprint(__name__)
@blueprint.endpoint('index')
def index():
    ...
errorhandler(error: Union[Type[Exception], int]) → Callable

Add an error handler function to the Blueprint.

This is designed to be used as a decorator, and has the same arguments as errorhandler(). It applies only to errors that originate in routes in this blueprint. An example usage,

blueprint = Blueprint(__name__)
@blueprint.errorhandler(404)
def not_found():
    ...
json_decoder: Optional[Type[JSONDecoder]] = None
json_encoder: Optional[Type[JSONEncoder]] = None
make_setup_state(app: Quart, first_registration: bool, *, url_prefix: Optional[str] = None) → BlueprintSetupState

Return a blueprint setup state instance.

Parameters
  • first_registration – True if this is the first registration of this blueprint on the app.

  • url_prefix – An optional prefix to all rules

record(func: Callable[[BlueprintSetupState], Callable]) → None

Used to register a deferred action.

record_once(func: Callable[[BlueprintSetupState], Callable]) → None

Used to register a deferred action that happens only once.

register(app: Quart, first_registration: bool, *, url_prefix: Optional[str] = None) → None

Register this blueprint on the app given.

register_error_handler(error: Union[Type[Exception], int], func: Callable) → None

Add an error handler function to the blueprint.

This is designed to be used on the blueprint directly, and has the same arguments as register_error_handler(). An example usage,

def not_found():
    ...

blueprint = Blueprint(__name__)
blueprint.register_error_handler(404, not_found)
route(path: str, methods: Optional[List[str]] = None, endpoint: Optional[str] = None, defaults: Optional[dict] = None, host: Optional[str] = None, subdomain: Optional[str] = None, *, provide_automatic_options: Optional[bool] = None, strict_slashes: bool = True) → Callable

Add a route to the blueprint.

This is designed to be used as a decorator, and has the same arguments as route(). An example usage,

blueprint = Blueprint(__name__)
@blueprint.route('/')
def route():
    ...
teardown_app_request(func: Callable) → Callable

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_request(func: Callable) → Callable

Add a teardown request function to the Blueprint.

This is designed to be used as a decorator, and has the same arguments as teardown_request(). It applies only to requests that are routed to an endpoint in this blueprint. An example usage,

blueprint = Blueprint(__name__)
@blueprint.teardown_request
def teardown():
    ...
teardown_websocket(func: Callable) → Callable

Add a teardown websocket function to the Blueprint.

This is designed to be used as a decorator, and has the same arguments as teardown_websocket(). It applies only to requests that are routed to an endpoint in this blueprint. An example usage,

blueprint = Blueprint(__name__)
@blueprint.teardown_websocket
def teardown():
    ...
url_defaults(func: Callable) → Callable

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 urls in this blueprint. An example usage,

blueprint = Blueprint(__name__)
@blueprint.url_defaults
def default(endpoint, values):
    ...
url_value_preprocessor(func: Callable) → Callable

Add a url value preprocessor.

This is designed to be used as a decorator, and has the same arguments as url_value_preprocessor(). This will apply to urls in this blueprint. An example usage,

blueprint = Blueprint(__name__)
@blueprint.url_value_preprocessor
def processor(endpoint, view_args):
    ...
websocket(path: str, endpoint: Optional[str] = None, defaults: Optional[dict] = None, host: Optional[str] = None, subdomain: Optional[str] = None, *, strict_slashes: bool = True) → Callable

Add a websocket to the blueprint.

This is designed to be used as a decorator, and has the same arguments as websocket(). An example usage,

blueprint = Blueprint(__name__)
@blueprint.websocket('/')
async def route():
    ...
class quart.blueprints.BlueprintSetupState(blueprint: quart.blueprints.Blueprint, app: Quart, first_registration: bool, *, url_prefix: Optional[str] = None)

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: bool = True) → None
register_endpoint(endpoint: str, func: Callable) → None
register_template_filter(func: Callable, name: Optional[str]) → None
register_template_global(func: Callable, name: Optional[str]) → None
register_template_test(func: Callable, name: Optional[str]) → None