quart.blueprints module#

class quart.blueprints.Blueprint(*args: Any, **kwargs: Any)#

Bases: Blueprint

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.

add_websocket(rule: str, endpoint: str | None = None, view_func: WebsocketCallable | None = None, **options: t.Any) None#

Add a websocket url rule to the application.

This is designed to be used on the application directly. An example usage,

def websocket_route():
    ...

app.add_websocket('/', websocket_route)
Parameters:
  • rule – The path to route on, should start with a /.

  • endpoint – Optional endpoint name, if not present the function name is used.

  • view_func – Callable that returns a response.

  • defaults

    A dictionary of variables to provide automatically, use to provide a simpler default path for a route, e.g. to allow for /book rather than /book/0,

    @app.websocket('/book', defaults={'page': 0})
    @app.websocket('/book/<int:page>')
    def book(page):
        ...
    

  • host – The full host name for this route (should include subdomain if needed) - cannot be used with subdomain.

  • subdomain – A subdomain for this specific route.

  • strict_slashes – Strictly match the trailing slash present in the path. Will redirect a leaf (no slash) to a branch (with slash).

after_app_serving(func: T_after_serving) 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: T_after_websocket) 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():
    ...
after_websocket(func: T_after_websocket) T_after_websocket#

Add an after websocket function.

This is designed to be used as a decorator, if used to decorate a synchronous function, the function will be wrapped in run_sync() and run in a thread executor (with the wrapped function returned). An example usage,

@app.after_websocket
async def func(response):
    return response
Parameters:

func – The after websocket function itself.

before_app_serving(func: T_before_serving) 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: T_before_websocket) 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():
    ...
before_websocket(func: T_before_websocket) T_before_websocket#

Add a before websocket function.

This is designed to be used as a decorator, if used to decorate a synchronous function, the function will be wrapped in run_sync() and run in a thread executor (with the wrapped function returned). An example usage,

@app.before_websocket
async def func():
    ...
Parameters:

func – The before websocket function itself.

get_send_file_max_age(filename: str | None) int | None#

Used by send_file() to determine the max_age cache value for a given file path if it wasn’t passed.

By default, this returns SEND_FILE_MAX_AGE_DEFAULT from the configuration of current_app. This defaults to None, which tells the browser to use conditional requests instead of a timed cache, which is usually preferable.

Note this is a duplicate of the same method in the Quart class.

async open_resource(path: bytes | str | PathLike, mode: str = 'rb') AiofilesContextManager[None, None, AsyncBufferedReader]#

Open a file for reading.

Use as

async with await app.open_resource(path) as file_:
    await file_.read()
async send_static_file(filename: str) Response#
teardown_app_websocket(func: T_teardown) 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():
    ...
websocket(rule: str, **options: Any) Callable[[T_websocket], T_websocket]#

Add a websocket to the application.

This is designed to be used as a decorator, if used to decorate a synchronous function, the function will be wrapped in run_sync() and run in a thread executor (with the wrapped function returned). An example usage,

@app.websocket('/')
async def websocket_route():
    ...
Parameters:
  • rule – The path to route on, should start with a /.

  • endpoint – Optional endpoint name, if not present the function name is used.

  • defaults

    A dictionary of variables to provide automatically, use to provide a simpler default path for a route, e.g. to allow for /book rather than /book/0,

    @app.websocket('/book', defaults={'page': 0})
    @app.websocket('/book/<int:page>')
    def book(page):
        ...
    

  • host – The full host name for this route (should include subdomain if needed) - cannot be used with subdomain.

  • subdomain – A subdomain for this specific route.

  • strict_slashes – Strictly match the trailing slash present in the path. Will redirect a leaf (no slash) to a branch (with slash).

while_app_serving(func: T_while_serving) 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