-
-
Notifications
You must be signed in to change notification settings - Fork 605
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
Page decorator not awaiting page functions #1870
Comments
Thanks for reporting this issue, @ProbablyBrian! @rodja I think we broke something when switching to FastAPI's lifespan API: |
The FastAPI documentation clearly says:
|
This seems to work: # ui_run_with.py
...
app.mount(mount_path, core.app)
app.on_event('startup')(_startup)
app.on_event('shutdown')(_shutdown) Or as a workaround for the user code: from fastapi import FastAPI
from nicegui import ui
from nicegui.nicegui import _shutdown, _startup
app = FastAPI()
@ui.page("/")
async def homepage():
ui.label("Homepage!")
ui.run_with(app)
app.on_event('startup')(_startup)
app.on_event('shutdown')(_shutdown) |
This issue is originating in Starlette (see encode/starlette#649). I think from nicegui.nicegui import _shutdown, _startup
...
ui.run_with(app)
app.on_event('startup')(_startup)
app.on_event('shutdown')(_shutdown) will only work if you do not use the lifespan-API in your main application. If you use these, I guess you would need to run from nicegui.nicegui import _shutdown, _startup
...
ui.run_with(app)
app.lifespan.add_event_handler("startup", _startup)
app.lifespan.add_event_handler("shutdown", _shutdown) Quite ugly in my opinion. Maybe we can add this magic to |
@rodja Oh, I see. You mean something like def lifespan(app):
print('lifespan start')
yield
print('lifespan end')
app = FastAPI(lifespan=lifespan) This doesn't work indeed. |
I'm pretty lost. I don't find a way to register any startup or shutdown handler if Let's ask StackOverflow: https://stackoverflow.com/questions/77362216/ |
I think I found a solution. ui_run_with.py: app.mount(mount_path, core.app)
main_app_lifespan = app.router.lifespan_context
@asynccontextmanager
async def lifespan_wrapper(app):
_startup()
async with main_app_lifespan(app) as maybe_state:
yield maybe_state
_shutdown()
app.router.lifespan_context = lifespan_wrapper It seems hacky, but works with and without |
I updated PR #1877 accordingly. |
Description
Just upgraded to 1.4.0 today and am running into the following problems. Code example to reproduce:
starting this with
$ uvicorn test:app
When navigating to the page, I receive a 500 Internal Server Error. The traceback is extensive but there are a few warnings emitted about coroutines not being awaited:
I downgraded back to 1.3.18 and ran the exact code and this works fine in the previous version.
Versions:
Other notes:
Is this something I'm doing incorrectly with the new 1.4.0 version?
The text was updated successfully, but these errors were encountered: