Skip to content

Commit e97e829

Browse files
rhtCorvince
authored andcommitted
feat: Add reset button to JupyterViz
1 parent 20dbf9e commit e97e829

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

mesa/experimental/jupyter_viz.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ def make_model():
5151
set_current_step(0)
5252
return model
5353

54-
model = solara.use_memo(make_model, dependencies=list(model_parameters.values()))
54+
reset_counter = solara.use_reactive(0)
55+
model = solara.use_memo(
56+
make_model, dependencies=[*list(model_parameters.values()), reset_counter.value]
57+
)
5558

5659
def handle_change_model_params(name: str, value: any):
5760
set_model_parameters({**model_parameters, name: value})
5861

5962
# 3. Set up UI
6063
solara.Markdown(name)
6164
UserInputs(user_params, on_change=handle_change_model_params)
62-
ModelController(model, play_interval, current_step, set_current_step)
65+
ModelController(model, play_interval, current_step, set_current_step, reset_counter)
6366

6467
with solara.GridFixed(columns=2):
6568
# 4. Space
@@ -81,7 +84,9 @@ def handle_change_model_params(name: str, value: any):
8184

8285

8386
@solara.component
84-
def ModelController(model, play_interval, current_step, set_current_step):
87+
def ModelController(
88+
model, play_interval, current_step, set_current_step, reset_counter
89+
):
8590
playing = solara.use_reactive(False)
8691
thread = solara.use_reactive(None)
8792

@@ -112,6 +117,9 @@ def do_pause():
112117
model.running = False
113118
thread.join()
114119

120+
def do_reset():
121+
reset_counter.value += 1
122+
115123
with solara.Row():
116124
solara.Button(label="Step", color="primary", on_click=do_step)
117125
# This style is necessary so that the play widget has almost the same
@@ -132,6 +140,7 @@ def do_pause():
132140
playing=playing.value,
133141
on_playing=playing.set,
134142
)
143+
solara.Button(label="Reset", color="primary", on_click=do_reset)
135144
solara.Markdown(md_text=f"**Step:** {current_step}")
136145
# threaded_do_play is not used for now because it
137146
# doesn't work in Google colab. We use

0 commit comments

Comments
 (0)