Skip to content

13 - Quart

Quart is an ASGI re-implementation of the Flask API with some added ASGI specific features, such as websockets. If you have a Flask project and would like to go truly async, migrating to Quart is an option. Quart recently became a Pallets project (the folks behind Flask, Click, Jinja, etc) and they apparently intend to merge Quart and Flask to eventually have ASGI support in Flask.

TODO

Read more
The code
from typing import Any

from quart import Quart, request

app = Quart(__name__)


@app.post("/my-endpoint")
async def my_endpoint() -> dict[str, Any]:
    request_json = await request.get_json()
    return {"echoed data": request_json}


#######################################
# Let's test it
import pytest


@pytest.mark.asyncio
async def test_my_endpoint() -> None:
    client = app.test_client()

    response = await client.post("/my-endpoint", json={"foo": "bar"})

    assert response.status_code == 200
    response_data = await response.get_json()
    assert response_data == {"echoed data": {"foo": "bar"}}

tested with:

quart==0.17.0

pytest==7.1.1
pytest-asyncio==0.18.3

Run the server:

QUART_APP=quart_example:app quart run

Or just the test case:

pytest quart_example.py