Server route

I need to somehow change the state depending on the behavior of the external service, I think I need some server route to accept the data, what is the best way to do it?
13 Replies
MaartenBreddels
MaartenBreddels8mo ago
Do you mean a FastAPI endpoint that sets the state of for instance a reactive variable? Could you share the specific use case, that might be interesting for us to hear, and also to better understand what you need.
MindsightsAI
MindsightsAIOP8mo ago
@MaartenBreddels Yes that's right, I do vscode extension, the application on solara runs as an iframe in webview. I need to pass data from vscode extension(js) to reactive variable I do not want to use hard way - pass data to web view, from web view to iFrame with solara, from solara vue to Python using cv
MaartenBreddels
MaartenBreddels8mo ago
Interesting, will this be a public vscode extension? Can we assume 1 virtual kernel (so 1 connected tab)?
MindsightsAI
MindsightsAIOP8mo ago
Yes for both questions, it is open source playground for HSL DSL https://hackmd.io/@hslang/hsl 1 user and 1 instance at one point in time
HackMD
Human Software Language (HSL) - HackMD
HSL introduces a new level of abstraction in programming, seamlessly merging human language with code via advanced Language Models (LLMs) and intuitive markdown syntax. This approach effortlessly transforms ideas into powerful software, eliminating complex syntax and steep learning curves. HSL streamlines development across various platforms, m...
MaartenBreddels
MaartenBreddels8mo ago
I started here, I create the following (endpoint.py):
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route

import solara.server.starlette
import solara.server.starlette
import solara.server.kernel_context



def reset(request: Request):
# we import this late, otherwise we get circular imports
import app
# undocumented, *might* break in the future, but we do our best to keep it working
# Reset the clicks for all kernels
for kernel_id, context in solara.server.kernel_context.contexts.items():
with context:
print("resetting clicks for", kernel_id)
app.clicks.value = 0
return JSONResponse({"reset": "ok"})

routes = [
Route("/_extra/reset", endpoint=reset),
*solara.server.starlette.routes,
]
app = Starlette(routes=routes)
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route

import solara.server.starlette
import solara.server.starlette
import solara.server.kernel_context



def reset(request: Request):
# we import this late, otherwise we get circular imports
import app
# undocumented, *might* break in the future, but we do our best to keep it working
# Reset the clicks for all kernels
for kernel_id, context in solara.server.kernel_context.contexts.items():
with context:
print("resetting clicks for", kernel_id)
app.clicks.value = 0
return JSONResponse({"reset": "ok"})

routes = [
Route("/_extra/reset", endpoint=reset),
*solara.server.starlette.routes,
]
app = Starlette(routes=routes)
And app.py:
import solara

clicks = solara.reactive(0)


@solara.component
def Page():
color = "green"
if clicks.value >= 5:
color = "red"

def increment():
clicks.value += 1
print("clicks", clicks) # noqa

solara.Button(label=f"Clicked: {clicks}", on_click=increment, color=color)
import solara

clicks = solara.reactive(0)


@solara.component
def Page():
color = "green"
if clicks.value >= 5:
color = "red"

def increment():
clicks.value += 1
print("clicks", clicks) # noqa

solara.Button(label=f"Clicked: {clicks}", on_click=increment, color=color)
Run it as SOLARA_APP=app uvicorn endpoint:app and reset the counter using curl http://127.0.0.1:8000/_extra/reset hope that helps, please share with us the end result!
MindsightsAI
MindsightsAIOP7mo ago
Your example works perfectly, but why don’t mine ? In the debugger all looks perfect, variable changed, but inside UI I see old values. Also, printing a variable after changing it works somehow strangely. Source code: https://hackmd.io/@Oe9F3Sh9QpG8wuiS3v99Bg/Bys1sM_bR
MaartenBreddels
MaartenBreddels7mo ago
403 Forbidden - I don't have permission to see that hackmd
MindsightsAI
MindsightsAIOP7mo ago
Access fixed
MaartenBreddels
MaartenBreddels7mo ago
Do you mean that
solara.Markdown(f"### a\n- a: {a.value}\n")
print(a.value)
solara.Markdown(
f"### VS Code State\n- File Path: {vscode_state.value['file_path']}\n- Cursor Location: {str(vscode_state.value['cursor_location'])}\n- Line Text: {vscode_state.value['line_text']}"
)
solara.Markdown(f"### a\n- a: {a.value}\n")
print(a.value)
solara.Markdown(
f"### VS Code State\n- File Path: {vscode_state.value['file_path']}\n- Cursor Location: {str(vscode_state.value['cursor_location'])}\n- Line Text: {vscode_state.value['line_text']}"
)
this piece makes it work? or it still doesn't work?
MindsightsAI
MindsightsAIOP7mo ago
That one works when the ‘a’ value changes based on the chat response, but not from the endpoint. I see logs with correct values from the endpoint and can set up breakpoints to see correct values. However, at the UI, I see old values; it looks like reactivity isn’t working as expected
MaartenBreddels
MaartenBreddels7mo ago
I would go from the working example I gave, and gradually work towards your example. I also don't see why your version doesn't work. Let me know when you solve it, really curious what it is, since I also don't see it.
Jeff
Jeff7mo ago
I have been working on this using Fastapi for a project I'm building. I did get an identical result to what MindsightsAI saw if SOLARA_APP=app.py is used instead of SOLARA_APP=app.
MaartenBreddels
MaartenBreddels7mo ago
Ah, yes, make sure you run as module/package, otherwise app.py is a different namespace . This is similar to ‘python app.py’ and ‘python -m app’
Want results from more Discord servers?
Add your server