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

fix: when used under a prefix with flask, strip off the leading root #113

Closed maartenbreddels closed 11 months ago

maartenbreddels commented 12 months ago

Found when answering https://github.com/widgetti/solara/discussions/106

bobwatcherx commented 11 months ago

can you give an example if i want to deploy solara with flask

bobwatcherx commented 11 months ago

or fastapi

bobwatcherx commented 11 months ago

why this code not work

from fastapi import FastAPI
import solara.server.fastapi
from solara import *
app = FastAPI()

@app.get("/solara")
@solara.component()
def Page():
    with Column(margin=10):
        Text("nice app")
        Button("nice")

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

solara_app = FastAPI()
app.mount("/solara", app=solara_app)
maartenbreddels commented 11 months ago

Did you see https://solara.dev/docs/deploying/self-hosted ?

If you have the following files:

.
├── app.py
└── sol.py
# app.py
from flask import Flask

import solara.server.flask

app = Flask(__name__)
app.register_blueprint(solara.server.flask.blueprint, url_prefix="/solara/")

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"
# sol.py
import solara

@solara.component
def Page():
    with solara.Column():
        solara.Title("nice app")
    with solara.Columns([1, 2]):
        solara.Button("nice", color="primary")
        solara.Button("nice222", color="red")
$ FLASK_DEBUG=1 SOLARA_APP=sol.py flask run 

Now navigate to http://127.0.0.1:5000/solara/

bobwatcherx commented 11 months ago

is it possible to deploy with vercel . I tried but not found in the browser app.py

import os
from flask import Flask

import solara.server.flask

app = Flask(__name__)
app.register_blueprint(solara.server.flask.blueprint, url_prefix="/solara/")

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

if __name__ == "__main__":
    # Mendapatkan nilai SOLARA_APP dari variabel lingkungan
    solara_app = os.environ.get("SOLARA_APP")

    # Jika SOLARA_APP ditentukan, gunakan sebagai konfigurasi Solara
    if solara_app:
        app.config["SOLARA_APP"] = solara_app

    app.run()

my vercel.json

{
  "rewrites": [
    { "source": "/(.*)", "destination": "/app.py" }
  ]
}

and .env SOLARA_APP=sol.py

maartenbreddels commented 11 months ago

Vercel does not support websockets: https://vercel.com/guides/do-vercel-serverless-functions-support-websocket-connections

This means a vanilla version of solara is unsuitable for deployment in Vercel. We plan to support fully browser-based python runtimes, so you don't need a server. In that case, it can be used with vercel. We hope to support this soon.

maartenbreddels commented 11 months ago

I just update the docs https://solara.dev/docs/deploying/self-hosted and released a new version, flask, starlette and fastapi should be properly working now, and should be properly documented. If anything is unclear, let me know.

bobwatcherx commented 11 months ago

Apakah Anda melihat https://solara.dev/docs/deploying/self-hosted ?

Jika Anda memiliki file berikut:

.
├── app.py
└── sol.py
# app.py
from flask import Flask

import solara.server.flask

app = Flask(__name__)
app.register_blueprint(solara.server.flask.blueprint, url_prefix="/solara/")

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"
# sol.py
import solara

@solara.component
def Page():
    with solara.Column():
        solara.Title("nice app")
    with solara.Columns([1, 2]):
        solara.Button("nice", color="primary")
        solara.Button("nice222", color="red")
$ FLASK_DEBUG=1 SOLARA_APP=sol.py flask run 

Sekarang arahkan ke http://127.0.0.1:5000/solara/

error run app in version 1.16.0 and this log error sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 139847832528448 and this is thread id 139847861432320.

bobwatcherx commented 11 months ago

but with fastapi it works fine .

bobwatcherx commented 11 months ago

I'm trying to deploy solara to railway but there is an error like this. how to fix it

app.py file

from fastapi import FastAPI
import solara.server.fastapi
from dotenv import load_dotenv
import os

load_dotenv()  # Memuat variabel lingkungan dari file .env

app = FastAPI()

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

app.mount("/solara/", app=solara.server.fastapi.app)

if __name__ == "__main__":
    solara_app = os.getenv("SOLARA_APP")
    command = f"{solara_app} uvicorn app:app"
    os.system(command)

railway.json file

{
  "$schema": "https://railway.app/railway.schema.json",
  "build": {
    "builder": "NIXPACKS"
  },
  "deploy": {
    "startCommand": "SOLARA_APP=sol.py uvicorn app:app --host 0.0.0.0 --port $PORT",
    "restartPolicyType": "ON_FAILURE",
    "restartPolicyMaxRetries": 10
  }
}
    await self.app(scope, receive, send)
  File "/home/miop/belajar/solaradep2/venv/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/home/miop/belajar/solaradep2/venv/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/home/miop/belajar/solaradep2/venv/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/home/miop/belajar/solaradep2/venv/lib/python3.10/site-packages/solara/server/starlette.py", line 240, in root
    content = server.read_root(request_path, root_path)
  File "/home/miop/belajar/solaradep2/venv/lib/python3.10/site-packages/solara/server/server.py", line 216, in read_root
    default_app = app.apps["__default__"]
maartenbreddels commented 11 months ago

Why do you add:

if __name__ == "__main__":
    solara_app = os.getenv("SOLARA_APP")
    command = f"{solara_app} uvicorn app:app"
    os.system(command)

That would start the app again and again right?

I have used railway before, and it worked out of the box, but now I cannot get it to work anymore.

I created https://github.com/maartenbreddels/solara-railway using solara create portal solara-railway

But for some reason it copies only the pyproject.toml to the docker image during the install phase, and not the whole project. Would be great if we can get it working again.