Skip to content

Commit

Permalink
refactor: Define current_step as reactive
Browse files Browse the repository at this point in the history
This should reduce the number of arguments that needs to be passed to
the components.
  • Loading branch information
rht committed Nov 15, 2023
1 parent f2f4d1e commit 33b65cc
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions mesa/experimental/jupyter_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def JupyterViz(
specify `space_drawer=False`
play_interval: play interval (default: 150)
"""
current_step, set_current_step = solara.use_state(0)
current_step = solara.use_reactive(0)

# 1. Set up model parameters
user_params, fixed_params = split_model_params(model_params)
Expand All @@ -49,7 +49,7 @@ def JupyterViz(
# 2. Set up Model
def make_model():
model = model_class(**model_parameters)
set_current_step(0)
current_step.value = 0
return model

reset_counter = solara.use_reactive(0)
Expand Down Expand Up @@ -96,9 +96,7 @@ def ColorCard(color, layout_type):
def render_in_jupyter():
with solara.GridFixed(columns=2):
UserInputs(user_params, on_change=handle_change_model_params)
ModelController(
model, play_interval, current_step, set_current_step, reset_counter
)
ModelController(model, play_interval, current_step, reset_counter)

Check warning on line 99 in mesa/experimental/jupyter_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L99

Added line #L99 was not covered by tests
solara.Markdown(md_text=f"###Step - {current_step}")

with solara.GridFixed(columns=2):
Expand Down Expand Up @@ -133,9 +131,7 @@ def render_in_browser():
with solara.Sidebar():
with solara.Card("Controls", margin=1, elevation=2):
UserInputs(user_params, on_change=handle_change_model_params)
ModelController(
model, play_interval, current_step, set_current_step, reset_counter
)
ModelController(model, play_interval, current_step, reset_counter)
with solara.Card("Progress", margin=1, elevation=2):
solara.Markdown(md_text=f"####Step - {current_step}")
resizable = solara.ui_checkbox("Allow resizing", value=True)
Expand All @@ -160,9 +156,7 @@ def render_in_browser():


@solara.component
def ModelController(
model, play_interval, current_step, set_current_step, reset_counter
):
def ModelController(model, play_interval, current_step, reset_counter):
playing = solara.use_reactive(False)
thread = solara.use_reactive(None)
# We track the previous step to detect if user resets the model via
Expand All @@ -172,8 +166,8 @@ def ModelController(
previous_step = solara.use_reactive(0)

def on_value_play(change):
if previous_step.value > current_step and current_step == 0:
# We add extra checks for current_step == 0, just to be sure.
if previous_step.value > current_step.value and current_step.value == 0:
# We add extra checks for current_step.value == 0, just to be sure.
# We automatically stop the playing if a model is reset.
playing.value = False
elif model.running:
Expand All @@ -183,8 +177,8 @@ def on_value_play(change):

def do_step():
model.step()
previous_step.value = current_step
set_current_step(model.schedule.steps)
previous_step.value = current_step.value
current_step.value = model.schedule.steps

Check warning on line 181 in mesa/experimental/jupyter_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L180-L181

Added lines #L180 - L181 were not covered by tests

def do_play():
model.running = True
Expand All @@ -207,11 +201,11 @@ def do_reset():
reset_counter.value += 1

def do_set_playing(value):
if current_step == 0:
if current_step.value == 0:
# This means the model has been recreated, and the step resets to
# 0. We want to avoid triggering the playing.value = False in the
# on_value_play function.
previous_step.value = current_step
previous_step.value = current_step.value

Check warning on line 208 in mesa/experimental/jupyter_viz.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/jupyter_viz.py#L208

Added line #L208 was not covered by tests
playing.set(value)

with solara.Row():
Expand Down

0 comments on commit 33b65cc

Please sign in to comment.