Get help from the marimo community

Updated last month

Serving multiple marimo apps on the same fastapi app

At a glance

The community member is trying to serve Marimo apps on a company server using Python and FastAPI. They are facing issues with imports, as the sandbox imports do not seem to work. The community member has considered a few options to solve the import problem, such as using mo.install() to pull in the sandbox imports, running each app in its own Marimo process and port, or using a global UV environment. However, they are unsure of the best approach that would keep the apps sandboxed while allowing them to have one Marimo FastAPI app.

In the comments, other community members suggest that there is no great out-of-the-box solution currently, and that installing before each app might be the simplest approach, but it could lead to potential conflicts in the global environment. The community members also discuss the idea of spawning a Marimo run --sandbox when the URL is hit and using FastAPI to proxy that port, although one community member had trouble getting the websocket functionality to work.

There is no explicitly marked answer in the comments, but the community members are collaborating to find a solution to the import problem while keeping the apps sandboxed.

Useful resources
I am trying to serve marimo apps on a company server (python, not webassembly).
I have fastapi working fine, but I am running up against a problem with imports.

Originally I thought the sandbox imports would work, but it looks like this is not the case (I found a message on this discord).

That leaves a few options to solve the import problem:

  • use mo.install() to somehow pull in the sandbox imports? But if all the apps are in the same marimo process does this create conflicting imports? Also how would I read in the imports?
  • use marimo run --sandbox and have each app have its own process and port that i have to forward to
  • use a global uv environment for all apps and keep it synced across apps somehow. This will eventually lead to conflicts.
Obviously my preference would be something where i can just have one marimo fastapi app,
but I am not sure what the best way to achieve this is, while keeping apps sandboxed.
Generally each app will have many imports.

Thanks for any advice!
M
b
5 comments
We don’t have a great solution now out of the box.

Installing before each app is probably the simplest but you do run into a potential conflict issue polluting the global environment. Do you envision you’d have conflicts? I guess pinning each dep in each app probably does create conflicts?

This has been requested a few times. We can try to prioritize this sooner.
thanks!

i am now looking at spawning a marimo run --sandbox when the url is hit, and then using fastapi to proxy that port? i can't tell how janky that will be but i think it should work?

i love sandbox and i think conflicts are unfortunately inevitable with bioinformatics tools!
I think the proxying could work too. You could open source that and see if others want to contribute (and maybe we can contribute too)
I had a pretty decent stab at this but I could not get the websocket stuff to work.

There is a nice library that in theory should be exactly what is needed
https://github.com/WSH032/fastapi-proxy-lib?tab=readme-ov-file

but even my minimal code fails, so i gave up for now!

Plain Text
import uvicorn
from fastapi_proxy_lib.fastapi.app import reverse_http_app
app = reverse_http_app(base_url="http://localhost:8001/")
if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)
I foolishly returned to this problem and burned a couple of hours failing to get the websockets to authenticate.

Posting here on the off chance someone sees an obvious problem!

I think this version is probably the closest to working(?), but there's extra cruft in here too compared to other versions I tested. In other words, I very likely tested it without each individual function too.
Add a reply
Sign up and join the conversation on Discord