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
36 changes: 1 addition & 35 deletions src/roboticstoolbox/backends/PyPlot/PyPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from roboticstoolbox.backends.Connector import Connector
from roboticstoolbox.backends.PyPlot.EllipsePlot import EllipsePlot, ShapePlot
from roboticstoolbox.backends.PyPlot.RobotPlot import RobotPlot
from roboticstoolbox.tools.plot import isnotebook as _isnotebook
from roboticstoolbox.tools.plot import plane_view, set_window_title
from roboticstoolbox.tools.types import ArrayLike

Expand Down Expand Up @@ -848,41 +849,6 @@ def update(val: float, text: list[Any], robot: Any) -> None: # pragma: no cover
self.step()


def _isnotebook() -> bool:
"""
Determine if code is being run from a Jupyter notebook or JupyterLite

``_isnotebook`` is True if running Jupyter notebook, JupyterLite, Google Colab, or
similar, else False

:references:

- https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-
is-executed-in-the-ipython-notebook/39662359#39662359
"""
import sys

# Check if running in Pyodide/JupyterLite
if sys.platform == "emscripten":
return True

# Colab's shell is google.colab._shell.Shell, not a ZMQInteractiveShell
if "google.colab" in sys.modules:
return True

# Fall back to checking IPython shell type
try:
shell = get_ipython().__class__.__name__ # type: ignore[name-defined]
if shell == "ZMQInteractiveShell":
return True # Jupyter notebook or qtconsole
elif shell == "TerminalInteractiveShell":
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter


def _resolve_render_mode(
render_mode: str | None = None, animation: bool = False
) -> str:
Expand Down
22 changes: 1 addition & 21 deletions src/roboticstoolbox/mobile/Vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from roboticstoolbox.mobile.Animations import VehicleAnimationBase, VehiclePolygon
from roboticstoolbox.mobile.drivers import VehicleDriverBase
from roboticstoolbox.mobile.workspace import WorkspaceLike, resolve_workspace
from roboticstoolbox.tools.plot import isnotebook as _isnotebook
from roboticstoolbox.tools.plot import set_window_title
from roboticstoolbox.tools.types import ArrayLike, NDArray

Expand All @@ -36,27 +37,6 @@
)


def _isnotebook() -> bool:
"""
Determine if code is being run from a Jupyter notebook or JupyterLite.
"""
if sys.platform == "emscripten":
return True

if "ipykernel" in sys.modules or "google.colab" in sys.modules:
return True

try:
get_ipython_fn = globals().get("get_ipython", None)
if callable(get_ipython_fn):
shell = get_ipython_fn().__class__.__name__
if shell == "ZMQInteractiveShell":
return True
return False
except Exception:
return False


def _refresh_animation_frame(
fig: Figure | SubFigure | None, dt: float, pause: bool
) -> None:
Expand Down
34 changes: 34 additions & 0 deletions src/roboticstoolbox/tools/plot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from typing import Any

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -232,3 +233,36 @@ def xplot(
apply_block(block)

return fig.get_axes()


def isnotebook() -> bool:
"""
Determine if code is being run from a Jupyter notebook or JupyterLite

``isnotebook`` is True if running Jupyter notebook, JupyterLite, Google Colab, or
similar, else False

:references:

- https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-
is-executed-in-the-ipython-notebook/39662359#39662359
"""
# Check if running in Pyodide/JupyterLite
if sys.platform == "emscripten":
return True

# Colab's shell is google.colab._shell.Shell, not a ZMQInteractiveShell
if "google.colab" in sys.modules:
return True

# Fall back to checking IPython shell type
try:
shell = get_ipython().__class__.__name__ # type: ignore[name-defined]
if shell == "ZMQInteractiveShell":
return True # Jupyter notebook or qtconsole
elif shell == "TerminalInteractiveShell":
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter
44 changes: 44 additions & 0 deletions tests/test_render_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,47 @@ def test_colab_with_ipympl_is_a_widget_for_a_static_plot(monkeypatch):

assert _resolve_render_mode(None) == "notebook-widget"
assert _resolve_render_mode(None, animation=True) == "notebook-inline"


# --- one notebook check for the whole package ---------------------------------


def test_mobile_uses_the_same_notebook_check():
from roboticstoolbox.tools.plot import isnotebook

# the package also exports a class called Vehicle, so ask for the module
vehicle_module = sys.modules["roboticstoolbox.mobile.Vehicle"]

assert pyplot_module._isnotebook is isnotebook
assert vehicle_module._isnotebook is isnotebook


def _shell(monkeypatch, name):
import builtins

monkeypatch.delitem(sys.modules, "google.colab", raising=False)
monkeypatch.setattr(sys, "platform", "linux")
shell = type(name, (), {})()
monkeypatch.setattr(builtins, "get_ipython", lambda: shell, raising=False)


def test_a_jupyter_kernel_is_a_notebook(monkeypatch):
from roboticstoolbox.tools.plot import isnotebook

_shell(monkeypatch, "ZMQInteractiveShell")

assert isnotebook() is True


def test_ipython_in_a_terminal_is_not_a_notebook_even_if_ipykernel_is_loaded(
monkeypatch,
):
# the check the mobile module used to have counted a loaded ipykernel
import types

from roboticstoolbox.tools.plot import isnotebook

_shell(monkeypatch, "TerminalInteractiveShell")
monkeypatch.setitem(sys.modules, "ipykernel", types.ModuleType("ipykernel"))

assert isnotebook() is False
Loading