diff --git a/src/machinevisiontoolbox/Camera.py b/src/machinevisiontoolbox/Camera.py index b25e32c5..8114f6f4 100644 --- a/src/machinevisiontoolbox/Camera.py +++ b/src/machinevisiontoolbox/Camera.py @@ -21,9 +21,10 @@ # from mpl_toolkits.mplot3d import Axes3D, art3d from mpl_toolkits.mplot3d.art3d import Poly3DCollection from spatialmath import SE3, Line3 +from spatialmath.base import isnotebook from machinevisiontoolbox import Image -from machinevisiontoolbox.base.imageio import _isnotebook, idisp, safe_plt_show +from machinevisiontoolbox.base.imageio import idisp, safe_plt_show from machinevisiontoolbox.ImagePointFeatures import FeatureMatch from machinevisiontoolbox.mvtb_types import ArrayLike @@ -740,7 +741,7 @@ def _new_imageplane( If this camera already has a virtual image plane, return ``True``. Otherwise, create an axes, and optionally a figure, and return ``False``. """ - if _isnotebook(): + if isnotebook(): ax = plt.gca() # return current or create new if self._ax is None or self._ax is not ax: self._ax = ax diff --git a/src/machinevisiontoolbox/base/imageio.py b/src/machinevisiontoolbox/base/imageio.py index 523c96eb..1f0b5679 100644 --- a/src/machinevisiontoolbox/base/imageio.py +++ b/src/machinevisiontoolbox/base/imageio.py @@ -1051,29 +1051,6 @@ def cv_destroy_window(title: str | None = None, block: bool = True) -> None: cv2.waitKey(1) # run the event loop -def _isnotebook() -> bool: - """ - Determine if code is being run from a Jupyter notebook - - ``_isnotebook`` is True if running Jupyter notebook, else False - - :references: - - - https://stackoverflow.com/questions/15411967/how-can-i-check-if-code- - is-executed-in-the-ipython-notebook/39662359#39662359 - """ - try: - shell = get_ipython().__class__.__name__ # type: ignore[name-defined] - if shell in ("Shell", "ZMQInteractiveShell"): - return True # Jupyter notebook or qtconsole or CoLab - elif shell == "TerminalInteractiveShell": - return False # Terminal running IPython - else: - return False # Other type (?) - except NameError: - return False # Probably standard Python interpreter - - def iread( filename: str | Path, *args: Any, **kwargs: Any ) -> tuple[np.ndarray, str] | tuple[list[np.ndarray], list[str]]: diff --git a/tests/base/test_io.py b/tests/base/test_io.py index 3ca164e0..a737bc37 100644 --- a/tests/base/test_io.py +++ b/tests/base/test_io.py @@ -902,15 +902,19 @@ def test_convert_dtype_half_alias(self): class TestHelperFunctions(unittest.TestCase): """Test helper functions in imageio""" - def test_isnotebook(self): - """Test _isnotebook function""" - from machinevisiontoolbox.base.imageio import _isnotebook - - result = _isnotebook() - # Should return a boolean - self.assertIsInstance(result, bool) - # In test environment, should be False - self.assertFalse(result) + def test_camera_isnotebook(self): + """Camera decides how to reuse its image plane with spatialmath's isnotebook""" + import importlib + import sys + from unittest import mock + + isnotebook = importlib.import_module("machinevisiontoolbox.Camera").isnotebook + + # In the test environment, should be False + self.assertFalse(isnotebook()) + # JupyterLite (Pyodide) is a notebook, though it is not a ZMQ shell + with mock.patch.object(sys, "platform", "emscripten"): + self.assertTrue(isnotebook()) class TestIdisp_matplotlib_paths(unittest.TestCase):