File: response_values.rst

package info (click to toggle)
quart 0.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,888 kB
  • sloc: python: 8,644; makefile: 42; sh: 17; sql: 6
file content (121 lines) | stat: -rw-r--r-- 2,641 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
.. _response_values:

Response Return Values
======================

Response functions can return a number of different types as the
response value which will trigger different responses to the
client. The possible direct returns are,

Response Values
---------------

str
^^^

.. code-block:: python

    return "Hello"
    return await render_template("index.html")

A solitary string return indicates that you intend to return a string
mimetype ``text/html``. The string will be encoded using the default
:attr:`~quart.wrappers._BaseRequestResponse.charset`.

dict
^^^^

.. code-block:: python

    return {"a": "b"}

A solitary dict return indicates that you intend to return json,
``application/json``. The jsonify function will be used to encode the
dictionary.

list
^^^^

.. code-block:: python

    return ["a", "b"]

A solitary list return indicates that you intend to return json,
``application/json``. The jsonify function will be used to encode the
list.

Response
^^^^^^^^

.. code-block:: python

    @app.route('/')
    async def route_func():
        return Response("Hello")

Returning a Response instance indicates that you know exactly what you
wish to return.

AsyncGenerator[bytes, None]
^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

    @app.route('/')
    async def route_func():

        async def agen():
            data = await something
            yield data

        return agen()

Returning an async generator allows for the response to be streamed to
the client, thereby lowing the peak memory usage, if combined with a
``Transfer-Encoding`` header with value ``chunked``.

Generator[bytes, None, None]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

    @app.route('/')
    async def route_func():

        def gen():
            yield data

        return gen()

Returning an generator allows for the response to be streamed to the
client, thereby lowing the peak memory usage, if combined with a
``Transfer-Encoding`` header with value ``chunked``.

Combinations
------------

Any of the above Response Values can be combined, as described,

Tuple[ResponseValue, int]
^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

    @app.route('/')
    async def route_func():
        return "Hello", 200

A tuple of a Response Value and a integer indicates that you intend to
specify the status code.

Tuple[str, int, Dict[str, str]]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

    @app.route('/')
    async def route_func():
        return "Hello", 200, {'X-Header': 'Value'}

A tuple of a Response Value, integer and dictionary indicates that you intend
to specify additional headers.