Hello, i asked this question on discord and was recommended to post it here. I have a working solution but the UI updates very slowly so any recommendations on how to improve this implementation would be greatly appreciated. First, the two code files:
import time
from broadcaster import Broadcast
from fastapi import FastAPI, WebSocket
app = FastAPI()
broadcast = Broadcast("memory://")
@app.on_event("startup")
async def startup():
await broadcast.connect()
@app.on_event("shutdown")
async def shutdown():
await broadcast.disconnect()
@app.websocket("/ws")
async def ws_endpoint(websocket: WebSocket):
await websocket.accept()
async with broadcast.subscribe(channel="notifications") as subscriber:
async for event in subscriber:
await websocket.send_text(event.message)
@app.get("/work_endpoint")
async def do_work():
print("starting work")
for i in range(10):
await broadcast.publish(channel="notifications", message=f"performing iteration {i}/10")
print(i)
time.sleep(1)
return {"response": "ping received"}
Explanation and Problem
I'm trying to have a Taipy text element update in real-time as my fastapi backend does work (be it processing, data acquisition etc); I'm using taipy almost exclusively as a package for creating a pretty frontend (and it's great for that).
The solution I've given works, but it's very slow. I can monitor the progress of the work_endpoint in the terminal, and the console text element on the Taipy frontend does reflect the progress (eventually), but it updates very slowly (a few seconds) and definitely doesn't give the effect of "real-time".
I used the example in here to work out how to update the console text element but I'm wondering if that's a bit slow.
Any suggestions on how to improve this implementation would be much appreciated - I've been stuck on this for a couple of nights now.
[edit] I could potentially take advantage of some of the callbacks listed here so I'll have to look at that some more.
What would you like to share or ask?
Hello, i asked this question on discord and was recommended to post it here. I have a working solution but the UI updates very slowly so any recommendations on how to improve this implementation would be greatly appreciated. First, the two code files:
Taipy Frontend
FastAPI backend
Explanation and Problem
I'm trying to have a Taipy text element update in real-time as my fastapi backend does work (be it processing, data acquisition etc); I'm using taipy almost exclusively as a package for creating a pretty frontend (and it's great for that).
The solution I've given works, but it's very slow. I can monitor the progress of the
work_endpoint
in the terminal, and the console text element on the Taipy frontend does reflect the progress (eventually), but it updates very slowly (a few seconds) and definitely doesn't give the effect of "real-time".I used the example in here to work out how to update the console text element but I'm wondering if that's a bit slow.
Any suggestions on how to improve this implementation would be much appreciated - I've been stuck on this for a couple of nights now.
[edit] I could potentially take advantage of some of the callbacks listed here so I'll have to look at that some more.
Many thanks
Code of Conduct