Skip to content

Commit

Permalink
docs: add live update example (#229)
Browse files Browse the repository at this point in the history
Demonstrates pushing data from the python/server side.
  • Loading branch information
alisterburt authored Oct 10, 2023
1 parent 919a79d commit 5a7ef42
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions solara/website/pages/examples/general/live_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from time import sleep

import solara
import numpy as np
from matplotlib import pyplot as plt

# ensure that an interactive backend doesn't start when plotting with matplotlib
plt.switch_backend('agg')


@solara.component
def Page():
# define some state which will be updated regularly in a separate thread
counter = solara.use_reactive(0)

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

# run the render loop in a separate thread
result = 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))
solara.FigureMatplotlib(fig)


Page()

0 comments on commit 5a7ef42

Please sign in to comment.