| 1 |
Recap and assembling the final application |
| 2 |
=========================================== |
| 3 |
|
| 4 |
So far we have looked at the different parts of the application in isolation, but now |
| 5 |
it's time to put them all together and assemble a complete application. |
| 6 |
|
| 7 |
|
| 8 |
Final application |
| 9 |
----------------- |
| 10 |
|
| 11 |
.. literalinclude:: /examples/todo_app/full_app.py |
| 12 |
:language: python |
| 13 |
:caption: ``app.py`` |
| 14 |
:linenos: |
| 15 |
|
| 16 |
|
| 17 |
Recap |
| 18 |
----- |
| 19 |
|
| 20 |
|
| 21 |
.. literalinclude:: /examples/todo_app/full_app.py |
| 22 |
:language: python |
| 23 |
:caption: ``app.py`` |
| 24 |
:lines: 29-33 |
| 25 |
:lineno-start: 29 |
| 26 |
|
| 27 |
|
| 28 |
A route handler set up with ``get("/")`` responds to ``GET`` requests and returns a list |
| 29 |
of all items on our TODO list. The optional query parameter ``done`` allows filtering |
| 30 |
the items by status. It is declared as ``FromQuery[bool | None]``: the |
| 31 |
:data:`~.params.FromQuery` marker tells Litestar to read the value from the URL query |
| 32 |
string, the inner ``bool`` triggers conversion, and wrapping it in |
| 33 |
:class:`Optional <typing.Optional>` (combined with the ``= None`` default) makes it |
| 34 |
optional. |
| 35 |
|
| 36 |
|
| 37 |
.. literalinclude:: /examples/todo_app/full_app.py |
| 38 |
:language: python |
| 39 |
:caption: ``app.py`` |
| 40 |
:lines: 36-39 |
| 41 |
:lineno-start: 36 |
| 42 |
|
| 43 |
|
| 44 |
A route handler set up with ``post("/")`` responds to ``POST`` requests and adds an item |
| 45 |
to the TODO list. The data for the new item is received via the request data, which the |
| 46 |
route handler accesses by specifying the ``data`` parameter. The type annotation of |
| 47 |
``TodoItem`` means the request data will parsed as JSON, which is then used to create an |
| 48 |
instance of the ``TodoItem`` dataclass, which - finally - gets passed into the function. |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
.. literalinclude:: /examples/todo_app/full_app.py |
| 53 |
:language: python |
| 54 |
:caption: ``app.py`` |
| 55 |
:lines: 42-47 |
| 56 |
:lineno-start: 42 |
| 57 |
|
| 58 |
A route handler set up with ``put("/{item_title:str}")``, making use of a path parameter, |
| 59 |
responds to ``PUT`` requests on the path ``/some todo title``, where ``some todo title`` |
| 60 |
is the title of the ``TodoItem`` you wish to update. The handler declares |
| 61 |
``item_title: FromPath[str]`` to receive the captured value; the :data:`~.params.FromPath` |
| 62 |
marker tells Litestar this kwarg comes from the URL path, and the matching name |
| 63 |
(``item_title``) lines it up with the ``{item_title:str}`` slot. The ``:str`` suffix in the |
| 64 |
path pattern means the value is treated as a string. Additionally, this route handler |
| 65 |
receives data of a ``TodoItem`` the same way as the ``POST`` handler. |
| 66 |
|
| 67 |
|
| 68 |
.. literalinclude:: /examples/todo_app/full_app.py |
| 69 |
:language: python |
| 70 |
:caption: ``app.py`` |
| 71 |
:lines: 50 |
| 72 |
:lineno-start: 50 |
| 73 |
|
| 74 |
|
| 75 |
An instance of ``Litestar`` is created, including the previously defined route handlers. |
| 76 |
This app can now be served using an ASGI server like |
| 77 |
`uvicorn <https://www.uvicorn.org/>`_, which can be conveniently done using Litestar's |
| 78 |
CLI by executing the ``litestar run`` command. |
| 79 |
|
| 80 |
|
| 81 |
Next steps |
| 82 |
---------- |
| 83 |
|
| 84 |
This tutorial covered some of the fundamental concepts of Litestar. For a more in-depth |
| 85 |
explanation of these topics, refer to the :doc:`Usage Guide </usage/index>`. |