Skip to content

Commit 039033f

Browse files
committed
solara: Implement decorator to simplify custom space drawer
1 parent 47637a7 commit 039033f

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

mesa/experimental/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .jupyter_viz import JupyterViz, make_text # noqa
1+
from .jupyter_viz import JupyterViz, make_text, prepare_matplotlib_space # noqa

mesa/experimental/jupyter_viz.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,19 @@ def make_user_input(user_input, name, options):
182182
raise ValueError(f"{input_type} is not a supported input type")
183183

184184

185-
def make_space(model, agent_portrayal):
185+
def prepare_matplotlib_space(drawer):
186+
def wrapped_drawer(model, agent_portrayal):
187+
space_fig = Figure()
188+
space_ax = space_fig.subplots()
189+
drawer(model, agent_portrayal, space_fig, space_ax)
190+
space_ax.set_axis_off()
191+
solara.FigureMatplotlib(space_fig)
192+
193+
return wrapped_drawer
194+
195+
196+
@prepare_matplotlib_space
197+
def make_space(model, agent_portrayal, space_fig, space_ax):
186198
def portray(g):
187199
x = []
188200
y = []
@@ -211,14 +223,10 @@ def portray(g):
211223
out["c"] = c
212224
return out
213225

214-
space_fig = Figure()
215-
space_ax = space_fig.subplots()
216226
if isinstance(model.grid, mesa.space.NetworkGrid):
217227
_draw_network_grid(model, space_ax, agent_portrayal)
218228
else:
219229
space_ax.scatter(**portray(model.grid))
220-
space_ax.set_axis_off()
221-
solara.FigureMatplotlib(space_fig)
222230

223231

224232
def _draw_network_grid(model, space_ax, agent_portrayal):

0 commit comments

Comments
 (0)