2
2
3
3
The simplest FastAPI file could look like this:
4
4
5
- ``` Python
6
- {!../ ../ docs_src/ first_steps/ tutorial001.py!}
7
- ```
5
+ {* ../../docs_src/first_steps/tutorial001.py * }
8
6
9
7
Copy that to a file ` main.py ` .
10
8
@@ -157,9 +155,7 @@ You could also use it to generate code automatically, for clients that communica
157
155
158
156
### Step 1: import `FastAPI`
159
157
160
- ```Python hl_lines="1"
161
- {!../../docs_src/first_steps/tutorial001.py! }
162
- ```
158
+ {* ../../docs_src/first_steps/tutorial001.py hl[1] * }
163
159
164
160
`FastAPI` is a Python class that provides all the functionality for your API.
165
161
@@ -173,9 +169,7 @@ You can use all the <a href="https://www.starlette.io/" class="external-link" ta
173
169
174
170
### Step 2: create a `FastAPI` "instance"
175
171
176
- ```Python hl_lines="3"
177
- {!../../docs_src/first_steps/tutorial001.py! }
178
- ```
172
+ {* ../../docs_src/first_steps/tutorial001.py hl[3] * }
179
173
180
174
Here the `app` variable will be an "instance" of the class `FastAPI`.
181
175
@@ -244,9 +238,7 @@ We are going to call them "**operations**" too.
244
238
245
239
#### Define a *path operation decorator*
246
240
247
- ```Python hl_lines="6"
248
- {!../../docs_src/first_steps/tutorial001.py! }
249
- ```
241
+ {* ../../docs_src/first_steps/tutorial001.py hl[6] * }
250
242
251
243
The `@app.get("/")` tells **FastAPI** that the function right below is in charge of handling requests that go to:
252
244
@@ -300,9 +292,7 @@ This is our "**path operation function**":
300
292
* **operation**: is `get`.
301
293
* **function**: is the function below the "decorator" (below `@app.get("/")`).
302
294
303
- ```Python hl_lines="7"
304
- {!../../docs_src/first_steps/tutorial001.py! }
305
- ```
295
+ {* ../../docs_src/first_steps/tutorial001.py hl[7] * }
306
296
307
297
This is a Python function.
308
298
@@ -314,9 +304,7 @@ In this case, it is an `async` function.
314
304
315
305
You could also define it as a normal function instead of `async def`:
316
306
317
- ```Python hl_lines="7"
318
- {!../../docs_src/first_steps/tutorial003.py! }
319
- ```
307
+ {* ../../docs_src/first_steps/tutorial003.py hl[7] * }
320
308
321
309
/// note
322
310
@@ -326,9 +314,7 @@ If you don't know the difference, check the [Async: *"In a hurry?"*](../async.md
326
314
327
315
### Step 5: return the content
328
316
329
- ```Python hl_lines="8"
330
- {!../../docs_src/first_steps/tutorial001.py! }
331
- ```
317
+ {* ../../docs_src/first_steps/tutorial001.py hl[8] * }
332
318
333
319
You can return a `dict`, `list`, singular values as `str`, `int`, etc.
334
320
0 commit comments