Skip to content

Commit

Permalink
Define reactive dependencies in 1 place
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Mar 6, 2024
1 parent c6b0530 commit 10f2d57
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions mesa/experimental/jupyter_viz.py
Expand Up @@ -17,7 +17,7 @@
# TODO: Turn this function into a Solara component once the current_step.value
# dependency is passed to measure()
def Card(
model, measures, agent_portrayal, space_drawer, current_step, color, layout_type
model, measures, agent_portrayal, space_drawer, dependencies, color, layout_type
):
with rv.Card(
style_=f"background-color: {color}; width: 100%; height: 100%"
Expand All @@ -27,11 +27,11 @@ def Card(
if space_drawer == "default":
# draw with the default implementation
components_matplotlib.SpaceMatplotlib(
model, agent_portrayal, dependencies=[current_step.value]
model, agent_portrayal, dependencies=dependencies
)
elif space_drawer == "altair":
components_altair.SpaceAltair(
model, agent_portrayal, dependencies=[current_step.value]
model, agent_portrayal, dependencies=dependencies
)
elif space_drawer:
# if specified, draw agent space with an alternate renderer
Expand All @@ -44,7 +44,7 @@ def Card(
measure(model)
else:
components_matplotlib.PlotMatplotlib(
model, measure, dependencies=[current_step.value]
model, measure, dependencies=dependencies
)
return main

Expand Down Expand Up @@ -115,6 +115,8 @@ def do_reseed():
reactive_seed.value = model.random.random()

# jupyter
dependencies = [current_step.value, reactive_seed.value]

def render_in_jupyter():
with solara.GridFixed(columns=2):
UserInputs(user_params, on_change=handle_change_model_params)
Expand All @@ -126,11 +128,11 @@ def render_in_jupyter():
if space_drawer == "default":
# draw with the default implementation
components_matplotlib.SpaceMatplotlib(
model, agent_portrayal, dependencies=[current_step.value]
model, agent_portrayal, dependencies=dependencies
)
elif space_drawer == "altair":
components_altair.SpaceAltair(
model, agent_portrayal, dependencies=[current_step.value]
model, agent_portrayal, dependencies=dependencies
)
elif space_drawer:
# if specified, draw agent space with an alternate renderer
Expand All @@ -144,7 +146,7 @@ def render_in_jupyter():
measure(model)
else:
components_matplotlib.PlotMatplotlib(
model, measure, dependencies=[current_step.value]
model, measure, dependencies=dependencies
)

def render_in_browser():
Expand Down Expand Up @@ -178,7 +180,7 @@ def render_in_browser():
measures,
agent_portrayal,
space_drawer,
current_step,
dependencies,
color="white",
layout_type=layout_types[i],
)
Expand Down

0 comments on commit 10f2d57

Please sign in to comment.