widgetti / solara

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

Serving App via FastAPI #27

Closed samiit closed 1 year ago

samiit commented 1 year ago

Hi everyone,

I would love to have some documentation hints at how to serve a wonderful SOLARA app using FastAPI.

In the example shown here, I don't know how to specify the python file name that contains the app. I guess the first argument of app.mount, i.e., /solara is the directory containing the python file containing the app.

Thanks, Sam

samiit commented 1 year ago

@maartenbreddels : can you shed some light?

maartenbreddels commented 1 year ago

In the beginning of the document:

The biggest difference with the development server is that all configurations should go via environment variables instead of command-line argument. For instance, if you run the development server like solara run myapp.py, we should instead set the SOLARA_APP environment variable to myapp.py. For instance

$ export SOLARA_APP=myapp.py

run flask or starlette

this informs the app to use, and the /solara is the path of the url (I.e. http://localhost:8765/solara), not the directory of the app. If you change it to / it should be available at the root (I.e. http://localhost:8765)

does that make sense? If you have ideas how to improve the docs, please let us know.

samiit commented 1 year ago

Hi,

I explored a bit based on the docs and made it work finally! Thanks for the pointer. This was just the missing ingredient.

I was wondering if I can contribute with a working example for FastAPI.

samiit commented 1 year ago

As an aside, may I know if there is some way to serve different Solara Apps based on different end points? Or is there only one app that we can serve based on the environment variable: SOLARA_APP=myapp.py

So, I would like something like the following:

main.py

from fastapi import FastAPI
from solara.server.fastapi import app as solapp

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello Solara App World!"}

app.mount("/app1", app=solapp)
app.mount("/app2", app=solapp)

And then declare the env variable

SOLARA_APP=views/solara_app1.py

Any provision to introduce another file in SOLARA_APP??

And then I run:

uvicorn main:app --port 1234 --reload

And now the following end point works:

  1. http://127.0.0.1:1234/
  2. http://127.0.0.1:1234/app1

Any suggestions how to get multiple apps to work, e.g., http://127.0.0.1:1234/app2

?? Thanks, Sam

maartenbreddels commented 1 year ago

Yes, I’d love it if you could open a pr on the docs on this part. I’m not sure we have good docs on how to contribute, or how to locally run the docs website. But if you make notes on what you are missing while working on that, we can also improve that part of the docs.

maartenbreddels commented 1 year ago

https://solara.dev/docs/howto/multipage might help you a bit, but they are not different endpoints, and support for regular widget apps is lacking. Long term that will likely improve, and having multiple apps is something that is possible, and is planned for, but not fully implemented. Can I ask why you need this, what problem are you trying to solve?

samiit commented 1 year ago

Thanks again for the pointer to multipage apps, but will need to wait how to have different apps at different end points!

I had plans to serve different apps to different users when they login to the web page (implemented using FastAPI), or even serve them a host of apps to choose from, each of them solving a different problem. So far, we were serving each app using Voila, but wondered if a Solara app would be a better option!

Thanks for the wonderful work and will watch this space for updates. Else would also be ready to contribute if I can to make this happen.

PS: I am transitioning from data science to DevOps and am learning things along the way!

maartenbreddels commented 1 year ago

Hi Sam,

we now support multipage for regular ipywidgets. See dcde83b33c85f65de3118ec1cb145749aade9b3d and https://solara-demo.herokuapp.com/docs/howto/multipage Let me know if everything is clear, and if this enabled you to create what you want with Solara. If not, I'm happy to do a call with you to understand your issue better.

Regard,

Maarten