diff --git a/IPython/core/magics/pylab.py b/IPython/core/magics/pylab.py index d9bdd17..7f7f1d3 100644 --- a/IPython/core/magics/pylab.py +++ b/IPython/core/magics/pylab.py @@ -24,12 +24,13 @@ from warnings import warn #----------------------------------------------------------------------------- magic_gui_arg = magic_arguments.argument( - 'gui', nargs='?', - help="""Name of the matplotlib backend to use such as 'qt' or 'widget'. + "gui", + nargs="?", + help="""Name of the matplotlib backend to use such as 'qt' or 'widget'. If given, the corresponding matplotlib backend is used, otherwise it will be matplotlib's default (which you can set in your matplotlib config file). - """ + """, ) @@ -93,11 +94,14 @@ class PylabMagics(Magics): args = magic_arguments.parse_argstring(self.matplotlib, line) if args.list: from IPython.core.pylabtools import _matplotlib_manages_backends + if _matplotlib_manages_backends(): from matplotlib.backends.registry import backend_registry + backends_list = backend_registry.list_all() else: from IPython.core.pylabtools import backends + backends_list = list(backends.keys()) print("Available matplotlib backends: %s" % backends_list) else: diff --git a/IPython/core/pylabtools.py b/IPython/core/pylabtools.py index e08ce5c..7c4439a 100644 --- a/IPython/core/pylabtools.py +++ b/IPython/core/pylabtools.py @@ -44,7 +44,9 @@ _deprecated_backends = { # GUI support to activate based on the desired matplotlib backend. For the # most part it's just a reverse of the above dict, but we also need to add a # few others that map to the same GUI manually: -_deprecated_backend2gui = dict(zip(_deprecated_backends.values(), _deprecated_backends.keys())) +_deprecated_backend2gui = dict( + zip(_deprecated_backends.values(), _deprecated_backends.keys()) +) # In the reverse mapping, there are a few extra valid matplotlib backends that # map to the same GUI support _deprecated_backend2gui["GTK"] = _deprecated_backend2gui["GTKCairo"] = "gtk" @@ -279,7 +281,7 @@ def select_figure_formats(shell, formats, **kwargs): [ f.pop(Figure, None) for f in shell.display_formatter.formatters.values() ] mplbackend = matplotlib.get_backend().lower() - if mplbackend in ('nbagg', 'ipympl', 'widget', 'module://ipympl.backend_nbagg'): + if mplbackend in ("nbagg", "ipympl", "widget", "module://ipympl.backend_nbagg"): formatter = shell.display_formatter.ipython_display_formatter formatter.for_type(Figure, _reshow_nbagg_figure) @@ -330,15 +332,16 @@ def find_gui_and_backend(gui=None, gui_select=None): """ import matplotlib + if _matplotlib_manages_backends(): backend_registry = matplotlib.backends.registry.backend_registry # gui argument may be a gui event loop or may be a backend name. if gui in ("auto", None): - backend = matplotlib.rcParamsOrig['backend'] + backend = matplotlib.rcParamsOrig["backend"] backend, gui = backend_registry.resolve_backend(backend) else: - backend, gui = backend_registry.resolve_gui_or_backend(gui) + backend, gui = backend_registry.resolve_gui_or_backend(gui) return gui, backend @@ -347,6 +350,7 @@ def find_gui_and_backend(gui=None, gui_select=None): has_unified_qt_backend = mpl_version_info >= (3, 5) from IPython.core.pylabtools import backends + backends_ = dict(backends) if not has_unified_qt_backend: backends_["qt"] = "qt5agg" @@ -466,5 +470,6 @@ def configure_inline_support(shell, backend): def _matplotlib_manages_backends(): import matplotlib + mpl_version_info = getattr(matplotlib, "__version_info__", (0, 0)) return mpl_version_info >= (3, 9) diff --git a/IPython/core/shellapp.py b/IPython/core/shellapp.py index 1b19b7e..4879e55 100644 --- a/IPython/core/shellapp.py +++ b/IPython/core/shellapp.py @@ -31,7 +31,7 @@ from IPython.terminal import pt_inputhooks gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases)) -backend_keys = [] +backend_keys: list[str] = [] shell_flags = {}