Repository navigation
fix(notebooks): make matplotlib list its backends again after Colab installs ipympl - #922
Merged
Merged
Conversation
…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>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 5 |
| Duplication | 0 |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In a fresh Google Colab runtime,
%matplotlib ipymplfails withValueError: ... 'module://ipympl.backend_nbagg' is not a valid value for backend, after the bootstrap cell has installedipympl. It works after Runtime / Restart session. #911 installedipymplbut did not handle this.Cause. matplotlib renames
module://ipympl.backend_nbaggtowidgetand 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 beforeipymplis 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 withipymplin 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_pointsflag, which I tried first, is not enough: with IPython installed (as on Colab) it registersmatplotlib-inline's backend a second time, which raisesRuntimeError: 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
ipymplnot, 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 intests/test_notebook_bootstrap.pycheck 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 fakegoogle.colaband 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