FastAPI framework, high performance, easy to learn, fast to code, ready for production
Documentation: https://fastapi.tiangolo.com
Source Code: https://github.com/fastapi/fastapi
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python based on standard Python type hints.
The key features are:
* estimation based on tests on an internal development team, building production applications.
$ pip install "fastapi[standard]"
---> 100%
main.py with:from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
async def...If your code uses async / await, use async def:
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
Note:
If you don't know, check the "In a hurry?" section about async and await in the docs.
$ fastapi dev main.py
╭────────── FastAPI CLI - Development mode ───────────╮
│ │
│ Serving at: http://127.0.0.1:8000 │
│ │
│ API docs: http://127.0.0.1:8000/docs │
│ │
│ Running in development mode, for production use: │
│ │
│ fastapi run │
│ │
╰─────────────────────────────────────────────────────╯
INFO: Will watch for changes in these directories: ['/home/user/code/awesomeapp']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [2248755] using WatchFiles
INFO: Started server process [2248757]
INFO: Waiting for application startup.
INFO: Application startup complete.
fastapi dev main.py...The command fastapi dev reads your main.py file, detects the FastAPI app in it, and starts a server using Uvicorn.
By default, fastapi dev will start with auto-reload enabled for local development.
You can read more about it in the FastAPI CLI docs.
{"item_id": 5, "q": "somequery"}
/ and /items/{item_id}.GET operations (also known as HTTP methods)./items/{item_id} has a path parameter item_id that should be an int./items/{item_id} has an optional str query parameter q.from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: Union[bool, None] = None
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_name": item.name, "item_id": item_id}
item_id: int
item: Item
str, int, float, bool, list, etc).datetime objects.UUID objects.item_id in the path for GET and PUT requests.item_id is of type int for GET and PUT requests.q (as in http://127.0.0.1:8000/items/foo?q=somequery) for GET requests.q parameter is declared with = None, it is optional.None it would be required (as is the body in the case with PUT).PUT requests to /items/{item_id}, read the body as JSON:name that should be a str.price that has to be a float.is_offer, that should be a bool, if present. return {"item_name": item.name, "item_id": item_id}
... "item_name": item.name ...
... "item_price": item.price ...
maximum_length or regex.pytest
standard Dependenciesemail-validator - for email validation.httpx - Required if you want to use the TestClient.jinja2 - Required if you want to use the default template configuration.python-multipart - Required if you want to support form "parsing", with request.form().uvicorn - for the server that loads and serves your application. This includes uvicorn[standard], which includes some dependencies (e.g. uvloop) needed for high performance serving.fastapi-cli - to provide the fastapi command.standard Dependenciespydantic-settings - for settings management.pydantic-extra-types - for extra types to be used with Pydantic.orjson - Required if you want to use ORJSONResponse.ujson - Required if you want to use UJSONResponse.
© 2018 Sebastián Ramírez
Licensed under the MIT License.
https://fastapi.tiangolo.com/