Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: live update example using usk_task instead of use_thread and simpler #963

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions solara/website/pages/documentation/examples/general/live_update.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
from time import sleep

import numpy as np
from matplotlib import pyplot as plt

from typing import cast, Optional
import httpx
import asyncio
import solara
import solara.lab


@solara.component
def Page():
# define some state which will be updated regularly in a separate thread
counter = solara.use_reactive(0)
btc = solara.use_reactive(cast(Optional[float], None))

def render():
"""Infinite loop regularly mutating counter state"""
async def fetch_btc_price():
while True:
sleep(0.2)
counter.value += 1

# run the render loop in a separate thread
result: solara.Result[bool] = solara.use_thread(render)
if result.error:
raise result.error

# create the LiveUpdatingComponent, this component depends on the counter
# value so will be redrawn whenever counter value changes
LiveUpdatingComponent(counter.value)


@solara.component
def LiveUpdatingComponent(counter):
"""Component which will be redrawn whenever the counter value changes."""
fig, ax = plt.subplots()
ax.plot(np.arange(10), np.random.random(10))
plt.close(fig)
solara.FigureMatplotlib(fig)
await asyncio.sleep(1)
async with httpx.AsyncClient() as client:
url = "https://api.binance.com/api/v1/ticker/price?symbol=BTCUSDT"
response = await client.get(url)
btc.value = float(response.json()["price"])
print("btc.value", btc.value)

fetch_result = solara.lab.use_task(fetch_btc_price, dependencies=[])
# the task keeps running, so is always in the pending mode, so we combine it with the btc value being None
if fetch_result.pending and btc.value is None:
solara.Text("Fetching BTC price...")
else:
if fetch_result.error:
solara.Error(f"Error fetching BTC price: {fetch_result.exception}")
else:
solara.Text(f"BTC price: ${btc.value}")


Page()
Loading