Home / File/ tutorial001_py310.py — fastapi Source File

tutorial001_py310.py — fastapi Source File

Architecture documentation for tutorial001_py310.py, a python file in the fastapi codebase. 2 imports, 0 dependents.

File python FastAPI Responses 2 imports 7 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  41dd859f_0739_ad21_3512_3eebb3d81f0b["tutorial001_py310.py"]
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  41dd859f_0739_ad21_3512_3eebb3d81f0b --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  bf11c16a_04e7_1fd0_b317_a1196fc34aad["sqlmodel"]
  41dd859f_0739_ad21_3512_3eebb3d81f0b --> bf11c16a_04e7_1fd0_b317_a1196fc34aad
  style 41dd859f_0739_ad21_3512_3eebb3d81f0b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select


class Hero(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str = Field(index=True)
    age: int | None = Field(default=None, index=True)
    secret_name: str


sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, connect_args=connect_args)


def create_db_and_tables():
    SQLModel.metadata.create_all(engine)


def get_session():
    with Session(engine) as session:
        yield session


app = FastAPI()


@app.on_event("startup")
def on_startup():
    create_db_and_tables()


@app.post("/heroes/")
def create_hero(hero: Hero, session: Session = Depends(get_session)) -> Hero:
    session.add(hero)
    session.commit()
    session.refresh(hero)
    return hero


@app.get("/heroes/")
def read_heroes(
    session: Session = Depends(get_session),
    offset: int = 0,
    limit: int = Query(default=100, le=100),
) -> list[Hero]:
    heroes = session.exec(select(Hero).offset(offset).limit(limit)).all()
    return heroes


@app.get("/heroes/{hero_id}")
def read_hero(hero_id: int, session: Session = Depends(get_session)) -> Hero:
    hero = session.get(Hero, hero_id)
    if not hero:
        raise HTTPException(status_code=404, detail="Hero not found")
    return hero


@app.delete("/heroes/{hero_id}")
def delete_hero(hero_id: int, session: Session = Depends(get_session)):
    hero = session.get(Hero, hero_id)
    if not hero:
        raise HTTPException(status_code=404, detail="Hero not found")
    session.delete(hero)
    session.commit()
    return {"ok": True}

Domain

Subdomains

Classes

Dependencies

Frequently Asked Questions

What does tutorial001_py310.py do?
tutorial001_py310.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Responses subdomain.
What functions are defined in tutorial001_py310.py?
tutorial001_py310.py defines 7 function(s): create_db_and_tables, create_hero, delete_hero, get_session, on_startup, read_hero, read_heroes.
What does tutorial001_py310.py depend on?
tutorial001_py310.py imports 2 module(s): __init__.py, sqlmodel.
Where is tutorial001_py310.py in the architecture?
tutorial001_py310.py is located at docs_src/sql_databases/tutorial001_py310.py (domain: FastAPI, subdomain: Responses, directory: docs_src/sql_databases).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free