Skip to content

feat(base): add environment() and move isnotebook out of the matplotlib block - #271

Open
petercorke wants to merge 1 commit into
rai-opensource:masterfrom
petercorke:feat/notebook-environment
Open

petercorke wants to merge 1 commit into
rai-opensource:masterfrom
petercorke:feat/notebook-environment

Conversation

@petercorke

Copy link
Copy Markdown
Collaborator

Summary

isnotebook() lived inside graphics.py's matplotlib try: block, so it disappeared when matplotlib was not installed, and it returned False on Google Colab and JupyterLite. RTB, MVTB and bdsim have each grown their own copy of the check as a result.

  • New matplotlib-free module spatialmath/base/_environment.py.
  • environment() returns one of "terminal", "ipython", "jupyter", "colab", "jupyterlite".
  • isnotebook() is now a thin wrapper, True for jupyter, colab and jupyterlite.
  • smb.isnotebook is still exported, and smb.environment is new. Nothing in SMTB called isnotebook().

JupyterLab, classic Notebook, VS Code and Jupyter Desktop all report "jupyter": they cannot be told apart reliably from inside the kernel, and how figures render depends on the matplotlib backend rather than the front end. The docstring says so.

Behaviour change

isnotebook() now returns True on Colab and JupyterLite (previously False).

Testing

Full suite: 416 passed, 3 skipped (was 413 on current master). Black 23.10.0 clean on the touched files.

New tests/base/test_environment.py covers all five values plus an unknown shell. These tests are mocked only: a fake get_ipython() and patched sys.modules / sys.platform stand in for the real front ends. No real Jupyter, Colab or JupyterLite kernel is exercised in CI.

Follow-ups (not in this PR)

RTB and MVTB can drop their private _isnotebook copies in favour of this once it lands.

🤖 Generated with Claude Code

…ib block

isnotebook() lived inside graphics.py's matplotlib try-block, so it vanished
when matplotlib was missing, and it reported False for Google Colab and
JupyterLite. RTB, MVTB and bdsim each grew their own copy as a result.

Add environment(), returning "terminal", "ipython", "jupyter", "colab" or
"jupyterlite", in a new matplotlib-free module, and make isnotebook() a thin
wrapper that is True for jupyter, colab and jupyterlite. smb.isnotebook is
still exported. Tests use a faked get_ipython/sys.modules/sys.platform.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@petercorke
petercorke requested a review from taughz as a code owner October 22, 2026 01:53
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants