Debian Codegraph @

docs/tutorials/video_tutorial.rst

raw
1 .. _video_tutorial:
2
3 Tutorial: Serving video
4 =======================
5
6 In this tutorial we will build a very basic video server. It will
7 serve a video directly.
8
9 This tutorial is meant to serve as an introduction to serving large
10 files with conditional responses in Quart. If you want to skip to the
11 end the code is on `Github
12 <https://github.com/pallets/quart/tree/main/examples/video>`_.
13
14 1: Creating the project
15 -----------------------
16
17 We need to create a project for our video server, I like to use
18 `Poetry <https://python-poetry.org>`_ to do this. Poetry is installed
19 via pip (or via `Brew <https://brew.sh/>`_):
20
21 .. code-block:: console
22
23 pip install poetry
24
25 We can then use Poetry to create a new video project:
26
27 .. code-block:: console
28
29 poetry new --src video
30
31 Our project can now be developed in the *video* directory, and all
32 subsequent commands should be in run the *video* directory.
33
34 2: Adding the dependencies
35 --------------------------
36
37 We only need Quart to build this simple video server, which we can
38 install as a dependency of the project by running the following:
39
40 .. code-block:: console
41
42 poetry add quart
43
44 Poetry will ensure that this dependency is present and the paths are
45 correct by running:
46
47 .. code-block:: console
48
49 poetry install
50
51 3: Creating the app
52 -------------------
53
54 We need a Quart app to be our web server, which is created by the
55 following addition to *src/video/__init__.py*:
56
57 .. code-block:: python
58 :caption: src/video/__init__.py
59
60 from quart import Quart
61
62 app = Quart(__name__)
63
64 def run() -> None:
65 app.run()
66
67 To make the app easy to run we can call the run method from a poetry
68 script, by adding the following to *pyproject.toml*:
69
70 .. code-block:: toml
71 :caption: pyproject.toml
72
73 [tool.poetry.scripts]
74 start = "video:run"
75
76 Which allows the following command to start the app:
77
78 .. code-block:: console
79
80 poetry run start
81
82 4: Serving the UI
83 -----------------
84
85 When users visit our website we will show them the same video served
86 directly, and via chunks. The following HTML template should be added
87 to *src/video/templates/index.html*:
88
89 .. code-block:: html
90 :caption: src/video/templates/index.html
91
92 <video controls width="100%">
93 <source src="/video.mp4" type="video/mp4">
94 </video>
95
96 This is a very basic UI in terms of the styling.
97
98 We can now serve this template for the root path i.e. ``/`` by adding
99 the following to *src/video/__init__.py*:
100
101 .. code-block:: python
102
103 from quart import render_template
104
105 @app.get("/")
106 async def index():
107 return await render_template("index.html")
108
109 5: Implementing the routes
110 --------------------------
111
112 As we are serving a large file we should allow for conditional
113 responses. This is where the data returned in the response is
114 conditional on what the request asked for. This is done via the
115 ``Range`` header field which can be inspected via the
116 ``request.range`` attribute.
117
118 Quart has in-built methods to make a response conditional on the
119 request range. The first is to use the conditional argument when
120 sending a file, the second is to use the response ``make_conditional``
121 method. The former is shown below, which should be added to
122 *src/video/__init__.py*:
123
124 .. code-block:: python
125 :caption: src/video/__init__.py
126
127 @app.route("/video.mp4")
128 async def auto_video():
129 return await send_file(app.static_folder / "video.mp4", conditional=True)
130
131 6: Testing
132 ----------
133
134 To test our app we need to check that the full video is returned
135 unless a conditional range request is made. This is done by adding the
136 following to *tests/test_video.py*:
137
138 .. code-block:: python
139 :caption: tests/test_video.py
140
141 from video import app
142
143 async def test_auto_video() -> None:
144 test_client = app.test_client()
145 response = await test_client.get("/video.mp4")
146 data = await response.get_data()
147 assert len(data) == 255_849
148
149 response = await test_client.get("/video.mp4", headers={"Range": "bytes=200-1000"})
150 data = await response.get_data()
151 assert len(data) == 801
152
153 As the test is an async function we need to install `pytest-asyncio
154 <https://github.com/pytest-dev/pytest-asyncio>`_ by running the
155 following:
156
157 .. code-block:: console
158
159 poetry add --dev pytest-asyncio
160
161 Once installed it needs to be configured by adding the following to
162 *pyproject.toml*:
163
164 .. code-block:: toml
165
166 [tool.pytest.ini_options]
167 asyncio_mode = "auto"
168
169 Finally we can run the tests via this command:
170
171 .. code-block:: console
172
173 poetry run pytest tests/
174
175 If you are running this in the Quart example folder you'll need to add
176 a ``-c pyproject.toml`` option to prevent pytest from using the Quart
177 pytest configuration.
178
179 7: Summary
180 ----------
181
182 We've built a server that will serve large files conditionally as
183 requested by the client, including the ability to limit the maximum
184 partial size.