mirror of https://github.com/tiangolo/fastapi.git
🔀 Include docs and plugins from master
This commit is contained in:
commit
8eaae72b76
|
|
@ -14,20 +14,21 @@ results = await some_library()
|
|||
|
||||
Then, declare your endpoint functions with `async def` like:
|
||||
|
||||
```Python
|
||||
```Python hl_lines="2"
|
||||
@app.get('/')
|
||||
async def read_results():
|
||||
results = await some_library()
|
||||
return results
|
||||
```
|
||||
|
||||
**Note**: You can only use `await` inside of functions created with `async def`.
|
||||
!!! note
|
||||
You can only use `await` inside of functions created with `async def`.
|
||||
|
||||
---
|
||||
|
||||
If you are using a third party library that communicates with something (a database, an API, the file system, etc) and doesn't have support for using `await`, (this is currently the case for most database libraries), then declare your endpoint functions as normally, with just `def`, like:
|
||||
|
||||
```Python
|
||||
```Python hl_lines="2"
|
||||
@app.get('/')
|
||||
def results():
|
||||
results = some_library()
|
||||
|
|
@ -283,7 +284,7 @@ The key here is the `await`. It tells Python that it has to wait for `get_burger
|
|||
|
||||
For `await` to work, it has to be inside a function that supports this asynchronicity. To do that, you just declare it with `async def`:
|
||||
|
||||
```Python
|
||||
```Python hl_lines="1"
|
||||
async def get_burgers(number: int):
|
||||
# Do some asynchronous stuff to create the burgers
|
||||
return burgers
|
||||
|
|
@ -291,7 +292,7 @@ async def get_burgers(number: int):
|
|||
|
||||
...instead of `def`:
|
||||
|
||||
```Python
|
||||
```Python hl_lines="2"
|
||||
# This is not asynchronous
|
||||
def get_sequential_burgers(number: int):
|
||||
# Do some sequential stuff to create the burgers
|
||||
|
|
@ -311,7 +312,7 @@ burgers = get_burgers(2)
|
|||
|
||||
So, if you are using a library that tells you that you can call it with `await`, you need to create the endpoint that uses it with `async def`, like in:
|
||||
|
||||
```Python
|
||||
```Python hl_lines="2 3"
|
||||
@app.get('/burgers')
|
||||
async def read_burgers():
|
||||
burgers = await get_burgers(2)
|
||||
|
|
@ -360,4 +361,4 @@ Let's see the same phrase from above:
|
|||
|
||||
That should make more sense now.
|
||||
|
||||
All that is what powers FastAPI (through Starlette) and what makes it so powerful and have an impressive performance.
|
||||
All that is what powers FastAPI (through Starlette) and what makes it have such an impressive performance.
|
||||
|
|
|
|||
|
|
@ -202,3 +202,4 @@ With **FastAPI** you get all of **Pydantic**'s features (as FastAPI is based on
|
|||
* You can have deeply **nested JSON** objects and have them all validated and annotated.
|
||||
* **Extendible**:
|
||||
* Pydantic allows custom data types to be defined or you can extend validation with methods on a model decorated with the validator decorator.
|
||||
* 100% test coverage.
|
||||
|
|
@ -27,12 +27,17 @@ FastAPI is a modern, fast (high-performance), web framework for building APIs wi
|
|||
The key features are:
|
||||
|
||||
* **Fast**: Very high performance, on par with **NodeJS** and **Go** (thanks to Starlette and Pydantic).
|
||||
|
||||
* **Fast to code**: Increase the speed to develop features by about 200% to 300% *.
|
||||
* **Less bugs**: Reduce about 40% of human (developer) induced errors. *
|
||||
* **Intuitive**: Great editor support. <abbr title="also known as auto-complete, autocompletion, IntelliSense">Completion</abbr> everywhere. Less time debugging.
|
||||
* **Easy**: Designed to be easy to use and learn. Less time reading docs.
|
||||
* **Short**: Minimize code duplication. Multiple features from each parameter declaration.
|
||||
* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Less bugs.
|
||||
* **Robust**: Get production-ready code. With automatic interactive documentation.
|
||||
* **Standards-based**: Based on (and fully compatible with) the open standards for APIs: <a href="https://github.com/OAI/OpenAPI-Specification" target="_blank">OpenAPI</a> and <a href="http://json-schema.org/" target="_blank">JSON Schema</a>.
|
||||
|
||||
<small>* estimation based on tests on an internal development team, building production applications.</small>
|
||||
|
||||
|
||||
## Requirements
|
||||
|
||||
|
|
@ -65,11 +70,27 @@ from fastapi import FastAPI
|
|||
|
||||
app = FastAPI()
|
||||
|
||||
@app.get('/')
|
||||
def read_root():
|
||||
return {'hello': 'world'}
|
||||
```
|
||||
|
||||
Or if your code uses `async` / `await`, use `async def`:
|
||||
|
||||
```Python hl_lines="6"
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.get('/')
|
||||
async def read_root():
|
||||
return {'hello': 'world'}
|
||||
```
|
||||
|
||||
!!! note
|
||||
If you don't know, check the section about [`async` and `await` in the docs](async.md).
|
||||
|
||||
|
||||
* Run the server with:
|
||||
|
||||
```bash
|
||||
|
|
@ -118,7 +139,7 @@ Now modify the file `main.py` to include:
|
|||
* an optional query parameter `q`.
|
||||
|
||||
|
||||
```Python
|
||||
```Python hl_lines="2 7 8 9 10 19"
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
|
@ -287,6 +308,9 @@ Used by Starlette:
|
|||
* <a href="https://graphene-python.org/" target="_blank"><code>graphene</code></a> - Required for `GraphQLApp` support.
|
||||
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
|
||||
|
||||
Used by FastAPI / Starlette:
|
||||
|
||||
* <a href="http://www.uvicorn.org" target="_blank"><code>uvicorn</code></a> - for the server that loads and serves your application.
|
||||
|
||||
You can install all of these with `pip3 install fastapi[full]`.
|
||||
|
||||
|
|
|
|||
|
|
@ -26,3 +26,5 @@ markdown_extensions:
|
|||
guess_lang: false
|
||||
- markdown_include.include:
|
||||
base_path: docs
|
||||
- admonition
|
||||
- codehilite
|
||||
|
|
|
|||
Loading…
Reference in New Issue