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.
Read more
- Docs: https://quart.palletsprojects.com/en/latest/index.html
- GitHub repo: https://github.com/pallets/quart
- Quart became part of Pallets: https://palletsprojects.com/blog/quart-pallets/
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:
Run the server:
Or just the test case: