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

Lifespan state is not passed when a FastAPI app mount a Nicegui app #3094

Closed
RomainEconomics opened this issue May 17, 2024 · 3 comments · Fixed by #3097
Closed

Lifespan state is not passed when a FastAPI app mount a Nicegui app #3094

RomainEconomics opened this issue May 17, 2024 · 3 comments · Fixed by #3097
Labels
enhancement New feature or request
Milestone

Comments

@RomainEconomics
Copy link
Contributor

Description

Following this example: https://github.com/zauberzeug/nicegui/tree/main/examples/fastapi

But simply adding a lifespan with some state in the main app:

from contextlib import asynccontextmanager

import frontend
from fastapi import FastAPI, Request


@asynccontextmanager
async def lifespan(app: FastAPI):
    yield {"some_state": 10}


app = FastAPI(lifespan=lifespan)


@app.get("/")
async def read_root(request: Request):
    print(request.state.__dict__)

frontend.init(app)

When calling the "/" route, the state dict is empty.

However, this works normally when running the app without mounting the frontend.

This seems to come from the fact that in the ui_run_with.py the state is not passed.

    @asynccontextmanager
    async def lifespan_wrapper(app):
        await _startup()
        async with main_app_lifespan(app) as state:
            yield state

        await _shutdown()

When yielding the state, this works again

@rodja rodja added the enhancement New feature or request label May 18, 2024
@rodja rodja added this to the 1.4.26 milestone May 18, 2024
@rodja
Copy link
Member

rodja commented May 18, 2024

Thanks for bringing this up @RomainEconomics. I was not aware about the ability to yield state in a lifespan context manager. Would you like to create a pull request for this?

@RomainEconomics
Copy link
Contributor Author

Yes, for sure
I've created the pull request with the small modification

@rodja
Copy link
Member

rodja commented May 18, 2024

Wonderful. Thank you so much, @RomainEconomics!

@rodja rodja closed this as completed May 18, 2024
@rodja rodja linked a pull request May 18, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants