|
1 | 1 | import threading
|
2 | 2 |
|
3 | 3 | import matplotlib.pyplot as plt
|
| 4 | +import reacton.ipywidgets as widgets |
4 | 5 | import solara
|
5 | 6 | from matplotlib.figure import Figure
|
6 | 7 | from matplotlib.ticker import MaxNLocator
|
@@ -155,10 +156,40 @@ def make_model():
|
155 | 156 | )
|
156 | 157 |
|
157 | 158 | # 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 | + |
158 | 168 | with solara.Row():
|
159 | 169 | 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) |
162 | 193 | # solara.Button(label="Reset", color="primary", on_click=do_reset)
|
163 | 194 |
|
164 | 195 | with solara.GridFixed(columns=2):
|
|
0 commit comments