widgetti / solara

A Pure Python, React-style Framework for Scaling Your Jupyter and Web Apps
https://solara.dev
MIT License
1.88k stars 137 forks source link

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

Open rht opened 1 year ago

rht commented 1 year ago

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

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 commented 1 year ago

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

heaysa commented 2 months ago

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

maartenbreddels commented 2 months ago

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.

EwoutH commented 2 months ago

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:

EwoutH commented 1 month ago

@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)

maartenbreddels commented 1 month ago

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)