Skip to content

Commit

Permalink
feat: Add reset button to JupyterViz
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored and Corvince committed Sep 11, 2023
1 parent 20dbf9e commit e97e829
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions mesa/experimental/jupyter_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ def make_model():
set_current_step(0)
return model

model = solara.use_memo(make_model, dependencies=list(model_parameters.values()))
reset_counter = solara.use_reactive(0)
model = solara.use_memo(
make_model, dependencies=[*list(model_parameters.values()), reset_counter.value]
)

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

# 3. Set up UI
solara.Markdown(name)
UserInputs(user_params, on_change=handle_change_model_params)
ModelController(model, play_interval, current_step, set_current_step)
ModelController(model, play_interval, current_step, set_current_step, reset_counter)

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


@solara.component
def ModelController(model, play_interval, current_step, set_current_step):
def ModelController(
model, play_interval, current_step, set_current_step, reset_counter
):
playing = solara.use_reactive(False)
thread = solara.use_reactive(None)

Expand Down Expand Up @@ -112,6 +117,9 @@ def do_pause():
model.running = False
thread.join()

def do_reset():
reset_counter.value += 1

with solara.Row():
solara.Button(label="Step", color="primary", on_click=do_step)
# This style is necessary so that the play widget has almost the same
Expand All @@ -132,6 +140,7 @@ def do_pause():
playing=playing.value,
on_playing=playing.set,
)
solara.Button(label="Reset", color="primary", on_click=do_reset)
solara.Markdown(md_text=f"**Step:** {current_step}")
# threaded_do_play is not used for now because it
# doesn't work in Google colab. We use
Expand Down

0 comments on commit e97e829

Please sign in to comment.