| 1 |
.. _cheatsheet: |
| 2 |
|
| 3 |
Cheatsheet |
| 4 |
========== |
| 5 |
|
| 6 |
Basic App |
| 7 |
--------- |
| 8 |
|
| 9 |
.. code-block:: python |
| 10 |
|
| 11 |
from quart import Quart |
| 12 |
|
| 13 |
app = Quart(__name__) |
| 14 |
|
| 15 |
@app.route("/hello") |
| 16 |
async def hello(): |
| 17 |
return "Hello, World!" |
| 18 |
|
| 19 |
if __name__ == "__main__": |
| 20 |
app.run(debug=True) |
| 21 |
|
| 22 |
Routing |
| 23 |
------- |
| 24 |
|
| 25 |
.. code-block:: python |
| 26 |
|
| 27 |
@app.route("/hello/<string:name>") # example.com/hello/quart |
| 28 |
async def hello(name): |
| 29 |
return f"Hello, {name}!" |
| 30 |
|
| 31 |
Request Methods |
| 32 |
--------------- |
| 33 |
|
| 34 |
.. code-block:: python |
| 35 |
|
| 36 |
@app.route("/get") # GET Only by default |
| 37 |
@app.route("/get", methods=["GET", "POST"]) # GET and POST |
| 38 |
@app.route("/get", methods=["DELETE"]) # Just DELETE |
| 39 |
|
| 40 |
JSON Responses |
| 41 |
-------------- |
| 42 |
|
| 43 |
.. code-block:: python |
| 44 |
|
| 45 |
@app.route("/hello") |
| 46 |
async def hello(): |
| 47 |
return {"Hello": "World!"} |
| 48 |
|
| 49 |
Template Rendering |
| 50 |
------------------ |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
from quart import render_template |
| 55 |
|
| 56 |
@app.route("/hello") |
| 57 |
async def hello(): |
| 58 |
return await render_template("index.html") # Required to be in templates/ |
| 59 |
|
| 60 |
Configuration |
| 61 |
------------- |
| 62 |
|
| 63 |
.. code-block:: python |
| 64 |
|
| 65 |
import json |
| 66 |
import tomllib |
| 67 |
|
| 68 |
app.config["VALUE"] = "something" |
| 69 |
|
| 70 |
app.config.from_file("filename.toml", tomllib.load) |
| 71 |
app.config.from_file("filename.json", json.load) |
| 72 |
|
| 73 |
Request |
| 74 |
------- |
| 75 |
|
| 76 |
.. code-block:: python |
| 77 |
|
| 78 |
from quart import request |
| 79 |
|
| 80 |
@app.route("/hello") |
| 81 |
async def hello(): |
| 82 |
request.method |
| 83 |
request.url |
| 84 |
request.headers["X-Bob"] |
| 85 |
request.args.get("a") # Query string e.g. example.com/hello?a=2 |
| 86 |
await request.get_data() # Full raw body |
| 87 |
(await request.form)["name"] |
| 88 |
(await request.get_json())["key"] |
| 89 |
request.cookies.get("name") |
| 90 |
|
| 91 |
WebSocket |
| 92 |
--------- |
| 93 |
|
| 94 |
.. code-block:: python |
| 95 |
|
| 96 |
from quart import websocket |
| 97 |
|
| 98 |
@app.websocket("/ws") |
| 99 |
async def ws(): |
| 100 |
websocket.headers |
| 101 |
while True: |
| 102 |
try: |
| 103 |
data = await websocket.receive() |
| 104 |
await websocket.send(f"Echo {data}") |
| 105 |
except asyncio.CancelledError: |
| 106 |
# Handle disconnect |
| 107 |
raise |
| 108 |
|
| 109 |
Cookies |
| 110 |
------- |
| 111 |
|
| 112 |
.. code-block:: python |
| 113 |
|
| 114 |
from quart import make_response |
| 115 |
|
| 116 |
@app.route("/hello") |
| 117 |
async def hello(): |
| 118 |
response = await make_response("Hello") |
| 119 |
response.set_cookie("name", "value") |
| 120 |
return response |
| 121 |
|
| 122 |
Abort |
| 123 |
----- |
| 124 |
|
| 125 |
.. code-block:: python |
| 126 |
|
| 127 |
from quart import abort |
| 128 |
|
| 129 |
@app.route("/hello") |
| 130 |
async def hello(): |
| 131 |
abort(409) |
| 132 |
|
| 133 |
|
| 134 |
HTTP/2 & HTTP/3 Server Push |
| 135 |
--------------------------- |
| 136 |
|
| 137 |
.. code-block:: python |
| 138 |
|
| 139 |
from quart import make_push_promise, url_for |
| 140 |
|
| 141 |
@app.route("/hello") |
| 142 |
async def hello(): |
| 143 |
await make_push_promise(url_for('static', filename='css/minimal.css')) |
| 144 |
... |