Open liquidcarbon opened 5 days ago
@liquidcarbon, marimo apps are stateful so when fastapi autoreload, it actually tears down all the state, including your active session, so the 401 makes sense to me since you no longer have the session you are connecting to.
maybe we can do something so that the page auto-refreshes too (creating a new session)
That would be cool! Could you point me at where in the code I can poke around?
I think the timing of refresh also matters. It wouldn't matter in my example because I'm just changing markdown, but if I started altering code, autosave may capture the notebook in a broken state. Maybe it's better to disable autosave when working in that way.
Also: #2891
What's the reason you don't use the watcher available in marimo run
in the ASGI apps?
https://github.com/marimo-team/marimo/blob/main/marimo/_server/start.py#L116 vs https://github.com/marimo-team/marimo/blob/main/marimo/_server/asgi.py#L150
good question! idk, i originally made the ASGI apps
for deployment and didn't think about using it for development. but i can see why you'd want to. does adding the watcher work for you? if you can add the watcher but turn off fastapi
s built-in watcher, that might work
Seems like your lifespans need to be in the parent app itself, something like app = FastAPI(lifespan=lifespans.Lifespans([lifespans.watcher]))
- but at this point I really don't know what I'm doing anymore. :)
Without that, tried modifying asgi.py
:
146 app = create_starlette_app(
147 base_url="",
148 lifespan=lifespans.Lifespans(
149 [
150 # Not all lifespans are needed for run mode
151 lifespans.watcher, # <- here
152 lifespans.etc,
153 lifespans.signal_handler,
154 ]
155 ),
156 enable_auth=not AuthToken.is_empty(auth_token),
157 allow_origins=("*",),
158 )
159 app.state.watch = True # <- and here
160 app.state.session_manager = session_manager
161 app.state.base_url = path
162 app.state.config_manager = user_config_mgr
No noticeable changes.
Tried with and without uvicorn's autoreload, with and without watchdog.
In marimo run --watch
everything works smoothly.
Describe the bug
I'm having a lot of fun building dashboards for a data app using Marimo mounted on top of FastAPI.
One minor nuisance is that when I change the notebook that drives the app, it takes a couple of refreshes to pick up the changes.
The left screen is the running marimo notebook (via
marimo edit
), the right is the FastAPI app served by uvicorn with autoreload.Is this something that's easy to fix?
I'm one step away from app dev heaven :)
Environment
Code to reproduce
FastAPI logs during the GIF recording - that
401 Unauthorized
might be the culprit?app.py - via
.venv/bin/python -m api.app
marimo_server.py