Fastapi Call websocket #3021
-
Hi, i modify your template in order to use nicegui as fastapi backend server with the frontend. It works very well, https://github.com/Frenz86/fastapinicegui BR |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Sorry. Could you break it down and create a minimal example which shows your problem? |
Beta Was this translation helpful? Give feedback.
-
@Frenz86 I roughly boiled it down to the following code, even though the FastAPI part isn't needed to reproduce the problem: import uvicorn
from fastapi import FastAPI
from nicegui import ui
app = FastAPI()
async def compute_and_update_result(num1, num2):
somma = num1 + num2
ui.notify(f'risultato : {somma}')
return somma
@ui.page('/')
async def home():
num1 = ui.number(value=1)
num2 = ui.number(value=2)
ui.button('risultato', on_click=lambda: compute_and_update_result(num1.value, num2.value))
result = await compute_and_update_result(num1.value, num2.value)
ui.markdown(f"#### result = {result}") # didn't update
ui.run_with(app, mount_path='/')
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True) You create a Something like this might work: @ui.page('/')
async def home():
num1 = ui.number(value=1)
num2 = ui.number(value=2)
async def handle_click():
result = await compute_and_update_result(num1.value, num2.value)
markdown.content = f"#### result = {result}"
ui.button('risultato', on_click=handle_click)
markdown = ui.markdown() |
Beta Was this translation helpful? Give feedback.
@Frenz86 I roughly boiled it down to the following code, even though the FastAPI part isn't needed to reproduce the problem: