Skip to content

Commit d6821a4

Browse files
rhtjackiekazil
authored andcommitted
refactor: Turn ColorCard into Solara component; rename to Card
1 parent 031c97b commit d6821a4

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

mesa/experimental/jupyter_viz.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@
1313
plt.switch_backend("agg")
1414

1515

16+
@solara.component
17+
def Card(
18+
model, measures, agent_portrayal, space_drawer, current_step, color, layout_type
19+
):
20+
with rv.Card(
21+
style_=f"background-color: {color}; width: 100%; height: 100%"
22+
) as main:
23+
if "Space" in layout_type:
24+
rv.CardTitle(children=["Space"])
25+
if space_drawer == "default":
26+
# draw with the default implementation
27+
components_matplotlib.SpaceMatplotlib(
28+
model, agent_portrayal, dependencies=[current_step.value]
29+
)
30+
elif space_drawer:
31+
# if specified, draw agent space with an alternate renderer
32+
space_drawer(model, agent_portrayal)
33+
elif "Measure" in layout_type:
34+
rv.CardTitle(children=["Measure"])
35+
measure = measures[layout_type["Measure"]]
36+
if callable(measure):
37+
# Is a custom object
38+
measure(model)
39+
else:
40+
components_matplotlib.make_plot(model, measure)
41+
return main
42+
43+
1644
@solara.component
1745
def JupyterViz(
1846
model_class,
@@ -61,33 +89,6 @@ def make_model():
6189
def handle_change_model_params(name: str, value: any):
6290
set_model_parameters({**model_parameters, name: value})
6391

64-
def ColorCard(color, layout_type):
65-
# TODO: turn this into a Solara component, but must pass in current
66-
# step as a dependency for the plots, so that there is no flickering
67-
# due to rerender.
68-
with rv.Card(
69-
style_=f"background-color: {color}; width: 100%; height: 100%"
70-
) as main:
71-
if "Space" in layout_type:
72-
rv.CardTitle(children=["Space"])
73-
if space_drawer == "default":
74-
# draw with the default implementation
75-
components_matplotlib.SpaceMatplotlib(
76-
model, agent_portrayal, dependencies=[current_step.value]
77-
)
78-
elif space_drawer:
79-
# if specified, draw agent space with an alternate renderer
80-
space_drawer(model, agent_portrayal)
81-
elif "Measure" in layout_type:
82-
rv.CardTitle(children=["Measure"])
83-
measure = measures[layout_type["Measure"]]
84-
if callable(measure):
85-
# Is a custom object
86-
measure(model)
87-
else:
88-
components_matplotlib.make_plot(model, measure)
89-
return main
90-
9192
# 3. Set up UI
9293

9394
with solara.AppBar():
@@ -141,7 +142,15 @@ def render_in_browser():
141142
solara.Markdown(md_text=f"####Step - {current_step}")
142143

143144
items = [
144-
ColorCard(color="white", layout_type=layout_types[i])
145+
Card(
146+
model,
147+
measures,
148+
agent_portrayal,
149+
space_drawer,
150+
current_step,
151+
color="white",
152+
layout_type=layout_types[i],
153+
)
145154
for i in range(len(layout_types))
146155
]
147156
solara.GridDraggable(

0 commit comments

Comments
 (0)