Skip to content

Commit 1c12fb4

Browse files
rhttpike3
authored andcommitted
solara frontend: Make play/pause work in Colab
1 parent 77966c4 commit 1c12fb4

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

experimental/jupyter_viz.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import threading
22

33
import matplotlib.pyplot as plt
4+
import reacton.ipywidgets as widgets
45
import solara
56
from matplotlib.figure import Figure
67
from matplotlib.ticker import MaxNLocator
@@ -155,10 +156,40 @@ def make_model():
155156
)
156157

157158
# 3. Buttons
159+
playing = solara.use_reactive(False)
160+
161+
def on_value_play(change):
162+
if viz.model.running:
163+
playing.value = True
164+
viz.do_step()
165+
else:
166+
playing.value = False
167+
158168
with solara.Row():
159169
solara.Button(label="Step", color="primary", on_click=viz.do_step)
160-
solara.Button(label="▶", color="primary", on_click=viz.threaded_do_play)
161-
solara.Button(label="⏸︎", color="primary", on_click=viz.do_pause)
170+
solara.Style(
171+
"""
172+
.widget-play {
173+
height: 30px;
174+
}
175+
"""
176+
)
177+
widgets.Play(
178+
value=0,
179+
interval=400,
180+
repeat=True,
181+
show_repeat=False,
182+
on_value=on_value_play,
183+
playing=playing.value,
184+
on_play=playing.set,
185+
)
186+
# threaded_do_play is not used for now because it
187+
# doesn't work in Google colab. We use
188+
# ipywidgets.Play until it is fixed. The threading
189+
# version is definite a much better implementation,
190+
# if it works.
191+
# solara.Button(label="▶", color="primary", on_click=viz.threaded_do_play)
192+
# solara.Button(label="⏸︎", color="primary", on_click=viz.do_pause)
162193
# solara.Button(label="Reset", color="primary", on_click=do_reset)
163194

164195
with solara.GridFixed(columns=2):

0 commit comments

Comments
 (0)