Skip to content

Multiple Files

Code

.
├── api
│   ├── __init__.py
│   ├── items.py
│   └── users.py
├── __init__.py
├── main.py
└── models.py
app/__init__.py

Tip

This is an empty file. 👆

app/main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from .api import items, users

app = FastAPI()

origins = [
    "http://localhost.tiangolo.com",
    "https://localhost.tiangolo.com",
    "http://localhost",
    "http://localhost:8080",
    "https://fastapiworkshop.yaquelinehoyos.com",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


app.include_router(users.router)
app.include_router(items.router)
app/models.py
from typing import Optional

from pydantic import BaseModel


class Item(BaseModel):
    title: str
    description: Optional[str] = None


class User(BaseModel):
    username: str
app/api/__init__.py

app/api/users.py
from fastapi import APIRouter

from ..models import User

router = APIRouter()


@router.post("/users/")
def create_user(user: User):
    return user


@router.get("/users/")
def read_users():
    return [{"username": "Rick"}, {"username": "Morty"}]


@router.get("/users/{user_id}")
def read_user(user_id: int):
    return {"id": user_id, "username": "Pickle Rick"}
app/api/items.py
from fastapi import APIRouter

from ..models import Item

router = APIRouter()


@router.post("/users/{user_id}/items/")
def create_item_for_user(user_id: int, item: Item):
    return item


@router.get("/items/")
def read_items():
    return [
        {"title": "Portal Gun", "description": "A gun that shoots portals"},
        {"title": "Towel"},
    ]

Server

Run the live server:

$ 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)
<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.

Note

The command uvicorn app.main:app --reload refers to:

  • app.main: the file app/main.py (the Python "module"). Inside of the Python package (directory) app/.
  • 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.

Tip

In that command, the fragment app.main:app is equivalent to:

from app.main import app

API docs

Now you can open the API docs UI at: http://127.0.0.1:8000/docs.

Admin Dashboard

You can go to the admin dashboard (created for this workshop) to interact with your API: https://fastapiworkshop.yaquelinehoyos.com.