##// END OF EJS Templates
Correctly handle "ipython --matplotlib=<whatever>"
Ian Thomas -
Show More
@@ -93,17 +93,12 b' class PylabMagics(Magics):'
93 93 """
94 94 args = magic_arguments.parse_argstring(self.matplotlib, line)
95 95 if args.list:
96 from IPython.core.pylabtools import _matplotlib_manages_backends
96 from IPython.core.pylabtools import _list_matplotlib_backends_and_gui_loops
97 97
98 if _matplotlib_manages_backends():
99 from matplotlib.backends.registry import backend_registry
100
101 backends_list = backend_registry.list_all()
102 else:
103 from IPython.core.pylabtools import backends
104
105 backends_list = list(backends.keys())
106 print("Available matplotlib backends: %s" % backends_list)
98 print(
99 "Available matplotlib backends: %s"
100 % _list_matplotlib_backends_and_gui_loops()
101 )
107 102 else:
108 103 gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
109 104 self._show_matplotlib_backend(args.gui, backend)
@@ -483,7 +483,8 b' def _matplotlib_manages_backends() -> bool:'
483 483
484 484 If it returns True, the caller can be sure that
485 485 matplotlib.backends.registry.backend_registry is available along with
486 member functions resolve_gui_or_backend, resolve_backend and list_all.
486 member functions resolve_gui_or_backend, resolve_backend, list_all, and
487 list_gui_frameworks.
487 488 """
488 489 global _matplotlib_manages_backends_value
489 490 if _matplotlib_manages_backends_value is None:
@@ -497,3 +498,21 b' def _matplotlib_manages_backends() -> bool:'
497 498 _matplotlib_manages_backends_value = False
498 499
499 500 return _matplotlib_manages_backends_value
501
502
503 def _list_matplotlib_backends_and_gui_loops() -> list[str]:
504 """Return list of all Matplotlib backends and GUI event loops.
505
506 This is the list returned by
507 %matplotlib --list
508 """
509 if _matplotlib_manages_backends():
510 from matplotlib.backends.registry import backend_registry
511
512 ret = backend_registry.list_all() + backend_registry.list_gui_frameworks()
513 else:
514 from IPython.core import pylabtools
515
516 ret = list(pylabtools.backends.keys())
517
518 return sorted(["auto"] + ret)
@@ -11,36 +11,45 b' import glob'
11 11 from itertools import chain
12 12 import os
13 13 import sys
14 import typing as t
14 15
15 16 from traitlets.config.application import boolean_flag
16 17 from traitlets.config.configurable import Configurable
17 18 from traitlets.config.loader import Config
18 19 from IPython.core.application import SYSTEM_CONFIG_DIRS, ENV_CONFIG_DIRS
19 from IPython.core import pylabtools
20 20 from IPython.utils.contexts import preserve_keys
21 21 from IPython.utils.path import filefind
22 22 from traitlets import (
23 Unicode, Instance, List, Bool, CaselessStrEnum, observe,
23 Unicode,
24 Instance,
25 List,
26 Bool,
27 CaselessStrEnum,
28 observe,
24 29 DottedObjectName,
30 Undefined,
25 31 )
26 32 from IPython.terminal import pt_inputhooks
27 33
28 #-----------------------------------------------------------------------------
34 # -----------------------------------------------------------------------------
29 35 # Aliases and Flags
30 #-----------------------------------------------------------------------------
36 # -----------------------------------------------------------------------------
31 37
32 38 gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases))
33 39
34 backend_keys: list[str] = []
35
36 40 shell_flags = {}
37 41
38 42 addflag = lambda *args: shell_flags.update(boolean_flag(*args))
39 addflag('autoindent', 'InteractiveShell.autoindent',
40 'Turn on autoindenting.', 'Turn off autoindenting.'
43 addflag(
44 "autoindent",
45 "InteractiveShell.autoindent",
46 "Turn on autoindenting.",
47 "Turn off autoindenting.",
41 48 )
42 addflag('automagic', 'InteractiveShell.automagic',
43 """Turn on the auto calling of magic commands. Type %%magic at the
49 addflag(
50 "automagic",
51 "InteractiveShell.automagic",
52 """Turn on the auto calling of magic commands. Type %%magic at the
44 53 IPython prompt for more information.""",
45 54 'Turn off the auto calling of magic commands.'
46 55 )
@@ -96,6 +105,37 b' shell_aliases = dict('
96 105 )
97 106 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
98 107
108
109 # -----------------------------------------------------------------------------
110 # Traitlets
111 # -----------------------------------------------------------------------------
112
113
114 class MatplotlibBackendCaselessStrEnum(CaselessStrEnum):
115 """An enum of Matplotlib backend strings where the case should be ignored.
116
117 Prior to Matplotlib 3.9.1 the list of valid backends is hardcoded in
118 pylabtools.backends. After that, Matplotlib manages backends.
119
120 The list of valid backends is determined when it is first needed to avoid
121 wasting unnecessary initialisation time.
122 """
123
124 def __init__(
125 self: CaselessStrEnum[t.Any],
126 default_value: t.Any = Undefined,
127 **kwargs: t.Any,
128 ) -> None:
129 super().__init__(None, default_value=default_value, **kwargs)
130
131 def __getattribute__(self, name):
132 if name == "values" and object.__getattribute__(self, name) is None:
133 from IPython.core.pylabtools import _list_matplotlib_backends_and_gui_loops
134
135 self.values = _list_matplotlib_backends_and_gui_loops()
136 return object.__getattribute__(self, name)
137
138
99 139 #-----------------------------------------------------------------------------
100 140 # Main classes and functions
101 141 #-----------------------------------------------------------------------------
@@ -155,30 +195,31 b' class InteractiveShellApp(Configurable):'
155 195 exec_lines = List(Unicode(),
156 196 help="""lines of code to run at IPython startup."""
157 197 ).tag(config=True)
158 code_to_run = Unicode('',
159 help="Execute the given command string."
160 ).tag(config=True)
161 module_to_run = Unicode('',
162 help="Run the module as a script."
198 code_to_run = Unicode("", help="Execute the given command string.").tag(config=True)
199 module_to_run = Unicode("", help="Run the module as a script.").tag(config=True)
200 gui = CaselessStrEnum(
201 gui_keys,
202 allow_none=True,
203 help="Enable GUI event loop integration with any of {0}.".format(gui_keys),
163 204 ).tag(config=True)
164 gui = CaselessStrEnum(gui_keys, allow_none=True,
165 help="Enable GUI event loop integration with any of {0}.".format(gui_keys)
166 ).tag(config=True)
167 matplotlib = CaselessStrEnum(backend_keys, allow_none=True,
205 matplotlib = MatplotlibBackendCaselessStrEnum(
206 allow_none=True,
168 207 help="""Configure matplotlib for interactive use with
169 the default matplotlib backend."""
208 the default matplotlib backend.""",
170 209 ).tag(config=True)
171 pylab = CaselessStrEnum(backend_keys, allow_none=True,
210 pylab = MatplotlibBackendCaselessStrEnum(
211 allow_none=True,
172 212 help="""Pre-load matplotlib and numpy for interactive use,
173 213 selecting a particular matplotlib backend and loop integration.
174 """
214 """,
175 215 ).tag(config=True)
176 pylab_import_all = Bool(True,
216 pylab_import_all = Bool(
217 True,
177 218 help="""If true, IPython will populate the user namespace with numpy, pylab, etc.
178 219 and an ``import *`` is done from numpy and pylab, when using pylab mode.
179 220
180 221 When False, pylab mode should not import any names into the user namespace.
181 """
222 """,
182 223 ).tag(config=True)
183 224 ignore_cwd = Bool(
184 225 False,
General Comments 0
You need to be logged in to leave comments. Login now