Add source examples for Python 3.10 and 3.9 with updated syntax (#842)

Co-authored-by: Esteban Maya Cadavid <emayacadavid9@gmail.com>
This commit is contained in:
Sebastián Ramírez
2024-03-21 17:49:38 -05:00
committed by GitHub
parent 4c3f242ae2
commit 9141c8a920
39 changed files with 7456 additions and 25 deletions

View File

@@ -32,6 +32,22 @@ We will start with the **simplest version**, with just heroes (no teams yet).
This is almost the same code we have seen up to now in previous examples:
//// tab | Python 3.10+
```Python hl_lines="18-19"
# One line of FastAPI imports here later 👈
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:2]!}
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:5-20]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="20-21"
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:1]!}
@@ -43,12 +59,26 @@ This is almost the same code we have seen up to now in previous examples:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!}
```
////
///
There's only one change here from the code we have used before, the `check_same_thread` in the `connect_args`.
@@ -77,6 +107,22 @@ We will import the `FastAPI` class from `fastapi`.
And then create an `app` object that is an instance of that `FastAPI` class:
//// tab | Python 3.10+
```Python hl_lines="1 6"
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:1-2]!}
# SQLModel code here omitted 👈
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:23]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3 8"
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:1-4]!}
@@ -87,12 +133,26 @@ And then create an `app` object that is an instance of that `FastAPI` class:
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!}
```
////
///
## Create Database and Tables on `startup`
@@ -101,6 +161,20 @@ We want to make sure that once the app starts running, the function `create_tabl
This should be called only once at startup, not before every request, so we put it in the function to handle the `"startup"` event:
//// tab | Python 3.10+
```Python hl_lines="6-8"
# Code above omitted 👆
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:23-28]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="6-8"
# Code above omitted 👆
@@ -109,12 +183,26 @@ This should be called only once at startup, not before every request, so we put
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!}
```
////
///
## Create Heroes *Path Operation*
@@ -129,6 +217,20 @@ Let's create the **path operation** code to create a new hero.
It will be called when a user sends a request with a `POST` **operation** to the `/heroes/` **path**:
//// tab | Python 3.10+
```Python hl_lines="11-12"
# Code above omitted 👆
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:23-37]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11-12"
# Code above omitted 👆
@@ -137,12 +239,26 @@ It will be called when a user sends a request with a `POST` **operation** to the
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!}
```
////
///
/// info
@@ -177,18 +293,44 @@ We will improve this further later, but for now, it already shows the power of h
Now let's add another **path operation** to read all the heroes:
//// tab | Python 3.10+
```Python hl_lines="20-24"
# Code above omitted 👆
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py[ln:23-44]!}
```
////
//// tab | Python 3.7+
```Python hl_lines="20-24"
# Code above omitted 👆
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py[ln:25-46]!}
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py!}
```
////
///
This is pretty straightforward.