mirror of
https://github.com/fastapi/fastapi.git
synced 2026-10-22 01:45:21 -04:00
🎨 Update terminal examples and Typer note (#1139)
* 🎨 Update terminal examples with Termynal * 🍱 Add Termynal scripts and styles from Typer for terminal examples
This commit is contained in:
1 parent
025b38df40
commit
faf88cea0b
22 files changed
+801
-77
No files matched your search
@@ -292,10 +292,16 @@ The end result is that the item paths are now:
|
||||
|
||||
Now, run `uvicorn`, using the module `app.main` and the variable `app`:
|
||||
|
||||
```bash
|
||||
uvicorn app.main:app --reload
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uvicorn app.main:app --reload
|
||||
|
||||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
And open the docs at <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
|
||||
|
||||
You will see the automatic API docs, including the paths from all the submodules, using the correct paths (and prefixes) and the correct tags:
|
||||
|
||||
@@ -12,10 +12,14 @@ In your FastAPI application, import and run `uvicorn` directly:
|
||||
|
||||
The main purpose of the `__name__ == "__main__"` is to have some code that is executed when your file is called with:
|
||||
|
||||
```bash
|
||||
python myapp.py
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ python myapp.py
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
but is not called when another file imports it, like in:
|
||||
|
||||
```Python
|
||||
@@ -28,10 +32,14 @@ Let's say your file is named `myapp.py`.
|
||||
|
||||
If you run it with:
|
||||
|
||||
```bash
|
||||
python myapp.py
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ python myapp.py
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
then the internal variable `__name__` in your file, created automatically by Python, will have as value the string `"__main__"`.
|
||||
|
||||
So, the section:
|
||||
|
||||
@@ -8,10 +8,20 @@ Copy that to a file `main.py`.
|
||||
|
||||
Run the live server:
|
||||
|
||||
```bash
|
||||
uvicorn main:app --reload
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uvicorn main:app --reload
|
||||
|
||||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
<span style="color: green;">INFO</span>: Started reloader process [28720]
|
||||
<span style="color: green;">INFO</span>: Started server process [28722]
|
||||
<span style="color: green;">INFO</span>: Waiting for application startup.
|
||||
<span style="color: green;">INFO</span>: Application startup complete.
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
!!! note
|
||||
The command `uvicorn main:app` refers to:
|
||||
|
||||
@@ -19,16 +29,13 @@ uvicorn main:app --reload
|
||||
* `app`: the object created inside of `main.py` with the line `app = FastAPI()`.
|
||||
* `--reload`: make the server restart after code changes. Only use for development.
|
||||
|
||||
You will see an output like:
|
||||
In the output, there's a line with something like:
|
||||
|
||||
```hl_lines="4"
|
||||
INFO: Started reloader process [17961]
|
||||
INFO: Started server process [17962]
|
||||
INFO: Waiting for application startup.
|
||||
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
```
|
||||
|
||||
That last line shows the URL where your app is being served, in your local machine.
|
||||
That line shows the URL where your app is being served, in your local machine.
|
||||
|
||||
### Check it
|
||||
|
||||
@@ -144,10 +151,16 @@ This will be the main point of interaction to create all your API.
|
||||
|
||||
This `app` is the same one referred by `uvicorn` in the command:
|
||||
|
||||
```bash
|
||||
uvicorn main:app --reload
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uvicorn main:app --reload
|
||||
|
||||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
If you create your app like:
|
||||
|
||||
```Python hl_lines="3"
|
||||
@@ -156,10 +169,16 @@ If you create your app like:
|
||||
|
||||
And put it in a file `main.py`, then you would call `uvicorn` like:
|
||||
|
||||
```bash
|
||||
uvicorn main:my_awesome_api --reload
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uvicorn main:my_awesome_api --reload
|
||||
|
||||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Step 3: create a *path operation*
|
||||
|
||||
#### Path
|
||||
|
||||
+20
-4
@@ -12,10 +12,20 @@ All the code blocks can be copied and used directly (they are actually tested Py
|
||||
|
||||
To run any of the examples, copy the code to a file `main.py`, and start `uvicorn` with:
|
||||
|
||||
```bash
|
||||
uvicorn main:app --reload
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uvicorn main:app --reload
|
||||
|
||||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
<span style="color: green;">INFO</span>: Started reloader process [28720]
|
||||
<span style="color: green;">INFO</span>: Started server process [28722]
|
||||
<span style="color: green;">INFO</span>: Waiting for application startup.
|
||||
<span style="color: green;">INFO</span>: Application startup complete.
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
It is **HIGHLY encouraged** that you write or copy the code, edit it and run it locally.
|
||||
|
||||
Using it in your editor is what really shows you the benefits of FastAPI, seeing how little code you have to write, all the type checks, autocompletion, etc.
|
||||
@@ -28,10 +38,16 @@ The first step is to install FastAPI.
|
||||
|
||||
For the tutorial, you might want to install it with all the optional dependencies and features:
|
||||
|
||||
```bash
|
||||
pip install fastapi[all]
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install fastapi[all]
|
||||
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
...that also includes `uvicorn`, that you can use as the server that runs your code.
|
||||
|
||||
!!! note
|
||||
|
||||
@@ -33,10 +33,16 @@ Copy the example in a file `main.py`:
|
||||
|
||||
Run the example with:
|
||||
|
||||
```bash
|
||||
uvicorn main:app --reload
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uvicorn main:app --reload
|
||||
|
||||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## Check it
|
||||
|
||||
Go to the interactive docs at: <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
|
||||
|
||||
@@ -28,10 +28,16 @@ If you want to play with JWT tokens and see how they work, check <a href="https:
|
||||
|
||||
We need to install `PyJWT` to generate and verify the JWT tokens in Python:
|
||||
|
||||
```bash
|
||||
pip install pyjwt
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install pyjwt
|
||||
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## Password hashing
|
||||
|
||||
"Hashing" means converting some content (a password in this case) into a sequence of bytes (just a string) that looks like gibberish.
|
||||
@@ -56,10 +62,16 @@ The recommended algorithm is "Bcrypt".
|
||||
|
||||
So, install PassLib with Bcrypt:
|
||||
|
||||
```bash
|
||||
pip install passlib[bcrypt]
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install passlib[bcrypt]
|
||||
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
!!! tip
|
||||
With `passlib`, you could even configure it to be able to read passwords created by **Django**, a **Flask** security plug-in or many others.
|
||||
|
||||
@@ -101,10 +113,16 @@ Create a random secret key that will be used to sign the JWT tokens.
|
||||
|
||||
To generate a secure random secret key use the command:
|
||||
|
||||
```bash
|
||||
openssl rand -hex 32
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ openssl rand -hex 32
|
||||
|
||||
09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
And copy the output to the variable `SECRET_KEY` (don't use the one in the example).
|
||||
|
||||
Create a variable `ALGORITHM` with the algorithm used to sign the JWT token and set it to `"HS256"`.
|
||||
|
||||
@@ -440,8 +440,8 @@ A "migration" is the set of steps needed whenever you change the structure of yo
|
||||
!!! info
|
||||
For this to work, you need to use **Python 3.7** or above, or in **Python 3.6**, install the "backports":
|
||||
|
||||
```bash
|
||||
pip install async-exit-stack async-generator
|
||||
```console
|
||||
$ pip install async-exit-stack async-generator
|
||||
```
|
||||
|
||||
This installs <a href="https://github.com/sorcio/async_exit_stack" class="external-link" target="_blank">async-exit-stack</a> and <a href="https://github.com/python-trio/async_generator" class="external-link" target="_blank">async-generator</a>.
|
||||
@@ -596,10 +596,17 @@ You can copy this code and use it as is.
|
||||
|
||||
Then you can run it with Uvicorn:
|
||||
|
||||
```bash
|
||||
uvicorn sql_app.main:app --reload
|
||||
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ uvicorn sql_app.main:app --reload
|
||||
|
||||
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
And then, you can open your browser at <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
|
||||
|
||||
And you will be able to interact with your **FastAPI** application, reading data from a real database:
|
||||
|
||||
@@ -4,10 +4,16 @@ You can serve static files automatically from a directory using `StaticFiles`.
|
||||
|
||||
First you need to install `aiofiles`:
|
||||
|
||||
```bash
|
||||
pip install aiofiles
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install aiofiles
|
||||
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## Use `StaticFiles`
|
||||
|
||||
* Import `StaticFiles`.
|
||||
|
||||
@@ -103,14 +103,36 @@ For more information about how to pass data to the backend (using `requests` or
|
||||
|
||||
After that, you just need to install `pytest`:
|
||||
|
||||
```bash
|
||||
pip install pytest
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pip install pytest
|
||||
|
||||
---> 100%
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
It will detect the files and tests automatically, execute them, and report the results back to you.
|
||||
|
||||
Run the tests with:
|
||||
|
||||
```bash
|
||||
pytest
|
||||
<div class="termy">
|
||||
|
||||
```console
|
||||
$ pytest
|
||||
|
||||
================ test session starts ================
|
||||
platform linux -- Python 3.6.9, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
|
||||
rootdir: /home/user/code/superawesome-cli/app
|
||||
plugins: forked-1.1.3, xdist-1.31.0, cov-2.8.1
|
||||
collected 6 items
|
||||
|
||||
---> 100%
|
||||
|
||||
test_main.py <span style="color: green; white-space: pre;">...... [100%]</span>
|
||||
|
||||
<span style="color: green;">================= 1 passed in 0.03s =================</span>
|
||||
```
|
||||
|
||||
</div>
|
||||
Reference in new issue
Block a user