Skip to content

[WIP] Update on_event with lifespan event handler in docs #958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/tutorial/fastapi/simple-hero-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ And then create an `app` object that is an instance of that `FastAPI` class:

We want to make sure that once the app starts running, the function `create_tables` is called. To create the database and tables.

This should be called only once at startup, not before every request, so we put it in the function to handle the `"startup"` event:
This should be called only once at startup, not before every request. We define this startup logic using the lifespan parameter of the FastAPI app. The input for this parameter is an async function lifespan() which yields after calling `create_db_and_tables`.

//// tab | Python 3.10+

Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/app_testing/tutorial001/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import List, Optional

from fastapi import Depends, FastAPI, HTTPException, Query
Expand Down Expand Up @@ -44,12 +45,13 @@ def get_session():
yield session


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
11 changes: 7 additions & 4 deletions docs_src/tutorial/fastapi/app_testing/tutorial001_py310/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import asynccontextmanager

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

Expand Down Expand Up @@ -42,12 +44,13 @@ def get_session():
yield session


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/app_testing/tutorial001_py39/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import Optional

from fastapi import Depends, FastAPI, HTTPException, Query
Expand Down Expand Up @@ -44,12 +45,13 @@ def get_session():
yield session


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/delete/tutorial001.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import List, Optional

from fastapi import FastAPI, HTTPException, Query
Expand Down Expand Up @@ -39,12 +40,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
11 changes: 7 additions & 4 deletions docs_src/tutorial/fastapi/delete/tutorial001_py310.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import asynccontextmanager

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

Expand Down Expand Up @@ -37,12 +39,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/delete/tutorial001_py39.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import Optional

from fastapi import FastAPI, HTTPException, Query
Expand Down Expand Up @@ -39,12 +40,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/limit_and_offset/tutorial001.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import List, Optional

from fastapi import FastAPI, HTTPException, Query
Expand Down Expand Up @@ -33,12 +34,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
11 changes: 7 additions & 4 deletions docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py310.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import asynccontextmanager

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

Expand Down Expand Up @@ -31,12 +33,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/limit_and_offset/tutorial001_py39.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import Optional

from fastapi import FastAPI, HTTPException, Query
Expand Down Expand Up @@ -33,12 +34,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/multiple_models/tutorial001.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import List, Optional

from fastapi import FastAPI
Expand Down Expand Up @@ -35,12 +36,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
11 changes: 7 additions & 4 deletions docs_src/tutorial/fastapi/multiple_models/tutorial001_py310.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from sqlmodel import Field, Session, SQLModel, create_engine, select

Expand Down Expand Up @@ -33,12 +35,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/multiple_models/tutorial001_py39.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import Optional

from fastapi import FastAPI
Expand Down Expand Up @@ -35,12 +36,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/multiple_models/tutorial002.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import List, Optional

from fastapi import FastAPI
Expand Down Expand Up @@ -33,12 +34,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
11 changes: 7 additions & 4 deletions docs_src/tutorial/fastapi/multiple_models/tutorial002_py310.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from sqlmodel import Field, Session, SQLModel, create_engine, select

Expand Down Expand Up @@ -31,12 +33,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/multiple_models/tutorial002_py39.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import Optional

from fastapi import FastAPI
Expand Down Expand Up @@ -33,12 +34,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
10 changes: 6 additions & 4 deletions docs_src/tutorial/fastapi/read_one/tutorial001.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import asynccontextmanager
from typing import List, Optional

from fastapi import FastAPI, HTTPException
Expand Down Expand Up @@ -33,12 +34,13 @@ def create_db_and_tables():
SQLModel.metadata.create_all(engine)


app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
create_db_and_tables()
yield


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


@app.post("/heroes/", response_model=HeroPublic)
Expand Down
Loading
Loading