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.

json_decoder

The decoder to use for JSON data, if not set will default to the app’s decoder.

Type

Optional[Type[json.decoder.JSONDecoder]]

json_encoder

The encoder to use for JSON data, if not set will default to the app’s encoder.

Type

Optional[Type[json.encoder.JSONEncoder]]

add_url_rule(rule: str, endpoint: Optional[str] = None, view_func: Optional[Callable] = 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[Callable] = 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: AfterRequestCallable) AfterRequestCallable

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: AfterWebsocketCallable) AfterWebsocketCallable

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: BeforeRequestCallable) BeforeRequestCallable

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: BeforeWebsocketCallable) BeforeWebsocketCallable

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: Union[Callable[[], Dict[str, Any]], Callable[[], Awaitable[Dict[str, Any]]]]) Union[Callable[[], Dict[str, Any]], Callable[[], Awaitable[Dict[str, Any]]]]

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

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

endpoint(endpoint: str) Callable

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[[ErrorHandlerCallable], ErrorHandlerCallable]

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

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]
json_decoder: Optional[Type[JSONDecoder]] = None
json_encoder: Optional[Type[JSONEncoder]] = None
name: str
open_resource(path: Union[bytes, str, os.PathLike], mode: str = 'rb') IO

Open a file for reading.

Use as

with app.open_resource(path) as file_:
    file_.read()
patch(rule: str, **options: Any) Callable

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

post(rule: str, **options: Any) Callable

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

put(rule: str, **options: Any) Callable

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

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: Union[Callable[[Optional[BaseException]], None], Callable[[Optional[BaseException]], Awaitable[None]]]) Union[Callable[[Optional[BaseException]], None], Callable[[Optional[BaseException]], Awaitable[None]]]

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: Union[Callable[[Optional[BaseException]], None], Callable[[Optional[BaseException]], Awaitable[None]]]) Union[Callable[[Optional[BaseException]], None], Callable[[Optional[BaseException]], Awaitable[None]]]

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: Callable[[str, dict], None]) Callable[[str, dict], None]

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

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

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