quart.scaffold module

class quart.scaffold.Scaffold(import_name: str, static_folder: Optional[str] = None, static_url_path: Optional[str] = None, template_folder: Optional[str] = None, root_path: Optional[str] = None)

Bases: object

Base class for Quart and Blueprint classes.

add_url_rule(rule: str, endpoint: Optional[str] = None, view_func: Optional[RouteCallable] = 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 application.

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

def route():
    ...

app.add_url_rule('/', 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.

  • provide_automatic_options – Optionally False to prevent OPTION handling.

  • methods – List of HTTP verbs the function routes.

  • 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.route('/book', defaults={'page': 0})
    @app.route('/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).

  • is_websocket – Whether or not the view_func is a websocket.

  • merge_slashes – Merge consecutive slashes to a single slash (unless as part of the path variable).

add_websocket(rule: str, endpoint: Optional[str] = None, view_func: Optional[WebsocketCallable] = None, defaults: Optional[dict] = None, host: Optional[str] = None, subdomain: Optional[str] = None, *, strict_slashes: Optional[bool] = None) 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_request(func: quart.scaffold.T_after_request) quart.scaffold.T_after_request

Add an after request 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_request
async def func(response):
    return response
Parameters

func – The after request function itself.

after_websocket(func: quart.scaffold.T_after_websocket) quart.scaffold.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_request(func: quart.scaffold.T_before_request) quart.scaffold.T_before_request

Add a before request 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_request
async def func():
    ...
Parameters

func – The before request function itself.

before_websocket(func: quart.scaffold.T_before_websocket) quart.scaffold.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.

context_processor(func: quart.scaffold.T_template_context_processor) quart.scaffold.T_template_context_processor

Add a template context processor.

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.context_processor
async def update_context(context):
    return context
delete(rule: str, **options: Any) Callable[[quart.scaffold.T_route], quart.scaffold.T_route]

Syntactic sugar for route() with methods=["DELETE"].

endpoint(endpoint: str) Callable[[quart.scaffold.F], quart.scaffold.F]

Register a function as an endpoint.

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.endpoint('name')
async def endpoint():
    ...
Parameters

endpoint – The endpoint name to use.

errorhandler(error: Union[Type[Exception], int]) Callable[[quart.scaffold.T_error_handler], quart.scaffold.T_error_handler]

Register a function as an error handler.

This is designed to be used as a decorator. An example usage,

@app.errorhandler(500)
def error_handler():
    return "Error", 500
Parameters

error – The error code or Exception to handle.

get(rule: str, **options: Any) Callable[[quart.scaffold.T_route], quart.scaffold.T_route]

Syntactic sugar for route() with methods=["GET"].

get_send_file_max_age(filename: str) Optional[int]
property has_static_folder: bool
property jinja_loader: Optional[jinja2.loaders.FileSystemLoader]
name: str
async open_resource(path: Union[bytes, str, os.PathLike], mode: str = 'rb') aiofiles.base.AiofilesContextManager[None, None, aiofiles.threadpool.binary.AsyncBufferedReader]

Open a file for reading.

Use as

async with await app.open_resource(path) as file_:
    await file_.read()
patch(rule: str, **options: Any) Callable[[quart.scaffold.T_route], quart.scaffold.T_route]

Syntactic sugar for route() with methods=["PATCH"].

post(rule: str, **options: Any) Callable[[quart.scaffold.T_route], quart.scaffold.T_route]

Syntactic sugar for route() with methods=["POST"].

put(rule: str, **options: Any) Callable[[quart.scaffold.T_route], quart.scaffold.T_route]

Syntactic sugar for route() with methods=["PUT"].

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

Register a function as an error handler.

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

def error_handler():
    return "Error", 500

app.register_error_handler(500, error_handler)
Parameters
  • error – The error code or Exception to handle.

  • func – The function to handle the error.

route(rule: 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: Optional[bool] = None) Callable[[quart.scaffold.T_route], quart.scaffold.T_route]

Add a HTTP request handling route.

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.route('/')
async def route():
    ...
Parameters
  • rule – The path to route on, should start with a /.

  • methods – List of HTTP verbs the function routes.

  • 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.route('/book', defaults={'page': 0})
    @app.route('/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.

  • provide_automatic_options – Optionally False to prevent OPTION handling.

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

async send_static_file(filename: str) Response
property static_folder: Optional[pathlib.Path]
property static_url_path: Optional[str]
teardown_request(func: quart.scaffold.T_teardown) quart.scaffold.T_teardown

Add a teardown request 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.teardown_request
async def func():
    ...
Parameters

func – The teardown request function itself.

teardown_websocket(func: quart.scaffold.T_teardown) quart.scaffold.T_teardown

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,

@app.teardown_websocket
async def func():
    ...
Parameters
  • func – The teardown websocket function itself.

  • name – Optional blueprint key name.

url_defaults(func: quart.scaffold.T_url_defaults) quart.scaffold.T_url_defaults

Add a url default preprocessor.

This is designed to be used as a decorator. An example usage,

@app.url_defaults
def default(endpoint, values):
    ...
url_value_preprocessor(func: quart.scaffold.T_url_value_preprocessor) quart.scaffold.T_url_value_preprocessor

Add a url value preprocessor.

This is designed to be used as a decorator. An example usage,

@app.url_value_preprocessor
def value_preprocessor(endpoint, view_args):
    ...
websocket(rule: str, endpoint: Optional[str] = None, defaults: Optional[dict] = None, host: Optional[str] = None, subdomain: Optional[str] = None, *, strict_slashes: Optional[bool] = None) Callable[[quart.scaffold.T_websocket], quart.scaffold.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).

quart.scaffold.setupmethod(func: quart.scaffold.F) quart.scaffold.F