Skip to content

fix(notebooks): make the notebooks work on Google Colab - #911

Merged
petercorke merged 6 commits into
mainfrom
fix/colab-ipympl
Nov 1, 2026
Merged

petercorke merged 6 commits into
mainfrom
fix/colab-ipympl

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Running the notebooks on Google Colab failed or misbehaved in four ways.

  • %matplotlib ipympl raised ValueError: ... module://ipympl.backend_nbagg is not a valid value for backend, because Colab does not ship ipympl. The bootstrap cell now installs it and enables Colab's custom widget manager. Every notebook's bootstrap cell is regenerated.
  • A static robot plot was displayed three times. _isnotebook() only recognised ZMQInteractiveShell, so Colab was treated as a desktop window. It now recognises Colab.
  • A static ipympl plot was created with interactive mode on, so it was displayed while still empty and again by plt.show(): a blank figure in VS Code. It is now created quietly and displayed once, when first drawn.
  • A trajectory drew a new plot per frame on Colab, which does not update a display handle in place. Colab now clears the cell output for each frame, as JupyterLite does.

Does this change numerical results or public behaviour? No numerical results change. Notebook display behaviour changes as above, for Colab and for static plots with ipympl.

Tested by hand with graphics-test.ipynb:

  • Colab: the static plot is shown once, has the widget triangle and rotates; the trajectory animates in place (its frames are images, so they cannot be rotated).
  • VS Code: the static plot shows and the trajectory animates.

New tests cover the Colab bootstrap branch, Colab detection and the display logic. They use mocks, so they do not exercise a real notebook frontend. The full suite passes. Not tested: the Swift cells on Colab, JupyterLite after this change, other browsers.

ioff() is also left set after an inline animation, which may stop a later plain plt figure appearing. This predates this change and is not addressed here.

This change was written with Claude Code (Claude Sonnet 5.5).

🤖 Generated with Claude Code

petercorke and others added 4 commits November 1, 2026 11:00
Colab does not ship ipympl, so "%matplotlib ipympl" failed with
"module://ipympl.backend_nbagg is not a valid value for backend".
Install it with the toolbox and enable Colab's custom widget manager,
which ipympl figures need. Regenerated every notebook's bootstrap cell.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…on Colab

A static plot with the ipympl backend created its figure with interactive mode
on, so ipympl displayed it while still empty and plt.show() displayed it again.
VS Code showed a blank figure and Colab showed three copies. The figure is now
created with interactive mode off and its canvas displayed once, when first drawn.

Colab does not update a display handle in place, so a trajectory drew a new
plot per frame. It now clears the cell output for each frame, as JupyterLite does.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
_isnotebook() only accepted ZMQInteractiveShell, but Colab's shell is
google.colab._shell.Shell, so Colab was treated as a desktop window. With the
ipympl backend that path displayed the same figure several times and never
reached the notebook rendering modes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 12 complexity · 0 duplication

Metric Results
Complexity 12
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

petercorke and others added 2 commits November 1, 2026 13:31
… is displayed once

The stub for display assumed every displayed object was an image, so a static
ipympl plot, which now displays its canvas, raised AttributeError. Record images
and widgets separately, and check that a trajectory displays no widget and a
static plot displays its canvas exactly once.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@petercorke
petercorke merged commit ccedb57 into main Nov 1, 2026
23 of 24 checks passed
@petercorke
petercorke deleted the fix/colab-ipympl branch November 1, 2026 05:23
@github-actions github-actions Bot mentioned this pull request Nov 1, 2026
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.

1 participant