Skip to content

fix(notebooks): make matplotlib list its backends again after Colab installs ipympl - #922

Merged
petercorke merged 1 commit into
mainfrom
fix/colab-matplotlib-backend-list
Nov 1, 2026
Merged

petercorke merged 1 commit into
mainfrom
fix/colab-matplotlib-backend-list

Conversation

@petercorke

Copy link
Copy Markdown
Owner

In a fresh Google Colab runtime, %matplotlib ipympl fails with ValueError: ... 'module://ipympl.backend_nbagg' is not a valid value for backend, after the bootstrap cell has installed ipympl. It works after Runtime / Restart session. #911 installed ipympl but did not handle this.

Cause. matplotlib renames module://ipympl.backend_nbagg to widget and looks that name up in a list of backends it builds once, from the installed packages, the first time it needs it. Colab imports matplotlib before ipympl is installed, so the list lacks it. I reproduced this in isolated virtual environments with matplotlib 3.10.0 (the version on Colab) and 3.11.2, so it does not go away when Colab upgrades.

Fix. After the pip install, the Colab branch of the bootstrap clears matplotlib's backend registry (backend_registry._clear()), so it is rebuilt with ipympl in it. It is guarded for a matplotlib without a registry (before 3.9) and for one without _clear, which is private, though matplotlib uses it in its own tests. Resetting only the registry's _loaded_entry_points flag, which I tried first, is not enough: with IPython installed (as on Colab) it registers matplotlib-inline's backend a second time, which raises RuntimeError: Entry point name 'inline' duplicated. The generated bootstrap cell is regenerated in all the notebooks.

Does this change numerical results or public behaviour? No. It changes the Colab branch of the notebooks' bootstrap cell only.

Tested: in isolated virtual environments with IPython installed and ipympl not, on matplotlib 3.10.0 and 3.11.2, the backend is rejected after the install and accepted after the template's snippet. New tests in tests/test_notebook_bootstrap.py check that the registry is cleared and rebuilt, and that the bootstrap still runs without a registry or without _clear; the first fails without the change. The tests use a fake google.colab and no real pip.

Not yet checked: a real fresh Colab runtime. I will not merge until that has been run.

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

🤖 Generated with Claude Code

…nstalls ipympl

On a fresh Colab runtime the bootstrap installs ipympl, but matplotlib built its
list of known backends at startup without it. It renames module://ipympl... to
"widget" and looks that up, so %matplotlib ipympl failed with "not a valid value
for backend" until the session was restarted. Clear matplotlib's backend registry
after the install, guarded for a matplotlib without one, or without _clear.

Resetting only the registry's loaded flag is not enough: matplotlib-inline's
backend, already registered, would then be registered twice, which is an error.

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 5 complexity · 0 duplication

Metric Results
Complexity 5
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
petercorke merged commit 6b3d0e1 into main Nov 1, 2026
24 checks passed
@petercorke
petercorke deleted the fix/colab-matplotlib-backend-list branch November 1, 2026 21:47
@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