Let matplotlib choose its default backend#35
Conversation
SimonPinches
left a comment
There was a problem hiding this comment.
Have we tested with ipympl inside a Jupyter/JupyterHub?
Co-authored-by: Simon Pinches <SimonPinches@users.noreply.github.com>
| import os | ||
| import re | ||
|
|
||
| import matplotlib |
There was a problem hiding this comment.
Moving this to a function-local import (inside _is_interactive_backend, which is the only place it's used) would avoid a subtle problem: the entry scripts import clihelper before idstools.view.common, and the --rc backend=... mechanism from PR #33 relies on setting MPLBACKEND before matplotlib's first import (matplotlib only reads that env var at import time). With this top-level import, matplotlib now gets imported before _backend_from_cli_rc runs, so --rc "backend=..." (including the examples added in jupyter.rst) is silently ignored for early backend selection. I verified this with matplotlib 3.8.2: MPLBACKEND set after import matplotlib has no effect, and switching rcParams["backend"] later doesn't convert already-created figure canvases.
I think we should re-test the two --rc examples in the new docs and confirm they actually produce an interactive (draggable/zoomable) figure rather than a static one?
| return getattr(canvas.fig.canvas, "required_interactive_framework", None) is not None | ||
|
|
||
|
|
||
| def _is_jupyter(): |
There was a problem hiding this comment.
This duplicates _is_jupyter() from idstools/view/common.py (line 35): Could we import it from there instead (from idstools.view.common import _is_jupyter)? No circular-import risk since view/common.py doesn't import clihelper, and as a bonus, importing view.common here would run its _backend_from_cli_rc env setup before matplotlib's first import, which would also solve the --rc backend ordering issue flagged above.
One drawback however is that importing view.common from clihelper means every clihelper user pays the cost of importing matplotlib.pyplot and the style setup at import time. If that's a problem, the cleaner alternative would be to move _is_jupyter (and the _backend_from_cli_rc env logic) into a small shared module with no pyplot dependency, and keep the matplotlib import in clihelper function-local. That's more work though!
…yed Matplotlib imports so --rc backend=... is applied before Matplotlib loads.

batch node test