Skip to content

Commit ab3df4d

Browse files
committed
solara: Implement decorator to simplify custom space drawer
1 parent ccde77b commit ab3df4d

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

mesa/experimental/__init__.py

+1-1
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

+13-5
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,19 @@ def change_handler(value, name=name):
219219
raise ValueError(f"{input_type} is not a supported input type")
220220

221221

222-
def make_space(model, agent_portrayal):
222+
def prepare_matplotlib_space(drawer):
223+
def wrapped_drawer(model, agent_portrayal):
224+
space_fig = Figure()
225+
space_ax = space_fig.subplots()
226+
drawer(model, agent_portrayal, space_fig, space_ax)
227+
space_ax.set_axis_off()
228+
solara.FigureMatplotlib(space_fig)
229+
230+
return wrapped_drawer
231+
232+
233+
@prepare_matplotlib_space
234+
def make_space(model, agent_portrayal, space_fig, space_ax):
223235
def portray(g):
224236
x = []
225237
y = []
@@ -248,14 +260,10 @@ def portray(g):
248260
out["c"] = c
249261
return out
250262

251-
space_fig = Figure()
252-
space_ax = space_fig.subplots()
253263
if isinstance(model.grid, mesa.space.NetworkGrid):
254264
_draw_network_grid(model, space_ax, agent_portrayal)
255265
else:
256266
space_ax.scatter(**portray(model.grid))
257-
space_ax.set_axis_off()
258-
solara.FigureMatplotlib(space_fig)
259267

260268

261269
def _draw_network_grid(model, space_ax, agent_portrayal):

0 commit comments

Comments
 (0)