# quart.blueprints module

class quart.blueprints.Blueprint(*args, **kwargs)

  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.

  Parameters:

    - args (t.Any)

    - kwargs (t.Any)

  get_send_file_max_age(filename)

    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.

    Parameters:

      filename (str | None)

    Return type:

      int | None

  async send_static_file(filename)
    Parameters:

      filename (str)

    Return type:

      Response

  async open_resource(path, mode='rb')

    Open a file for reading.

    Use as

        async with await app.open_resource(path) as file_:
            await file_.read()

    Parameters:

      - path (bytes | str | PathLike)

      - mode (str)

    Return type:

      AiofilesContextManager

  websocket(rule, **options)

    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 (str) – 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).

      - options (Any)

    Return type:

      Callable[[T_websocket], T_websocket]

  add_websocket(rule, endpoint=None, view_func=None, **options)

    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 (str) – The path to route on, should start with a `/`.

      - endpoint (str | None) – Optional endpoint name, if not present the function name is used.

      - view_func (WebsocketCallable | None) – 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).

      - options (t.Any)

    Return type:

      None

  before_websocket(func)

    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 (T_before_websocket) – The before websocket function itself.

    Return type:

      T_before_websocket

  after_websocket(func)

    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 (T_after_websocket) – The after websocket function itself.

    Return type:

      T_after_websocket

  teardown_websocket(func)

    Add a teardown 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, .. code-block:: python

    > @app.teardown_websocket async def func():

    > > …

    Parameters:

      - func (T_teardown) – The teardown websocket function itself.

      - name – Optional blueprint key name.

    Return type:

      T_teardown

  before_app_websocket(func)

    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():
            ...

    Parameters:

      func (T_before_websocket)

    Return type:

      T_before_websocket

  before_app_serving(func)

    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():
            ...

    Parameters:

      func (T_before_serving)

    Return type:

      T_before_serving

  after_app_websocket(func)

    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():
            ...

    Parameters:

      func (T_after_websocket)

    Return type:

      T_after_websocket

  after_app_serving(func)

    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():
            ...

    Parameters:

      func (T_after_serving)

    Return type:

      T_after_serving

  while_app_serving(func)

    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

    Parameters:

      func (T_while_serving)

    Return type:

      T_while_serving

  teardown_app_websocket(func)

    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():
            ...

    Parameters:

      func (T_teardown)

    Return type:

      T_teardown