Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/machinevisiontoolbox/Camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
23 changes: 0 additions & 23 deletions src/machinevisiontoolbox/base/imageio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down
22 changes: 13 additions & 9 deletions tests/base/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading