coltone24
coltone24
Mmarimo
Created by coltone24 on 1/13/2025 in #help-support
Accessing Middleware Values in a Marimo Cell
I'm running a FastAPI server with Marimo and using SessionMiddleware to manage authentication via a cookie containing the access token. Here's the FastAPI setup:
server = marimo.create_asgi_app()
apps_dir = os.path.join(os.path.dirname(__file__), "apps")
for filename in sorted(os.listdir(apps_dir)):
if filename.endswith(".py"):
app_name = os.path.splitext(filename)[0]
app_path = os.path.join(apps_dir, filename)
server = server.with_app(path=f"/{app_name}", root=app_path)

app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="test")
app.add_middleware(auth_middleware)

labs_app.mount("/", server.build())
server = marimo.create_asgi_app()
apps_dir = os.path.join(os.path.dirname(__file__), "apps")
for filename in sorted(os.listdir(apps_dir)):
if filename.endswith(".py"):
app_name = os.path.splitext(filename)[0]
app_path = os.path.join(apps_dir, filename)
server = server.with_app(path=f"/{app_name}", root=app_path)

app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="test")
app.add_middleware(auth_middleware)

labs_app.mount("/", server.build())
In the Marimo app code, I can access the access_token from the session middleware, but I can't figure out how to access the same value within a Marimo cell. Here's the cell code:
import marimo

app = marimo.App(width="medium")

access_token = context["access_token"]

@app.cell
def _():
import marimo as mo
mo.md(access_token) # Trying to use the access_token from above but fails
return mo

if __name__ == "__main__":
app.run()
import marimo

app = marimo.App(width="medium")

access_token = context["access_token"]

@app.cell
def _():
import marimo as mo
mo.md(access_token) # Trying to use the access_token from above but fails
return mo

if __name__ == "__main__":
app.run()
1. How can I properly access the values outside the cell (like access_token) in a Marimo cell? 2. Is there a recommended approach for passing session values from FastAPI's middleware into Marimo's context or cells? Any guidance or examples would be appreciated!
11 replies