-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add live update example (#229)
Demonstrates pushing data from the python/server side.
- Loading branch information
1 parent
919a79d
commit 5a7ef42
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |