Repository: projectmesa/mesa
Pull request #1877 (2023-11-15 01:39:51)

Convert make_space into Solara component
Depends on #1875 to be merged first.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/projectmesa/mesa/pull/1877?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) Report
Attention: `8 lines` in your changes are missing coverage. Please review.
> Comparison is base [(`f2f4d1e`)](https://app.codecov.io/gh/projectmesa/mesa/commit/f2f4d1ef8ccb84a545c43b9f19b3b5204b82033e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) 77.10% compared to head [(`60bc0a8`)](https://app.codecov.io/gh/projectmesa/mesa/pull/1877?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) 77.04%.

| [Files](https://app.codecov.io/gh/projectmesa/mesa/pull/1877?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) | Patch % | Lines |
|---|---|---|
| [mesa/experimental/jupyter\_viz.py](https://app.codecov.io/gh/projectmesa/mesa/pull/1877?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa#diff-bWVzYS9leHBlcmltZW50YWwvanVweXRlcl92aXoucHk=) | 50.00% | [8 Missing :warning: ](https://app.codecov.io/gh/projectmesa/mesa/pull/1877?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #1877      +/-   ##
==========================================
- Coverage   77.10%   77.04%   -0.06%     
==========================================
  Files          15       15              
  Lines         974      976       +2     
  Branches      213      214       +1     
==========================================
+ Hits          751      752       +1     
- Misses        195      198       +3     
+ Partials       28       26       -2     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/projectmesa/mesa/pull/1877?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).

tpike3: This is failing the python 3.8 and python 3.9 build---

```
def SpaceMatplotlib(model, agent_portrayal, dependencies: List[Any] | None = None):
E   TypeError: unsupported operand type(s) for |: '_GenericAlias' and 'NoneType'
```
rht: Fixed.

Patch:
diff --git a/mesa/experimental/jupyter_viz.py b/mesa/experimental/jupyter_viz.py
index 552a1982..d0843448 100644
--- a/mesa/experimental/jupyter_viz.py
+++ b/mesa/experimental/jupyter_viz.py
@@ -1,5 +1,6 @@
 import sys
 import threading
+from typing import List, Optional
 
 import matplotlib.pyplot as plt
 import networkx as nx
@@ -33,7 +34,7 @@ def JupyterViz(
         name: name for display
         agent_portrayal: options for rendering agents (dictionary)
         space_drawer: method to render the agent space for
-            the model; default implementation is :meth:`make_space`;
+            the model; default implementation is the `SpaceMatplotlib` component;
             simulations with no space to visualize should
             specify `space_drawer=False`
         play_interval: play interval (default: 150)
@@ -71,7 +72,9 @@ def JupyterViz(
                 rv.CardTitle(children=["Space"])
                 if space_drawer == "default":
                     # draw with the default implementation
-                    make_space(model, agent_portrayal)
+                    SpaceMatplotlib(
+                        model, agent_portrayal, dependencies=[current_step.value]
+                    )
                 elif space_drawer:
                     # if specified, draw agent space with an alternate renderer
                     space_drawer(model, agent_portrayal)
@@ -103,7 +106,9 @@ def JupyterViz(
             # 4. Space
             if space_drawer == "default":
                 # draw with the default implementation
-                make_space(model, agent_portrayal)
+                SpaceMatplotlib(
+                    model, agent_portrayal, dependencies=[current_step.value]
+                )
             elif space_drawer:
                 # if specified, draw agent space with an alternate renderer
                 space_drawer(model, agent_portrayal)
@@ -310,7 +315,8 @@ def UserInputs(user_params, on_change=None):
             raise ValueError(f"{input_type} is not a supported input type")
 
 
-def make_space(model, agent_portrayal):
+@solara.component
+def SpaceMatplotlib(model, agent_portrayal, dependencies: Optional[List[any]] = None):
     space_fig = Figure()
     space_ax = space_fig.subplots()
     space = getattr(model, "grid", None)
@@ -324,7 +330,7 @@ def make_space(model, agent_portrayal):
     else:
         _draw_grid(space, space_ax, agent_portrayal)
     space_ax.set_axis_off()
-    solara.FigureMatplotlib(space_fig, format="png")
+    solara.FigureMatplotlib(space_fig, format="png", dependencies=dependencies)
 
 
 def _draw_grid(space, space_ax, agent_portrayal):


Test patch:
diff --git a/tests/test_jupyter_viz.py b/tests/test_jupyter_viz.py
index 97021252..dd125ffc 100644
--- a/tests/test_jupyter_viz.py
+++ b/tests/test_jupyter_viz.py
@@ -82,13 +82,15 @@ class TestMakeUserInput(unittest.TestCase):
 
 
 class TestJupyterViz(unittest.TestCase):
-    @patch("mesa.experimental.jupyter_viz.make_space")
-    def test_call_space_drawer(self, mock_make_space):
+    @patch("mesa.experimental.jupyter_viz.SpaceMatplotlib")
+    def test_call_space_drawer(self, mock_space_matplotlib):
         mock_model_class = Mock()
         agent_portrayal = {
             "Shape": "circle",
             "color": "gray",
         }
+        current_step = 0
+        dependencies = [current_step]
         # initialize with space drawer unspecified (use default)
         # component must be rendered for code to run
         solara.render(
@@ -99,13 +101,13 @@ class TestJupyterViz(unittest.TestCase):
             )
         )
         # should call default method with class instance and agent portrayal
-        mock_make_space.assert_called_with(
-            mock_model_class.return_value, agent_portrayal
+        mock_space_matplotlib.assert_called_with(
+            mock_model_class.return_value, agent_portrayal, dependencies=dependencies
         )
 
         # specify no space should be drawn; any false value should work
         for falsy_value in [None, False, 0]:
-            mock_make_space.reset_mock()
+            mock_space_matplotlib.reset_mock()
             solara.render(
                 JupyterViz(
                     model_class=mock_model_class,
@@ -115,7 +117,7 @@ class TestJupyterViz(unittest.TestCase):
                 )
             )
             # should call default method with class instance and agent portrayal
-            assert mock_make_space.call_count == 0
+            assert mock_space_matplotlib.call_count == 0
 
         # specify a custom space method
         altspace_drawer = Mock()