Skip to content
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

Add startup and shutdown handlers to existing FastAPI app #1877

Merged
merged 4 commits into from
Oct 26, 2023

Conversation

falkoschindler
Copy link
Contributor

This PR tries to solve #1870 and #1874 by adding startup and shutdown handlers to an existing FastAPI app.
This has been broken by switching to the new lifespan API. But the lifespan events of a sub application don't seem to be executed. Therefore this extra step is necessary.

@falkoschindler falkoschindler linked an issue Oct 25, 2023 that may be closed by this pull request
@falkoschindler falkoschindler added the bug Something isn't working label Oct 25, 2023
@falkoschindler falkoschindler added this to the 1.4.1 milestone Oct 25, 2023
@falkoschindler falkoschindler modified the milestones: 1.4.2, 1.4.1 Oct 25, 2023
@falkoschindler
Copy link
Contributor Author

Test app:

test.py

from fastapi import FastAPI
from nicegui import ui

def lifespan(_):
    print("main startup")
    yield
    print("main shutdown")

app = FastAPI(lifespan=lifespan)
# or without lifespan:
# app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

@ui.page('/page')
def page():
    ui.button('Hi!', on_click=lambda: ui.notify('Hello World!'))

ui.run_with(app)

Run with: uvicorn test:app --port 8000

@rodja
Copy link
Member

rodja commented Oct 26, 2023

I have not time to test it myself today :-(
Maybe someone else can do a review for me, @ProbablyBrian and/or @zilch42?

@rodja rodja linked an issue Oct 26, 2023 that may be closed by this pull request
Copy link
Member

@rodja rodja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just walked the code with @falkoschindler. Seems to work as expected.

@rodja rodja merged commit 4a00988 into main Oct 26, 2023
5 checks passed
@rodja rodja deleted the run-with-lifespan branch October 26, 2023 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Async broken on v1.4.0 with FastAPI app Page decorator not awaiting page functions
2 participants