|
13 | 13 | plt.switch_backend("agg") |
14 | 14 |
|
15 | 15 |
|
| 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 | + |
16 | 44 | @solara.component |
17 | 45 | def JupyterViz( |
18 | 46 | model_class, |
@@ -61,33 +89,6 @@ def make_model(): |
61 | 89 | def handle_change_model_params(name: str, value: any): |
62 | 90 | set_model_parameters({**model_parameters, name: value}) |
63 | 91 |
|
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 | | - |
91 | 92 | # 3. Set up UI |
92 | 93 |
|
93 | 94 | with solara.AppBar(): |
@@ -141,7 +142,15 @@ def render_in_browser(): |
141 | 142 | solara.Markdown(md_text=f"####Step - {current_step}") |
142 | 143 |
|
143 | 144 | 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 | + ) |
145 | 154 | for i in range(len(layout_types)) |
146 | 155 | ] |
147 | 156 | solara.GridDraggable( |
|
0 commit comments