solara icon indicating copy to clipboard operation
solara copied to clipboard

feature request: Allow launching the Solara server via python app.py

Open rht opened this issue 2 years ago • 6 comments

Currently, there are 2 ways to run the Solara app:

  • solara run app.py
  • Launch a Jupyter notebook server, then execute the cells in app.py

My request is to have an option to do python app.py so that it is easier to document. Though, saying solara run app.py already works. Perhaps a better use (though this is not my use case at the moment) case would be to have another web server code that launches the Solara server programmatically.

rht avatar Jul 04 '23 10:07 rht

Though a subprocess.Popen(["solara", "run", "app.py"]) would have worked too.

rht avatar Jul 04 '23 11:07 rht

One use case for adding this functionality: it may make it easier to produce an pyinstaller executable from code employing solara.

heaysa avatar Aug 14 '24 03:08 heaysa

In https://github.com/widgetti/solara/pull/716 (pyinstaller PR) we use

@click.command()
@click.option(
    "--port",
    default=int(os.environ.get("PORT", 0)),
    help="Port to run the server on, 0 for a random free port",
)
def run(port: int, webview: bool = False):
    if "SOLARA_APP" not in os.environ:
        os.environ["SOLARA_APP"] = "sample_app"

    import solara.server.starlette

    server = solara.server.starlette.ServerStarlette(host="localhost", port=port)
    print(f"Starting server on {server.base_url}")
    server.serve_threaded()
    server.wait_until_serving()
    server.join()

See: https://github.com/widgetti/solara/pull/716/files#diff-d0b5daeae94ba7a7305fb2cc670f3670739192411f2b867befe0f7eaea562a44

I hope that helps for now. It's not a documented API, but we've been using it for quite a while and I'll try to not break it without reason.

I can imagine we'll add a solara.serve(..) in the future.

maartenbreddels avatar Aug 14 '24 13:08 maartenbreddels

Thanks for sharing, will certainly look into it!

For Mesa, this would be a really useful feature. So if at one point its considered making this a stable and documented feature, I would support it.

In general, we're looking at how to interface (ideally back and forth) between Solara and the Python interpreter. See for some details:

  • https://github.com/projectmesa/mesa/issues/2176

EwoutH avatar Aug 14 '24 20:08 EwoutH

@maartenbreddels what do you think of the feasibility of being able to go back and forth between a Solara component and other Python instances? Ideally both could control each other, but that might get quite complicated.

Having two distinct modes would already be useful:

  1. Model controls visualization progress/updates
  2. Visualization controls model steps/functions

(CC @Corvince)

EwoutH avatar Aug 22 '24 08:08 EwoutH

I think that is quite feasable (if I understand you correctly).

The problem I see, is that solara needs to know (or be told) when to update something. If you use reactive variables, that makes it easy, you get this for free. However, that means a dependency of your model classes on solara itself.

Otherwise, you'd have to use something like traitlets etc (and think about multi-user, if you run solara out of the notebook, in solara server)

maartenbreddels avatar Aug 23 '24 14:08 maartenbreddels