Show More
@@ -1,518 +1,522 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Pylab (matplotlib) support utilities.""" |
|
3 | 3 | |
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | 7 | from io import BytesIO |
|
8 | 8 | from binascii import b2a_base64 |
|
9 | 9 | from functools import partial |
|
10 | 10 | import warnings |
|
11 | 11 | |
|
12 | 12 | from IPython.core.display import _pngxy |
|
13 | 13 | from IPython.utils.decorators import flag_calls |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | # Matplotlib backend resolution functionality moved from IPython to Matplotlib |
|
17 |
# in IPython 8.24 and Matplotlib 3.9. |
|
|
17 | # in IPython 8.24 and Matplotlib 3.9.0. Need to keep `backends` and `backend2gui` | |
|
18 | 18 | # here for earlier Matplotlib and for external backend libraries such as |
|
19 | 19 | # mplcairo that might rely upon it. |
|
20 | 20 | _deprecated_backends = { |
|
21 | 21 | "tk": "TkAgg", |
|
22 | 22 | "gtk": "GTKAgg", |
|
23 | 23 | "gtk3": "GTK3Agg", |
|
24 | 24 | "gtk4": "GTK4Agg", |
|
25 | 25 | "wx": "WXAgg", |
|
26 | 26 | "qt4": "Qt4Agg", |
|
27 | 27 | "qt5": "Qt5Agg", |
|
28 | 28 | "qt6": "QtAgg", |
|
29 | 29 | "qt": "QtAgg", |
|
30 | 30 | "osx": "MacOSX", |
|
31 | 31 | "nbagg": "nbAgg", |
|
32 | 32 | "webagg": "WebAgg", |
|
33 | 33 | "notebook": "nbAgg", |
|
34 | 34 | "agg": "agg", |
|
35 | 35 | "svg": "svg", |
|
36 | 36 | "pdf": "pdf", |
|
37 | 37 | "ps": "ps", |
|
38 | 38 | "inline": "module://matplotlib_inline.backend_inline", |
|
39 | 39 | "ipympl": "module://ipympl.backend_nbagg", |
|
40 | 40 | "widget": "module://ipympl.backend_nbagg", |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | # We also need a reverse backends2guis mapping that will properly choose which |
|
44 | 44 | # GUI support to activate based on the desired matplotlib backend. For the |
|
45 | 45 | # most part it's just a reverse of the above dict, but we also need to add a |
|
46 | 46 | # few others that map to the same GUI manually: |
|
47 | 47 | _deprecated_backend2gui = dict( |
|
48 | 48 | zip(_deprecated_backends.values(), _deprecated_backends.keys()) |
|
49 | 49 | ) |
|
50 | 50 | # In the reverse mapping, there are a few extra valid matplotlib backends that |
|
51 | 51 | # map to the same GUI support |
|
52 | 52 | _deprecated_backend2gui["GTK"] = _deprecated_backend2gui["GTKCairo"] = "gtk" |
|
53 | 53 | _deprecated_backend2gui["GTK3Cairo"] = "gtk3" |
|
54 | 54 | _deprecated_backend2gui["GTK4Cairo"] = "gtk4" |
|
55 | 55 | _deprecated_backend2gui["WX"] = "wx" |
|
56 | 56 | _deprecated_backend2gui["CocoaAgg"] = "osx" |
|
57 | 57 | # There needs to be a hysteresis here as the new QtAgg Matplotlib backend |
|
58 | 58 | # supports either Qt5 or Qt6 and the IPython qt event loop support Qt4, Qt5, |
|
59 | 59 | # and Qt6. |
|
60 | 60 | _deprecated_backend2gui["QtAgg"] = "qt" |
|
61 | 61 | _deprecated_backend2gui["Qt4Agg"] = "qt4" |
|
62 | 62 | _deprecated_backend2gui["Qt5Agg"] = "qt5" |
|
63 | 63 | |
|
64 | 64 | # And some backends that don't need GUI integration |
|
65 | 65 | del _deprecated_backend2gui["nbAgg"] |
|
66 | 66 | del _deprecated_backend2gui["agg"] |
|
67 | 67 | del _deprecated_backend2gui["svg"] |
|
68 | 68 | del _deprecated_backend2gui["pdf"] |
|
69 | 69 | del _deprecated_backend2gui["ps"] |
|
70 | 70 | del _deprecated_backend2gui["module://matplotlib_inline.backend_inline"] |
|
71 | 71 | del _deprecated_backend2gui["module://ipympl.backend_nbagg"] |
|
72 | 72 | |
|
73 | 73 | |
|
74 | 74 | # Deprecated attributes backends and backend2gui mostly following PEP 562. |
|
75 | 75 | def __getattr__(name): |
|
76 | 76 | if name in ("backends", "backend2gui"): |
|
77 | 77 | warnings.warn( |
|
78 | 78 | f"{name} is deprecated since IPython 8.24, backends are managed " |
|
79 | 79 | "in matplotlib and can be externally registered.", |
|
80 | 80 | DeprecationWarning, |
|
81 | 81 | ) |
|
82 | 82 | return globals()[f"_deprecated_{name}"] |
|
83 | 83 | raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |
|
84 | 84 | |
|
85 | 85 | |
|
86 | 86 | #----------------------------------------------------------------------------- |
|
87 | 87 | # Matplotlib utilities |
|
88 | 88 | #----------------------------------------------------------------------------- |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | def getfigs(*fig_nums): |
|
92 | 92 | """Get a list of matplotlib figures by figure numbers. |
|
93 | 93 | |
|
94 | 94 | If no arguments are given, all available figures are returned. If the |
|
95 | 95 | argument list contains references to invalid figures, a warning is printed |
|
96 | 96 | but the function continues pasting further figures. |
|
97 | 97 | |
|
98 | 98 | Parameters |
|
99 | 99 | ---------- |
|
100 | 100 | figs : tuple |
|
101 | 101 | A tuple of ints giving the figure numbers of the figures to return. |
|
102 | 102 | """ |
|
103 | 103 | from matplotlib._pylab_helpers import Gcf |
|
104 | 104 | if not fig_nums: |
|
105 | 105 | fig_managers = Gcf.get_all_fig_managers() |
|
106 | 106 | return [fm.canvas.figure for fm in fig_managers] |
|
107 | 107 | else: |
|
108 | 108 | figs = [] |
|
109 | 109 | for num in fig_nums: |
|
110 | 110 | f = Gcf.figs.get(num) |
|
111 | 111 | if f is None: |
|
112 | 112 | print('Warning: figure %s not available.' % num) |
|
113 | 113 | else: |
|
114 | 114 | figs.append(f.canvas.figure) |
|
115 | 115 | return figs |
|
116 | 116 | |
|
117 | 117 | |
|
118 | 118 | def figsize(sizex, sizey): |
|
119 | 119 | """Set the default figure size to be [sizex, sizey]. |
|
120 | 120 | |
|
121 | 121 | This is just an easy to remember, convenience wrapper that sets:: |
|
122 | 122 | |
|
123 | 123 | matplotlib.rcParams['figure.figsize'] = [sizex, sizey] |
|
124 | 124 | """ |
|
125 | 125 | import matplotlib |
|
126 | 126 | matplotlib.rcParams['figure.figsize'] = [sizex, sizey] |
|
127 | 127 | |
|
128 | 128 | |
|
129 | 129 | def print_figure(fig, fmt="png", bbox_inches="tight", base64=False, **kwargs): |
|
130 | 130 | """Print a figure to an image, and return the resulting file data |
|
131 | 131 | |
|
132 | 132 | Returned data will be bytes unless ``fmt='svg'``, |
|
133 | 133 | in which case it will be unicode. |
|
134 | 134 | |
|
135 | 135 | Any keyword args are passed to fig.canvas.print_figure, |
|
136 | 136 | such as ``quality`` or ``bbox_inches``. |
|
137 | 137 | |
|
138 | 138 | If `base64` is True, return base64-encoded str instead of raw bytes |
|
139 | 139 | for binary-encoded image formats |
|
140 | 140 | |
|
141 | 141 | .. versionadded:: 7.29 |
|
142 | 142 | base64 argument |
|
143 | 143 | """ |
|
144 | 144 | # When there's an empty figure, we shouldn't return anything, otherwise we |
|
145 | 145 | # get big blank areas in the qt console. |
|
146 | 146 | if not fig.axes and not fig.lines: |
|
147 | 147 | return |
|
148 | 148 | |
|
149 | 149 | dpi = fig.dpi |
|
150 | 150 | if fmt == 'retina': |
|
151 | 151 | dpi = dpi * 2 |
|
152 | 152 | fmt = 'png' |
|
153 | 153 | |
|
154 | 154 | # build keyword args |
|
155 | 155 | kw = { |
|
156 | 156 | "format":fmt, |
|
157 | 157 | "facecolor":fig.get_facecolor(), |
|
158 | 158 | "edgecolor":fig.get_edgecolor(), |
|
159 | 159 | "dpi":dpi, |
|
160 | 160 | "bbox_inches":bbox_inches, |
|
161 | 161 | } |
|
162 | 162 | # **kwargs get higher priority |
|
163 | 163 | kw.update(kwargs) |
|
164 | 164 | |
|
165 | 165 | bytes_io = BytesIO() |
|
166 | 166 | if fig.canvas is None: |
|
167 | 167 | from matplotlib.backend_bases import FigureCanvasBase |
|
168 | 168 | FigureCanvasBase(fig) |
|
169 | 169 | |
|
170 | 170 | fig.canvas.print_figure(bytes_io, **kw) |
|
171 | 171 | data = bytes_io.getvalue() |
|
172 | 172 | if fmt == 'svg': |
|
173 | 173 | data = data.decode('utf-8') |
|
174 | 174 | elif base64: |
|
175 | 175 | data = b2a_base64(data, newline=False).decode("ascii") |
|
176 | 176 | return data |
|
177 | 177 | |
|
178 | 178 | def retina_figure(fig, base64=False, **kwargs): |
|
179 | 179 | """format a figure as a pixel-doubled (retina) PNG |
|
180 | 180 | |
|
181 | 181 | If `base64` is True, return base64-encoded str instead of raw bytes |
|
182 | 182 | for binary-encoded image formats |
|
183 | 183 | |
|
184 | 184 | .. versionadded:: 7.29 |
|
185 | 185 | base64 argument |
|
186 | 186 | """ |
|
187 | 187 | pngdata = print_figure(fig, fmt="retina", base64=False, **kwargs) |
|
188 | 188 | # Make sure that retina_figure acts just like print_figure and returns |
|
189 | 189 | # None when the figure is empty. |
|
190 | 190 | if pngdata is None: |
|
191 | 191 | return |
|
192 | 192 | w, h = _pngxy(pngdata) |
|
193 | 193 | metadata = {"width": w//2, "height":h//2} |
|
194 | 194 | if base64: |
|
195 | 195 | pngdata = b2a_base64(pngdata, newline=False).decode("ascii") |
|
196 | 196 | return pngdata, metadata |
|
197 | 197 | |
|
198 | 198 | |
|
199 | 199 | # We need a little factory function here to create the closure where |
|
200 | 200 | # safe_execfile can live. |
|
201 | 201 | def mpl_runner(safe_execfile): |
|
202 | 202 | """Factory to return a matplotlib-enabled runner for %run. |
|
203 | 203 | |
|
204 | 204 | Parameters |
|
205 | 205 | ---------- |
|
206 | 206 | safe_execfile : function |
|
207 | 207 | This must be a function with the same interface as the |
|
208 | 208 | :meth:`safe_execfile` method of IPython. |
|
209 | 209 | |
|
210 | 210 | Returns |
|
211 | 211 | ------- |
|
212 | 212 | A function suitable for use as the ``runner`` argument of the %run magic |
|
213 | 213 | function. |
|
214 | 214 | """ |
|
215 | 215 | |
|
216 | 216 | def mpl_execfile(fname,*where,**kw): |
|
217 | 217 | """matplotlib-aware wrapper around safe_execfile. |
|
218 | 218 | |
|
219 | 219 | Its interface is identical to that of the :func:`execfile` builtin. |
|
220 | 220 | |
|
221 | 221 | This is ultimately a call to execfile(), but wrapped in safeties to |
|
222 | 222 | properly handle interactive rendering.""" |
|
223 | 223 | |
|
224 | 224 | import matplotlib |
|
225 | 225 | import matplotlib.pyplot as plt |
|
226 | 226 | |
|
227 | 227 | #print '*** Matplotlib runner ***' # dbg |
|
228 | 228 | # turn off rendering until end of script |
|
229 | 229 | with matplotlib.rc_context({"interactive": False}): |
|
230 | 230 | safe_execfile(fname, *where, **kw) |
|
231 | 231 | |
|
232 | 232 | if matplotlib.is_interactive(): |
|
233 | 233 | plt.show() |
|
234 | 234 | |
|
235 | 235 | # make rendering call now, if the user tried to do it |
|
236 | 236 | if plt.draw_if_interactive.called: |
|
237 | 237 | plt.draw() |
|
238 | 238 | plt.draw_if_interactive.called = False |
|
239 | 239 | |
|
240 | 240 | # re-draw everything that is stale |
|
241 | 241 | try: |
|
242 | 242 | da = plt.draw_all |
|
243 | 243 | except AttributeError: |
|
244 | 244 | pass |
|
245 | 245 | else: |
|
246 | 246 | da() |
|
247 | 247 | |
|
248 | 248 | return mpl_execfile |
|
249 | 249 | |
|
250 | 250 | |
|
251 | 251 | def _reshow_nbagg_figure(fig): |
|
252 | 252 | """reshow an nbagg figure""" |
|
253 | 253 | try: |
|
254 | 254 | reshow = fig.canvas.manager.reshow |
|
255 | 255 | except AttributeError as e: |
|
256 | 256 | raise NotImplementedError() from e |
|
257 | 257 | else: |
|
258 | 258 | reshow() |
|
259 | 259 | |
|
260 | 260 | |
|
261 | 261 | def select_figure_formats(shell, formats, **kwargs): |
|
262 | 262 | """Select figure formats for the inline backend. |
|
263 | 263 | |
|
264 | 264 | Parameters |
|
265 | 265 | ---------- |
|
266 | 266 | shell : InteractiveShell |
|
267 | 267 | The main IPython instance. |
|
268 | 268 | formats : str or set |
|
269 | 269 | One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'. |
|
270 | 270 | **kwargs : any |
|
271 | 271 | Extra keyword arguments to be passed to fig.canvas.print_figure. |
|
272 | 272 | """ |
|
273 | 273 | import matplotlib |
|
274 | 274 | from matplotlib.figure import Figure |
|
275 | 275 | |
|
276 | 276 | svg_formatter = shell.display_formatter.formatters['image/svg+xml'] |
|
277 | 277 | png_formatter = shell.display_formatter.formatters['image/png'] |
|
278 | 278 | jpg_formatter = shell.display_formatter.formatters['image/jpeg'] |
|
279 | 279 | pdf_formatter = shell.display_formatter.formatters['application/pdf'] |
|
280 | 280 | |
|
281 | 281 | if isinstance(formats, str): |
|
282 | 282 | formats = {formats} |
|
283 | 283 | # cast in case of list / tuple |
|
284 | 284 | formats = set(formats) |
|
285 | 285 | |
|
286 | 286 | [ f.pop(Figure, None) for f in shell.display_formatter.formatters.values() ] |
|
287 | 287 | mplbackend = matplotlib.get_backend().lower() |
|
288 | 288 | if mplbackend in ("nbagg", "ipympl", "widget", "module://ipympl.backend_nbagg"): |
|
289 | 289 | formatter = shell.display_formatter.ipython_display_formatter |
|
290 | 290 | formatter.for_type(Figure, _reshow_nbagg_figure) |
|
291 | 291 | |
|
292 | 292 | supported = {'png', 'png2x', 'retina', 'jpg', 'jpeg', 'svg', 'pdf'} |
|
293 | 293 | bad = formats.difference(supported) |
|
294 | 294 | if bad: |
|
295 | 295 | bs = "%s" % ','.join([repr(f) for f in bad]) |
|
296 | 296 | gs = "%s" % ','.join([repr(f) for f in supported]) |
|
297 | 297 | raise ValueError("supported formats are: %s not %s" % (gs, bs)) |
|
298 | 298 | |
|
299 | 299 | if "png" in formats: |
|
300 | 300 | png_formatter.for_type( |
|
301 | 301 | Figure, partial(print_figure, fmt="png", base64=True, **kwargs) |
|
302 | 302 | ) |
|
303 | 303 | if "retina" in formats or "png2x" in formats: |
|
304 | 304 | png_formatter.for_type(Figure, partial(retina_figure, base64=True, **kwargs)) |
|
305 | 305 | if "jpg" in formats or "jpeg" in formats: |
|
306 | 306 | jpg_formatter.for_type( |
|
307 | 307 | Figure, partial(print_figure, fmt="jpg", base64=True, **kwargs) |
|
308 | 308 | ) |
|
309 | 309 | if "svg" in formats: |
|
310 | 310 | svg_formatter.for_type(Figure, partial(print_figure, fmt="svg", **kwargs)) |
|
311 | 311 | if "pdf" in formats: |
|
312 | 312 | pdf_formatter.for_type( |
|
313 | 313 | Figure, partial(print_figure, fmt="pdf", base64=True, **kwargs) |
|
314 | 314 | ) |
|
315 | 315 | |
|
316 | 316 | #----------------------------------------------------------------------------- |
|
317 | 317 | # Code for initializing matplotlib and importing pylab |
|
318 | 318 | #----------------------------------------------------------------------------- |
|
319 | 319 | |
|
320 | 320 | |
|
321 | 321 | def find_gui_and_backend(gui=None, gui_select=None): |
|
322 | 322 | """Given a gui string return the gui and mpl backend. |
|
323 | 323 | |
|
324 | 324 | Parameters |
|
325 | 325 | ---------- |
|
326 | 326 | gui : str |
|
327 | 327 | Can be one of ('tk','gtk','wx','qt','qt4','inline','agg'). |
|
328 | 328 | gui_select : str |
|
329 | 329 | Can be one of ('tk','gtk','wx','qt','qt4','inline'). |
|
330 | 330 | This is any gui already selected by the shell. |
|
331 | 331 | |
|
332 | 332 | Returns |
|
333 | 333 | ------- |
|
334 | 334 | A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg', |
|
335 | 335 | 'WXAgg','Qt4Agg','module://matplotlib_inline.backend_inline','agg'). |
|
336 | 336 | """ |
|
337 | 337 | |
|
338 | 338 | import matplotlib |
|
339 | 339 | |
|
340 | 340 | if _matplotlib_manages_backends(): |
|
341 | 341 | backend_registry = matplotlib.backends.registry.backend_registry |
|
342 | 342 | |
|
343 | 343 | # gui argument may be a gui event loop or may be a backend name. |
|
344 | 344 | if gui in ("auto", None): |
|
345 | 345 | backend = matplotlib.rcParamsOrig["backend"] |
|
346 | 346 | backend, gui = backend_registry.resolve_backend(backend) |
|
347 | 347 | else: |
|
348 | 348 | backend, gui = backend_registry.resolve_gui_or_backend(gui) |
|
349 | 349 | |
|
350 | 350 | return gui, backend |
|
351 | 351 | |
|
352 | 352 | # Fallback to previous behaviour (Matplotlib < 3.9) |
|
353 | 353 | mpl_version_info = getattr(matplotlib, "__version_info__", (0, 0)) |
|
354 | 354 | has_unified_qt_backend = mpl_version_info >= (3, 5) |
|
355 | 355 | |
|
356 | 356 | from IPython.core.pylabtools import backends |
|
357 | 357 | |
|
358 | 358 | backends_ = dict(backends) |
|
359 | 359 | if not has_unified_qt_backend: |
|
360 | 360 | backends_["qt"] = "qt5agg" |
|
361 | 361 | |
|
362 | 362 | if gui and gui != 'auto': |
|
363 | 363 | # select backend based on requested gui |
|
364 | 364 | backend = backends_[gui] |
|
365 | 365 | if gui == 'agg': |
|
366 | 366 | gui = None |
|
367 | 367 | else: |
|
368 | 368 | # We need to read the backend from the original data structure, *not* |
|
369 | 369 | # from mpl.rcParams, since a prior invocation of %matplotlib may have |
|
370 | 370 | # overwritten that. |
|
371 | 371 | # WARNING: this assumes matplotlib 1.1 or newer!! |
|
372 | 372 | backend = matplotlib.rcParamsOrig['backend'] |
|
373 | 373 | # In this case, we need to find what the appropriate gui selection call |
|
374 | 374 | # should be for IPython, so we can activate inputhook accordingly |
|
375 | 375 | from IPython.core.pylabtools import backend2gui |
|
376 | 376 | gui = backend2gui.get(backend, None) |
|
377 | 377 | |
|
378 | 378 | # If we have already had a gui active, we need it and inline are the |
|
379 | 379 | # ones allowed. |
|
380 | 380 | if gui_select and gui != gui_select: |
|
381 | 381 | gui = gui_select |
|
382 | 382 | backend = backends_[gui] |
|
383 | 383 | |
|
384 | 384 | # Matplotlib before _matplotlib_manages_backends() can return "inline" for |
|
385 | 385 | # no gui event loop rather than the None that IPython >= 8.24.0 expects. |
|
386 | 386 | if gui == "inline": |
|
387 | 387 | gui = None |
|
388 | 388 | |
|
389 | 389 | return gui, backend |
|
390 | 390 | |
|
391 | 391 | |
|
392 | 392 | def activate_matplotlib(backend): |
|
393 | 393 | """Activate the given backend and set interactive to True.""" |
|
394 | 394 | |
|
395 | 395 | import matplotlib |
|
396 | 396 | matplotlib.interactive(True) |
|
397 | 397 | |
|
398 | 398 | # Matplotlib had a bug where even switch_backend could not force |
|
399 | 399 | # the rcParam to update. This needs to be set *before* the module |
|
400 | 400 | # magic of switch_backend(). |
|
401 | 401 | matplotlib.rcParams['backend'] = backend |
|
402 | 402 | |
|
403 | 403 | # Due to circular imports, pyplot may be only partially initialised |
|
404 | 404 | # when this function runs. |
|
405 | 405 | # So avoid needing matplotlib attribute-lookup to access pyplot. |
|
406 | 406 | from matplotlib import pyplot as plt |
|
407 | 407 | |
|
408 | 408 | plt.switch_backend(backend) |
|
409 | 409 | |
|
410 | 410 | plt.show._needmain = False |
|
411 | 411 | # We need to detect at runtime whether show() is called by the user. |
|
412 | 412 | # For this, we wrap it into a decorator which adds a 'called' flag. |
|
413 | 413 | plt.draw_if_interactive = flag_calls(plt.draw_if_interactive) |
|
414 | 414 | |
|
415 | 415 | |
|
416 | 416 | def import_pylab(user_ns, import_all=True): |
|
417 | 417 | """Populate the namespace with pylab-related values. |
|
418 | 418 | |
|
419 | 419 | Imports matplotlib, pylab, numpy, and everything from pylab and numpy. |
|
420 | 420 | |
|
421 | 421 | Also imports a few names from IPython (figsize, display, getfigs) |
|
422 | 422 | |
|
423 | 423 | """ |
|
424 | 424 | |
|
425 | 425 | # Import numpy as np/pyplot as plt are conventions we're trying to |
|
426 | 426 | # somewhat standardize on. Making them available to users by default |
|
427 | 427 | # will greatly help this. |
|
428 | 428 | s = ("import numpy\n" |
|
429 | 429 | "import matplotlib\n" |
|
430 | 430 | "from matplotlib import pylab, mlab, pyplot\n" |
|
431 | 431 | "np = numpy\n" |
|
432 | 432 | "plt = pyplot\n" |
|
433 | 433 | ) |
|
434 | 434 | exec(s, user_ns) |
|
435 | 435 | |
|
436 | 436 | if import_all: |
|
437 | 437 | s = ("from matplotlib.pylab import *\n" |
|
438 | 438 | "from numpy import *\n") |
|
439 | 439 | exec(s, user_ns) |
|
440 | 440 | |
|
441 | 441 | # IPython symbols to add |
|
442 | 442 | user_ns['figsize'] = figsize |
|
443 | 443 | from IPython.display import display |
|
444 | 444 | # Add display and getfigs to the user's namespace |
|
445 | 445 | user_ns['display'] = display |
|
446 | 446 | user_ns['getfigs'] = getfigs |
|
447 | 447 | |
|
448 | 448 | |
|
449 | 449 | def configure_inline_support(shell, backend): |
|
450 | 450 | """ |
|
451 | 451 | .. deprecated:: 7.23 |
|
452 | 452 | |
|
453 | 453 | use `matplotlib_inline.backend_inline.configure_inline_support()` |
|
454 | 454 | |
|
455 | 455 | Configure an IPython shell object for matplotlib use. |
|
456 | 456 | |
|
457 | 457 | Parameters |
|
458 | 458 | ---------- |
|
459 | 459 | shell : InteractiveShell instance |
|
460 | 460 | backend : matplotlib backend |
|
461 | 461 | """ |
|
462 | 462 | warnings.warn( |
|
463 | 463 | "`configure_inline_support` is deprecated since IPython 7.23, directly " |
|
464 | 464 | "use `matplotlib_inline.backend_inline.configure_inline_support()`", |
|
465 | 465 | DeprecationWarning, |
|
466 | 466 | stacklevel=2, |
|
467 | 467 | ) |
|
468 | 468 | |
|
469 | 469 | from matplotlib_inline.backend_inline import ( |
|
470 | 470 | configure_inline_support as configure_inline_support_orig, |
|
471 | 471 | ) |
|
472 | 472 | |
|
473 | 473 | configure_inline_support_orig(shell, backend) |
|
474 | 474 | |
|
475 | 475 | |
|
476 | 476 | # Determine if Matplotlib manages backends only if needed, and cache result. |
|
477 | 477 | # Do not read this directly, instead use _matplotlib_manages_backends(). |
|
478 | 478 | _matplotlib_manages_backends_value: bool | None = None |
|
479 | 479 | |
|
480 | 480 | |
|
481 | 481 | def _matplotlib_manages_backends() -> bool: |
|
482 | 482 | """Return True if Matplotlib manages backends, False otherwise. |
|
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 | 486 | member functions resolve_gui_or_backend, resolve_backend, list_all, and |
|
487 | 487 | list_gui_frameworks. |
|
488 | ||
|
489 | This function can be removed as it will always return True when Python | |
|
490 | 3.12, the latest version supported by Matplotlib < 3.9, reaches | |
|
491 | end-of-life in late 2028. | |
|
488 | 492 | """ |
|
489 | 493 | global _matplotlib_manages_backends_value |
|
490 | 494 | if _matplotlib_manages_backends_value is None: |
|
491 | 495 | try: |
|
492 | 496 | from matplotlib.backends.registry import backend_registry |
|
493 | 497 | |
|
494 | 498 | _matplotlib_manages_backends_value = hasattr( |
|
495 | 499 | backend_registry, "resolve_gui_or_backend" |
|
496 | 500 | ) |
|
497 | 501 | except ImportError: |
|
498 | 502 | _matplotlib_manages_backends_value = False |
|
499 | 503 | |
|
500 | 504 | return _matplotlib_manages_backends_value |
|
501 | 505 | |
|
502 | 506 | |
|
503 | 507 | def _list_matplotlib_backends_and_gui_loops() -> list[str]: |
|
504 | 508 | """Return list of all Matplotlib backends and GUI event loops. |
|
505 | 509 | |
|
506 | 510 | This is the list returned by |
|
507 | 511 | %matplotlib --list |
|
508 | 512 | """ |
|
509 | 513 | if _matplotlib_manages_backends(): |
|
510 | 514 | from matplotlib.backends.registry import backend_registry |
|
511 | 515 | |
|
512 | 516 | ret = backend_registry.list_all() + backend_registry.list_gui_frameworks() |
|
513 | 517 | else: |
|
514 | 518 | from IPython.core import pylabtools |
|
515 | 519 | |
|
516 | 520 | ret = list(pylabtools.backends.keys()) |
|
517 | 521 | |
|
518 | 522 | return sorted(["auto"] + ret) |
@@ -1,491 +1,495 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | A mixin for :class:`~IPython.core.application.Application` classes that |
|
4 | 4 | launch InteractiveShell instances, load extensions, etc. |
|
5 | 5 | """ |
|
6 | 6 | |
|
7 | 7 | # Copyright (c) IPython Development Team. |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | |
|
10 | 10 | import glob |
|
11 | 11 | from itertools import chain |
|
12 | 12 | import os |
|
13 | 13 | import sys |
|
14 | 14 | import typing as t |
|
15 | 15 | |
|
16 | 16 | from traitlets.config.application import boolean_flag |
|
17 | 17 | from traitlets.config.configurable import Configurable |
|
18 | 18 | from traitlets.config.loader import Config |
|
19 | 19 | from IPython.core.application import SYSTEM_CONFIG_DIRS, ENV_CONFIG_DIRS |
|
20 | 20 | from IPython.utils.contexts import preserve_keys |
|
21 | 21 | from IPython.utils.path import filefind |
|
22 | 22 | from traitlets import ( |
|
23 | 23 | Unicode, |
|
24 | 24 | Instance, |
|
25 | 25 | List, |
|
26 | 26 | Bool, |
|
27 | 27 | CaselessStrEnum, |
|
28 | 28 | observe, |
|
29 | 29 | DottedObjectName, |
|
30 | 30 | Undefined, |
|
31 | 31 | ) |
|
32 | 32 | from IPython.terminal import pt_inputhooks |
|
33 | 33 | |
|
34 | 34 | # ----------------------------------------------------------------------------- |
|
35 | 35 | # Aliases and Flags |
|
36 | 36 | # ----------------------------------------------------------------------------- |
|
37 | 37 | |
|
38 | 38 | gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases)) |
|
39 | 39 | |
|
40 | 40 | shell_flags = {} |
|
41 | 41 | |
|
42 | 42 | addflag = lambda *args: shell_flags.update(boolean_flag(*args)) |
|
43 | 43 | addflag( |
|
44 | 44 | "autoindent", |
|
45 | 45 | "InteractiveShell.autoindent", |
|
46 | 46 | "Turn on autoindenting.", |
|
47 | 47 | "Turn off autoindenting.", |
|
48 | 48 | ) |
|
49 | 49 | addflag( |
|
50 | 50 | "automagic", |
|
51 | 51 | "InteractiveShell.automagic", |
|
52 | 52 | """Turn on the auto calling of magic commands. Type %%magic at the |
|
53 | 53 | IPython prompt for more information.""", |
|
54 | 54 | 'Turn off the auto calling of magic commands.' |
|
55 | 55 | ) |
|
56 | 56 | addflag('pdb', 'InteractiveShell.pdb', |
|
57 | 57 | "Enable auto calling the pdb debugger after every exception.", |
|
58 | 58 | "Disable auto calling the pdb debugger after every exception." |
|
59 | 59 | ) |
|
60 | 60 | addflag('pprint', 'PlainTextFormatter.pprint', |
|
61 | 61 | "Enable auto pretty printing of results.", |
|
62 | 62 | "Disable auto pretty printing of results." |
|
63 | 63 | ) |
|
64 | 64 | addflag('color-info', 'InteractiveShell.color_info', |
|
65 | 65 | """IPython can display information about objects via a set of functions, |
|
66 | 66 | and optionally can use colors for this, syntax highlighting |
|
67 | 67 | source code and various other elements. This is on by default, but can cause |
|
68 | 68 | problems with some pagers. If you see such problems, you can disable the |
|
69 | 69 | colours.""", |
|
70 | 70 | "Disable using colors for info related things." |
|
71 | 71 | ) |
|
72 | 72 | addflag('ignore-cwd', 'InteractiveShellApp.ignore_cwd', |
|
73 | 73 | "Exclude the current working directory from sys.path", |
|
74 | 74 | "Include the current working directory in sys.path", |
|
75 | 75 | ) |
|
76 | 76 | nosep_config = Config() |
|
77 | 77 | nosep_config.InteractiveShell.separate_in = '' |
|
78 | 78 | nosep_config.InteractiveShell.separate_out = '' |
|
79 | 79 | nosep_config.InteractiveShell.separate_out2 = '' |
|
80 | 80 | |
|
81 | 81 | shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.") |
|
82 | 82 | shell_flags['pylab'] = ( |
|
83 | 83 | {'InteractiveShellApp' : {'pylab' : 'auto'}}, |
|
84 | 84 | """Pre-load matplotlib and numpy for interactive use with |
|
85 |
the default matplotlib backend. |
|
|
85 | the default matplotlib backend. The exact options available | |
|
86 | depend on what Matplotlib provides at runtime.""", | |
|
86 | 87 | ) |
|
87 | 88 | shell_flags['matplotlib'] = ( |
|
88 | 89 | {'InteractiveShellApp' : {'matplotlib' : 'auto'}}, |
|
89 | 90 | """Configure matplotlib for interactive use with |
|
90 |
the default matplotlib backend. |
|
|
91 | the default matplotlib backend. The exact options available | |
|
92 | depend on what Matplotlib provides at runtime.""", | |
|
91 | 93 | ) |
|
92 | 94 | |
|
93 | 95 | # it's possible we don't want short aliases for *all* of these: |
|
94 | 96 | shell_aliases = dict( |
|
95 | 97 | autocall='InteractiveShell.autocall', |
|
96 | 98 | colors='InteractiveShell.colors', |
|
97 | 99 | logfile='InteractiveShell.logfile', |
|
98 | 100 | logappend='InteractiveShell.logappend', |
|
99 | 101 | c='InteractiveShellApp.code_to_run', |
|
100 | 102 | m='InteractiveShellApp.module_to_run', |
|
101 | 103 | ext="InteractiveShellApp.extra_extensions", |
|
102 | 104 | gui='InteractiveShellApp.gui', |
|
103 | 105 | pylab='InteractiveShellApp.pylab', |
|
104 | 106 | matplotlib='InteractiveShellApp.matplotlib', |
|
105 | 107 | ) |
|
106 | 108 | shell_aliases['cache-size'] = 'InteractiveShell.cache_size' |
|
107 | 109 | |
|
108 | 110 | |
|
109 | 111 | # ----------------------------------------------------------------------------- |
|
110 | 112 | # Traitlets |
|
111 | 113 | # ----------------------------------------------------------------------------- |
|
112 | 114 | |
|
113 | 115 | |
|
114 | 116 | class MatplotlibBackendCaselessStrEnum(CaselessStrEnum): |
|
115 | 117 | """An enum of Matplotlib backend strings where the case should be ignored. |
|
116 | 118 | |
|
117 |
Prior to Matplotlib 3.9. |
|
|
119 | Prior to Matplotlib 3.9.0 the list of valid backends is hardcoded in | |
|
118 | 120 | pylabtools.backends. After that, Matplotlib manages backends. |
|
119 | 121 | |
|
120 | 122 | The list of valid backends is determined when it is first needed to avoid |
|
121 | 123 | wasting unnecessary initialisation time. |
|
122 | 124 | """ |
|
123 | 125 | |
|
124 | 126 | def __init__( |
|
125 | 127 | self: CaselessStrEnum[t.Any], |
|
126 | 128 | default_value: t.Any = Undefined, |
|
127 | 129 | **kwargs: t.Any, |
|
128 | 130 | ) -> None: |
|
129 | 131 | super().__init__(None, default_value=default_value, **kwargs) |
|
130 | 132 | |
|
131 | 133 | def __getattribute__(self, name): |
|
132 | 134 | if name == "values" and object.__getattribute__(self, name) is None: |
|
133 | 135 | from IPython.core.pylabtools import _list_matplotlib_backends_and_gui_loops |
|
134 | 136 | |
|
135 | 137 | self.values = _list_matplotlib_backends_and_gui_loops() |
|
136 | 138 | return object.__getattribute__(self, name) |
|
137 | 139 | |
|
138 | 140 | |
|
139 | 141 | #----------------------------------------------------------------------------- |
|
140 | 142 | # Main classes and functions |
|
141 | 143 | #----------------------------------------------------------------------------- |
|
142 | 144 | |
|
143 | 145 | class InteractiveShellApp(Configurable): |
|
144 | 146 | """A Mixin for applications that start InteractiveShell instances. |
|
145 | 147 | |
|
146 | 148 | Provides configurables for loading extensions and executing files |
|
147 | 149 | as part of configuring a Shell environment. |
|
148 | 150 | |
|
149 | 151 | The following methods should be called by the :meth:`initialize` method |
|
150 | 152 | of the subclass: |
|
151 | 153 | |
|
152 | 154 | - :meth:`init_path` |
|
153 | 155 | - :meth:`init_shell` (to be implemented by the subclass) |
|
154 | 156 | - :meth:`init_gui_pylab` |
|
155 | 157 | - :meth:`init_extensions` |
|
156 | 158 | - :meth:`init_code` |
|
157 | 159 | """ |
|
158 | 160 | extensions = List(Unicode(), |
|
159 | 161 | help="A list of dotted module names of IPython extensions to load." |
|
160 | 162 | ).tag(config=True) |
|
161 | 163 | |
|
162 | 164 | extra_extensions = List( |
|
163 | 165 | DottedObjectName(), |
|
164 | 166 | help=""" |
|
165 | 167 | Dotted module name(s) of one or more IPython extensions to load. |
|
166 | 168 | |
|
167 | 169 | For specifying extra extensions to load on the command-line. |
|
168 | 170 | |
|
169 | 171 | .. versionadded:: 7.10 |
|
170 | 172 | """, |
|
171 | 173 | ).tag(config=True) |
|
172 | 174 | |
|
173 | 175 | reraise_ipython_extension_failures = Bool(False, |
|
174 | 176 | help="Reraise exceptions encountered loading IPython extensions?", |
|
175 | 177 | ).tag(config=True) |
|
176 | 178 | |
|
177 | 179 | # Extensions that are always loaded (not configurable) |
|
178 | 180 | default_extensions = List(Unicode(), [u'storemagic']).tag(config=False) |
|
179 | 181 | |
|
180 | 182 | hide_initial_ns = Bool(True, |
|
181 | 183 | help="""Should variables loaded at startup (by startup files, exec_lines, etc.) |
|
182 | 184 | be hidden from tools like %who?""" |
|
183 | 185 | ).tag(config=True) |
|
184 | 186 | |
|
185 | 187 | exec_files = List(Unicode(), |
|
186 | 188 | help="""List of files to run at IPython startup.""" |
|
187 | 189 | ).tag(config=True) |
|
188 | 190 | exec_PYTHONSTARTUP = Bool(True, |
|
189 | 191 | help="""Run the file referenced by the PYTHONSTARTUP environment |
|
190 | 192 | variable at IPython startup.""" |
|
191 | 193 | ).tag(config=True) |
|
192 | 194 | file_to_run = Unicode('', |
|
193 | 195 | help="""A file to be run""").tag(config=True) |
|
194 | 196 | |
|
195 | 197 | exec_lines = List(Unicode(), |
|
196 | 198 | help="""lines of code to run at IPython startup.""" |
|
197 | 199 | ).tag(config=True) |
|
198 | 200 | code_to_run = Unicode("", help="Execute the given command string.").tag(config=True) |
|
199 | 201 | module_to_run = Unicode("", help="Run the module as a script.").tag(config=True) |
|
200 | 202 | gui = CaselessStrEnum( |
|
201 | 203 | gui_keys, |
|
202 | 204 | allow_none=True, |
|
203 | 205 | help="Enable GUI event loop integration with any of {0}.".format(gui_keys), |
|
204 | 206 | ).tag(config=True) |
|
205 | 207 | matplotlib = MatplotlibBackendCaselessStrEnum( |
|
206 | 208 | allow_none=True, |
|
207 | 209 | help="""Configure matplotlib for interactive use with |
|
208 |
the default matplotlib backend. |
|
|
210 | the default matplotlib backend. The exact options available | |
|
211 | depend on what Matplotlib provides at runtime.""", | |
|
209 | 212 | ).tag(config=True) |
|
210 | 213 | pylab = MatplotlibBackendCaselessStrEnum( |
|
211 | 214 | allow_none=True, |
|
212 | 215 | help="""Pre-load matplotlib and numpy for interactive use, |
|
213 | 216 | selecting a particular matplotlib backend and loop integration. |
|
217 | The exact options available depend on what Matplotlib provides at runtime. | |
|
214 | 218 | """, |
|
215 | 219 | ).tag(config=True) |
|
216 | 220 | pylab_import_all = Bool( |
|
217 | 221 | True, |
|
218 | 222 | help="""If true, IPython will populate the user namespace with numpy, pylab, etc. |
|
219 | 223 | and an ``import *`` is done from numpy and pylab, when using pylab mode. |
|
220 | 224 | |
|
221 | 225 | When False, pylab mode should not import any names into the user namespace. |
|
222 | 226 | """, |
|
223 | 227 | ).tag(config=True) |
|
224 | 228 | ignore_cwd = Bool( |
|
225 | 229 | False, |
|
226 | 230 | help="""If True, IPython will not add the current working directory to sys.path. |
|
227 | 231 | When False, the current working directory is added to sys.path, allowing imports |
|
228 | 232 | of modules defined in the current directory.""" |
|
229 | 233 | ).tag(config=True) |
|
230 | 234 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', |
|
231 | 235 | allow_none=True) |
|
232 | 236 | # whether interact-loop should start |
|
233 | 237 | interact = Bool(True) |
|
234 | 238 | |
|
235 | 239 | user_ns = Instance(dict, args=None, allow_none=True) |
|
236 | 240 | @observe('user_ns') |
|
237 | 241 | def _user_ns_changed(self, change): |
|
238 | 242 | if self.shell is not None: |
|
239 | 243 | self.shell.user_ns = change['new'] |
|
240 | 244 | self.shell.init_user_ns() |
|
241 | 245 | |
|
242 | 246 | def init_path(self): |
|
243 | 247 | """Add current working directory, '', to sys.path |
|
244 | 248 | |
|
245 | 249 | Unlike Python's default, we insert before the first `site-packages` |
|
246 | 250 | or `dist-packages` directory, |
|
247 | 251 | so that it is after the standard library. |
|
248 | 252 | |
|
249 | 253 | .. versionchanged:: 7.2 |
|
250 | 254 | Try to insert after the standard library, instead of first. |
|
251 | 255 | .. versionchanged:: 8.0 |
|
252 | 256 | Allow optionally not including the current directory in sys.path |
|
253 | 257 | """ |
|
254 | 258 | if '' in sys.path or self.ignore_cwd: |
|
255 | 259 | return |
|
256 | 260 | for idx, path in enumerate(sys.path): |
|
257 | 261 | parent, last_part = os.path.split(path) |
|
258 | 262 | if last_part in {'site-packages', 'dist-packages'}: |
|
259 | 263 | break |
|
260 | 264 | else: |
|
261 | 265 | # no site-packages or dist-packages found (?!) |
|
262 | 266 | # back to original behavior of inserting at the front |
|
263 | 267 | idx = 0 |
|
264 | 268 | sys.path.insert(idx, '') |
|
265 | 269 | |
|
266 | 270 | def init_shell(self): |
|
267 | 271 | raise NotImplementedError("Override in subclasses") |
|
268 | 272 | |
|
269 | 273 | def init_gui_pylab(self): |
|
270 | 274 | """Enable GUI event loop integration, taking pylab into account.""" |
|
271 | 275 | enable = False |
|
272 | 276 | shell = self.shell |
|
273 | 277 | if self.pylab: |
|
274 | 278 | enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all) |
|
275 | 279 | key = self.pylab |
|
276 | 280 | elif self.matplotlib: |
|
277 | 281 | enable = shell.enable_matplotlib |
|
278 | 282 | key = self.matplotlib |
|
279 | 283 | elif self.gui: |
|
280 | 284 | enable = shell.enable_gui |
|
281 | 285 | key = self.gui |
|
282 | 286 | |
|
283 | 287 | if not enable: |
|
284 | 288 | return |
|
285 | 289 | |
|
286 | 290 | try: |
|
287 | 291 | r = enable(key) |
|
288 | 292 | except ImportError: |
|
289 | 293 | self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?") |
|
290 | 294 | self.shell.showtraceback() |
|
291 | 295 | return |
|
292 | 296 | except Exception: |
|
293 | 297 | self.log.warning("GUI event loop or pylab initialization failed") |
|
294 | 298 | self.shell.showtraceback() |
|
295 | 299 | return |
|
296 | 300 | |
|
297 | 301 | if isinstance(r, tuple): |
|
298 | 302 | gui, backend = r[:2] |
|
299 | 303 | self.log.info("Enabling GUI event loop integration, " |
|
300 | 304 | "eventloop=%s, matplotlib=%s", gui, backend) |
|
301 | 305 | if key == "auto": |
|
302 | 306 | print("Using matplotlib backend: %s" % backend) |
|
303 | 307 | else: |
|
304 | 308 | gui = r |
|
305 | 309 | self.log.info("Enabling GUI event loop integration, " |
|
306 | 310 | "eventloop=%s", gui) |
|
307 | 311 | |
|
308 | 312 | def init_extensions(self): |
|
309 | 313 | """Load all IPython extensions in IPythonApp.extensions. |
|
310 | 314 | |
|
311 | 315 | This uses the :meth:`ExtensionManager.load_extensions` to load all |
|
312 | 316 | the extensions listed in ``self.extensions``. |
|
313 | 317 | """ |
|
314 | 318 | try: |
|
315 | 319 | self.log.debug("Loading IPython extensions...") |
|
316 | 320 | extensions = ( |
|
317 | 321 | self.default_extensions + self.extensions + self.extra_extensions |
|
318 | 322 | ) |
|
319 | 323 | for ext in extensions: |
|
320 | 324 | try: |
|
321 | 325 | self.log.info("Loading IPython extension: %s", ext) |
|
322 | 326 | self.shell.extension_manager.load_extension(ext) |
|
323 | 327 | except: |
|
324 | 328 | if self.reraise_ipython_extension_failures: |
|
325 | 329 | raise |
|
326 | 330 | msg = ("Error in loading extension: {ext}\n" |
|
327 | 331 | "Check your config files in {location}".format( |
|
328 | 332 | ext=ext, |
|
329 | 333 | location=self.profile_dir.location |
|
330 | 334 | )) |
|
331 | 335 | self.log.warning(msg, exc_info=True) |
|
332 | 336 | except: |
|
333 | 337 | if self.reraise_ipython_extension_failures: |
|
334 | 338 | raise |
|
335 | 339 | self.log.warning("Unknown error in loading extensions:", exc_info=True) |
|
336 | 340 | |
|
337 | 341 | def init_code(self): |
|
338 | 342 | """run the pre-flight code, specified via exec_lines""" |
|
339 | 343 | self._run_startup_files() |
|
340 | 344 | self._run_exec_lines() |
|
341 | 345 | self._run_exec_files() |
|
342 | 346 | |
|
343 | 347 | # Hide variables defined here from %who etc. |
|
344 | 348 | if self.hide_initial_ns: |
|
345 | 349 | self.shell.user_ns_hidden.update(self.shell.user_ns) |
|
346 | 350 | |
|
347 | 351 | # command-line execution (ipython -i script.py, ipython -m module) |
|
348 | 352 | # should *not* be excluded from %whos |
|
349 | 353 | self._run_cmd_line_code() |
|
350 | 354 | self._run_module() |
|
351 | 355 | |
|
352 | 356 | # flush output, so itwon't be attached to the first cell |
|
353 | 357 | sys.stdout.flush() |
|
354 | 358 | sys.stderr.flush() |
|
355 | 359 | self.shell._sys_modules_keys = set(sys.modules.keys()) |
|
356 | 360 | |
|
357 | 361 | def _run_exec_lines(self): |
|
358 | 362 | """Run lines of code in IPythonApp.exec_lines in the user's namespace.""" |
|
359 | 363 | if not self.exec_lines: |
|
360 | 364 | return |
|
361 | 365 | try: |
|
362 | 366 | self.log.debug("Running code from IPythonApp.exec_lines...") |
|
363 | 367 | for line in self.exec_lines: |
|
364 | 368 | try: |
|
365 | 369 | self.log.info("Running code in user namespace: %s" % |
|
366 | 370 | line) |
|
367 | 371 | self.shell.run_cell(line, store_history=False) |
|
368 | 372 | except: |
|
369 | 373 | self.log.warning("Error in executing line in user " |
|
370 | 374 | "namespace: %s" % line) |
|
371 | 375 | self.shell.showtraceback() |
|
372 | 376 | except: |
|
373 | 377 | self.log.warning("Unknown error in handling IPythonApp.exec_lines:") |
|
374 | 378 | self.shell.showtraceback() |
|
375 | 379 | |
|
376 | 380 | def _exec_file(self, fname, shell_futures=False): |
|
377 | 381 | try: |
|
378 | 382 | full_filename = filefind(fname, [u'.', self.ipython_dir]) |
|
379 | 383 | except IOError: |
|
380 | 384 | self.log.warning("File not found: %r"%fname) |
|
381 | 385 | return |
|
382 | 386 | # Make sure that the running script gets a proper sys.argv as if it |
|
383 | 387 | # were run from a system shell. |
|
384 | 388 | save_argv = sys.argv |
|
385 | 389 | sys.argv = [full_filename] + self.extra_args[1:] |
|
386 | 390 | try: |
|
387 | 391 | if os.path.isfile(full_filename): |
|
388 | 392 | self.log.info("Running file in user namespace: %s" % |
|
389 | 393 | full_filename) |
|
390 | 394 | # Ensure that __file__ is always defined to match Python |
|
391 | 395 | # behavior. |
|
392 | 396 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
393 | 397 | self.shell.user_ns['__file__'] = fname |
|
394 | 398 | if full_filename.endswith('.ipy') or full_filename.endswith('.ipynb'): |
|
395 | 399 | self.shell.safe_execfile_ipy(full_filename, |
|
396 | 400 | shell_futures=shell_futures) |
|
397 | 401 | else: |
|
398 | 402 | # default to python, even without extension |
|
399 | 403 | self.shell.safe_execfile(full_filename, |
|
400 | 404 | self.shell.user_ns, |
|
401 | 405 | shell_futures=shell_futures, |
|
402 | 406 | raise_exceptions=True) |
|
403 | 407 | finally: |
|
404 | 408 | sys.argv = save_argv |
|
405 | 409 | |
|
406 | 410 | def _run_startup_files(self): |
|
407 | 411 | """Run files from profile startup directory""" |
|
408 | 412 | startup_dirs = [self.profile_dir.startup_dir] + [ |
|
409 | 413 | os.path.join(p, 'startup') for p in chain(ENV_CONFIG_DIRS, SYSTEM_CONFIG_DIRS) |
|
410 | 414 | ] |
|
411 | 415 | startup_files = [] |
|
412 | 416 | |
|
413 | 417 | if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \ |
|
414 | 418 | not (self.file_to_run or self.code_to_run or self.module_to_run): |
|
415 | 419 | python_startup = os.environ['PYTHONSTARTUP'] |
|
416 | 420 | self.log.debug("Running PYTHONSTARTUP file %s...", python_startup) |
|
417 | 421 | try: |
|
418 | 422 | self._exec_file(python_startup) |
|
419 | 423 | except: |
|
420 | 424 | self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup) |
|
421 | 425 | self.shell.showtraceback() |
|
422 | 426 | for startup_dir in startup_dirs[::-1]: |
|
423 | 427 | startup_files += glob.glob(os.path.join(startup_dir, '*.py')) |
|
424 | 428 | startup_files += glob.glob(os.path.join(startup_dir, '*.ipy')) |
|
425 | 429 | if not startup_files: |
|
426 | 430 | return |
|
427 | 431 | |
|
428 | 432 | self.log.debug("Running startup files from %s...", startup_dir) |
|
429 | 433 | try: |
|
430 | 434 | for fname in sorted(startup_files): |
|
431 | 435 | self._exec_file(fname) |
|
432 | 436 | except: |
|
433 | 437 | self.log.warning("Unknown error in handling startup files:") |
|
434 | 438 | self.shell.showtraceback() |
|
435 | 439 | |
|
436 | 440 | def _run_exec_files(self): |
|
437 | 441 | """Run files from IPythonApp.exec_files""" |
|
438 | 442 | if not self.exec_files: |
|
439 | 443 | return |
|
440 | 444 | |
|
441 | 445 | self.log.debug("Running files in IPythonApp.exec_files...") |
|
442 | 446 | try: |
|
443 | 447 | for fname in self.exec_files: |
|
444 | 448 | self._exec_file(fname) |
|
445 | 449 | except: |
|
446 | 450 | self.log.warning("Unknown error in handling IPythonApp.exec_files:") |
|
447 | 451 | self.shell.showtraceback() |
|
448 | 452 | |
|
449 | 453 | def _run_cmd_line_code(self): |
|
450 | 454 | """Run code or file specified at the command-line""" |
|
451 | 455 | if self.code_to_run: |
|
452 | 456 | line = self.code_to_run |
|
453 | 457 | try: |
|
454 | 458 | self.log.info("Running code given at command line (c=): %s" % |
|
455 | 459 | line) |
|
456 | 460 | self.shell.run_cell(line, store_history=False) |
|
457 | 461 | except: |
|
458 | 462 | self.log.warning("Error in executing line in user namespace: %s" % |
|
459 | 463 | line) |
|
460 | 464 | self.shell.showtraceback() |
|
461 | 465 | if not self.interact: |
|
462 | 466 | self.exit(1) |
|
463 | 467 | |
|
464 | 468 | # Like Python itself, ignore the second if the first of these is present |
|
465 | 469 | elif self.file_to_run: |
|
466 | 470 | fname = self.file_to_run |
|
467 | 471 | if os.path.isdir(fname): |
|
468 | 472 | fname = os.path.join(fname, "__main__.py") |
|
469 | 473 | if not os.path.exists(fname): |
|
470 | 474 | self.log.warning("File '%s' doesn't exist", fname) |
|
471 | 475 | if not self.interact: |
|
472 | 476 | self.exit(2) |
|
473 | 477 | try: |
|
474 | 478 | self._exec_file(fname, shell_futures=True) |
|
475 | 479 | except: |
|
476 | 480 | self.shell.showtraceback(tb_offset=4) |
|
477 | 481 | if not self.interact: |
|
478 | 482 | self.exit(1) |
|
479 | 483 | |
|
480 | 484 | def _run_module(self): |
|
481 | 485 | """Run module specified at the command-line.""" |
|
482 | 486 | if self.module_to_run: |
|
483 | 487 | # Make sure that the module gets a proper sys.argv as if it were |
|
484 | 488 | # run using `python -m`. |
|
485 | 489 | save_argv = sys.argv |
|
486 | 490 | sys.argv = [sys.executable] + self.extra_args |
|
487 | 491 | try: |
|
488 | 492 | self.shell.safe_run_module(self.module_to_run, |
|
489 | 493 | self.shell.user_ns) |
|
490 | 494 | finally: |
|
491 | 495 | sys.argv = save_argv |
@@ -1,67 +1,81 b'' | |||
|
1 | 1 | .. _plotting: |
|
2 | 2 | |
|
3 | 3 | Rich Outputs |
|
4 | 4 | ------------ |
|
5 | 5 | |
|
6 | 6 | One of the main feature of IPython when used as a kernel is its ability to |
|
7 | 7 | show rich output. This means that object that can be representing as image, |
|
8 | 8 | sounds, animation, (etc...) can be shown this way if the frontend support it. |
|
9 | 9 | |
|
10 | 10 | In order for this to be possible, you need to use the ``display()`` function, |
|
11 | 11 | that should be available by default on IPython 5.4+ and 6.1+, or that you can |
|
12 | 12 | import with ``from IPython.display import display``. Then use ``display(<your |
|
13 | 13 | object>)`` instead of ``print()``, and if possible your object will be displayed |
|
14 | 14 | with a richer representation. In the terminal of course, there won't be much |
|
15 | 15 | difference as object are most of the time represented by text, but in notebook |
|
16 | 16 | and similar interface you will get richer outputs. |
|
17 | 17 | |
|
18 | 18 | |
|
19 | .. _matplotlib_magic: | |
|
20 | ||
|
19 | 21 | Plotting |
|
20 | 22 | -------- |
|
21 | 23 | |
|
22 | 24 | .. note:: |
|
23 | 25 | |
|
24 | 26 | Starting with IPython 5.0 and matplotlib 2.0 you can avoid the use of |
|
25 | 27 | IPython's specific magic and use |
|
26 | 28 | ``matplotlib.pyplot.ion()``/``matplotlib.pyplot.ioff()`` which have the |
|
27 | 29 | advantages of working outside of IPython as well. |
|
28 | 30 | |
|
29 | 31 | |
|
30 | 32 |
One major feature of the IPython kernel is the ability to display plots that |
|
31 | 33 |
are the output of running code cells. The IPython kernel is designed to work |
|
32 | 34 | seamlessly with the matplotlib_ plotting library to provide this functionality. |
|
33 | 35 | |
|
34 | 36 | To set this up, before any plotting or import of matplotlib is performed you |
|
35 |
m |
|
|
37 | may execute the ``%matplotlib`` :ref:`magic command <magics_explained>`. This | |
|
36 | 38 | performs the necessary behind-the-scenes setup for IPython to work correctly |
|
37 | 39 | hand in hand with ``matplotlib``; it does *not*, however, actually execute any |
|
38 | 40 | Python ``import`` commands, that is, no names are added to the namespace. |
|
39 | 41 | |
|
40 |
If the ``%matplotlib`` magic |
|
|
41 | output of a plotting command is displayed using the default ``matplotlib`` | |
|
42 | backend in a separate window. Alternatively, the backend can be explicitly | |
|
43 | requested using, for example:: | |
|
42 | If you do not use the ``%matplotlib`` magic or you call it without an argument, | |
|
43 | the output of a plotting command is displayed using the default ``matplotlib`` | |
|
44 | backend, which may be different depending on Operating System and whether | |
|
45 | running within Jupyter or not. | |
|
46 | ||
|
47 | Alternatively, the backend can be explicitly requested using, for example:: | |
|
44 | 48 | |
|
45 | 49 | %matplotlib gtk |
|
46 | 50 | |
|
47 | A particularly interesting backend, provided by IPython, is the ``inline`` | |
|
48 | backend. This is available only for the Jupyter Notebook and the | |
|
49 | Jupyter QtConsole. It can be invoked as follows:: | |
|
51 | The argument passed to the ``%matplotlib`` magic command may be the name of any | |
|
52 | backend understood by ``matplotlib`` or it may the name of a GUI loop such as | |
|
53 | ``qt`` or ``osx``, in which case an appropriate backend supporting that GUI | |
|
54 | loop will be selected. To obtain a full list of all backends and GUI loops | |
|
55 | understood by ``matplotlib`` use ``%matplotlib --list``. | |
|
50 | 56 | |
|
51 | %matplotlib inline | |
|
57 | There are some specific backends that are used in the Jupyter ecosystem: | |
|
52 | 58 | |
|
53 | With this backend, the output of plotting commands is displayed *inline* within | |
|
54 | frontends like the Jupyter notebook, directly below the code cell that produced | |
|
55 | it. The resulting plots will then also be stored in the notebook document. | |
|
59 | - The ``inline`` backend is provided by IPython and can be used in Jupyter Lab, | |
|
60 | Notebook and QtConsole; it is the default backend when using Jupyter. The | |
|
61 | outputs of plotting commands are displayed *inline* within frontends like | |
|
62 | Jupyter Notebook, directly below the code cells that produced them. | |
|
63 | The resulting plots will then also be stored in the notebook document. | |
|
56 | 64 | |
|
57 | .. seealso:: | |
|
65 | - The ``notebook`` or ``nbagg`` backend is built into ``matplotlib`` and can be | |
|
66 | used with Jupyter ``notebook <7`` and ``nbclassic``. Plots are interactive so | |
|
67 | they can be zoomed and panned. | |
|
58 | 68 | |
|
59 | `Plotting with Matplotlib`_ example notebook | |
|
69 | - The ``ipympl`` or ``widget`` backend is for use with Jupyter ``lab`` and | |
|
70 | ``notebook >=7``. It is in a separate ``ipympl`` module that must be | |
|
71 | installed using ``pip`` or ``conda`` in the usual manner. Plots are | |
|
72 | interactive so they can be zoomed and panned. | |
|
60 | 73 | |
|
74 | .. seealso:: | |
|
61 | 75 | |
|
62 | The matplotlib_ library also ships with ``%matplotlib notebook`` command that | |
|
63 | allows interactive figures if your environment allows it. | |
|
76 | `Plotting with Matplotlib`_ example notebook | |
|
64 | 77 | |
|
65 |
See the matplotlib_ documentation for more information |
|
|
78 | See the matplotlib_ documentation for more information, in particular the | |
|
79 | section on backends. | |
|
66 | 80 | |
|
67 | 81 | .. include:: ../links.txt |
@@ -1,1037 +1,1017 b'' | |||
|
1 | 1 | ================= |
|
2 | 2 | IPython reference |
|
3 | 3 | ================= |
|
4 | 4 | |
|
5 | 5 | .. _command_line_options: |
|
6 | 6 | |
|
7 | 7 | Command-line usage |
|
8 | 8 | ================== |
|
9 | 9 | |
|
10 | 10 | You start IPython with the command:: |
|
11 | 11 | |
|
12 | 12 | $ ipython [options] files |
|
13 | 13 | |
|
14 | 14 | If invoked with no options, it executes the file and exits, passing the |
|
15 | 15 | remaining arguments to the script, just as if you had specified the same |
|
16 | 16 | command with python. You may need to specify `--` before args to be passed |
|
17 | 17 | to the script, to prevent IPython from attempting to parse them. |
|
18 | 18 | If you add the ``-i`` flag, it drops you into the interpreter while still |
|
19 | 19 | acknowledging any options you may have set in your ``ipython_config.py``. This |
|
20 | 20 | behavior is different from standard Python, which when called as python ``-i`` |
|
21 | 21 | will only execute one file and ignore your configuration setup. |
|
22 | 22 | |
|
23 | 23 | Please note that some of the configuration options are not available at the |
|
24 | 24 | command line, simply because they are not practical here. Look into your |
|
25 | 25 | configuration files for details on those. There are separate configuration files |
|
26 | 26 | for each profile, and the files look like :file:`ipython_config.py` or |
|
27 | 27 | :file:`ipython_config_{frontendname}.py`. Profile directories look like |
|
28 | 28 | :file:`profile_{profilename}` and are typically installed in the |
|
29 | 29 | :envvar:`IPYTHONDIR` directory, which defaults to :file:`$HOME/.ipython`. For |
|
30 | 30 | Windows users, :envvar:`HOME` resolves to :file:`C:\\Users\\{YourUserName}` in |
|
31 | 31 | most instances. |
|
32 | 32 | |
|
33 | 33 | Command-line Options |
|
34 | 34 | -------------------- |
|
35 | 35 | |
|
36 | 36 | To see the options IPython accepts, use ``ipython --help`` (and you probably |
|
37 | 37 | should run the output through a pager such as ``ipython --help | less`` for |
|
38 | 38 | more convenient reading). This shows all the options that have a single-word |
|
39 | 39 | alias to control them, but IPython lets you configure all of its objects from |
|
40 | 40 | the command-line by passing the full class name and a corresponding value; type |
|
41 | 41 | ``ipython --help-all`` to see this full list. For example:: |
|
42 | 42 | |
|
43 | 43 | $ ipython --help-all |
|
44 | 44 | <...snip...> |
|
45 | 45 | --matplotlib=<CaselessStrEnum> (InteractiveShellApp.matplotlib) |
|
46 | 46 | Default: None |
|
47 |
Choices: ['auto', 'gtk |
|
|
47 | Choices: ['auto', 'gtk3', 'gtk4', 'inline', 'nbagg', 'notebook', 'osx', 'qt', 'qt5', 'qt6', 'tk', 'wx'] | |
|
48 | 48 | Configure matplotlib for interactive use with the default matplotlib |
|
49 | 49 | backend. |
|
50 | 50 | <...snip...> |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | Indicate that the following:: |
|
54 | 54 | |
|
55 | 55 | $ ipython --matplotlib qt |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | is equivalent to:: |
|
59 | 59 | |
|
60 | 60 | $ ipython --InteractiveShellApp.matplotlib='qt' |
|
61 | 61 | |
|
62 | 62 | Note that in the second form, you *must* use the equal sign, as the expression |
|
63 | 63 | is evaluated as an actual Python assignment. While in the above example the |
|
64 | 64 | short form is more convenient, only the most common options have a short form, |
|
65 | 65 | while any configurable variable in IPython can be set at the command-line by |
|
66 | 66 | using the long form. This long form is the same syntax used in the |
|
67 | 67 | configuration files, if you want to set these options permanently. |
|
68 | 68 | |
|
69 | 69 | |
|
70 | 70 | Interactive use |
|
71 | 71 | =============== |
|
72 | 72 | |
|
73 | 73 | IPython is meant to work as a drop-in replacement for the standard interactive |
|
74 | 74 | interpreter. As such, any code which is valid python should execute normally |
|
75 | 75 | under IPython (cases where this is not true should be reported as bugs). It |
|
76 | 76 | does, however, offer many features which are not available at a standard python |
|
77 | 77 | prompt. What follows is a list of these. |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | Caution for Windows users |
|
81 | 81 | ------------------------- |
|
82 | 82 | |
|
83 | 83 | Windows, unfortunately, uses the ``\`` character as a path separator. This is a |
|
84 | 84 | terrible choice, because ``\`` also represents the escape character in most |
|
85 | 85 | modern programming languages, including Python. For this reason, using '/' |
|
86 | 86 | character is recommended if you have problems with ``\``. However, in Windows |
|
87 | 87 | commands '/' flags options, so you can not use it for the root directory. This |
|
88 | 88 | means that paths beginning at the root must be typed in a contrived manner |
|
89 | 89 | like: ``%copy \opt/foo/bar.txt \tmp`` |
|
90 | 90 | |
|
91 | 91 | .. _magic: |
|
92 | 92 | |
|
93 | 93 | Magic command system |
|
94 | 94 | -------------------- |
|
95 | 95 | |
|
96 | 96 | IPython will treat any line whose first character is a % as a special |
|
97 | 97 | call to a 'magic' function. These allow you to control the behavior of |
|
98 | 98 | IPython itself, plus a lot of system-type features. They are all |
|
99 | 99 | prefixed with a % character, but parameters are given without |
|
100 | 100 | parentheses or quotes. |
|
101 | 101 | |
|
102 | 102 | Lines that begin with ``%%`` signal a *cell magic*: they take as arguments not |
|
103 | 103 | only the rest of the current line, but all lines below them as well, in the |
|
104 | 104 | current execution block. Cell magics can in fact make arbitrary modifications |
|
105 | 105 | to the input they receive, which need not even be valid Python code at all. |
|
106 | 106 | They receive the whole block as a single string. |
|
107 | 107 | |
|
108 | 108 | As a line magic example, the :magic:`cd` magic works just like the OS command of |
|
109 | 109 | the same name:: |
|
110 | 110 | |
|
111 | 111 | In [8]: %cd |
|
112 | 112 | /home/fperez |
|
113 | 113 | |
|
114 | 114 | The following uses the builtin :magic:`timeit` in cell mode:: |
|
115 | 115 | |
|
116 | 116 | In [10]: %%timeit x = range(10000) |
|
117 | 117 | ...: min(x) |
|
118 | 118 | ...: max(x) |
|
119 | 119 | ...: |
|
120 | 120 | 518 µs ± 4.39 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) |
|
121 | 121 | |
|
122 | 122 | In this case, ``x = range(10000)`` is called as the line argument, and the |
|
123 | 123 | block with ``min(x)`` and ``max(x)`` is called as the cell body. The |
|
124 | 124 | :magic:`timeit` magic receives both. |
|
125 | 125 | |
|
126 | 126 | If you have 'automagic' enabled (as it is by default), you don't need to type in |
|
127 | 127 | the single ``%`` explicitly for line magics; IPython will scan its internal |
|
128 | 128 | list of magic functions and call one if it exists. With automagic on you can |
|
129 | 129 | then just type ``cd mydir`` to go to directory 'mydir':: |
|
130 | 130 | |
|
131 | 131 | In [9]: cd mydir |
|
132 | 132 | /home/fperez/mydir |
|
133 | 133 | |
|
134 | 134 | Cell magics *always* require an explicit ``%%`` prefix, automagic |
|
135 | 135 | calling only works for line magics. |
|
136 | 136 | |
|
137 | 137 | The automagic system has the lowest possible precedence in name searches, so |
|
138 | 138 | you can freely use variables with the same names as magic commands. If a magic |
|
139 | 139 | command is 'shadowed' by a variable, you will need the explicit ``%`` prefix to |
|
140 | 140 | use it: |
|
141 | 141 | |
|
142 | 142 | .. sourcecode:: ipython |
|
143 | 143 | |
|
144 | 144 | In [1]: cd ipython # %cd is called by automagic |
|
145 | 145 | /home/fperez/ipython |
|
146 | 146 | |
|
147 | 147 | In [2]: cd=1 # now cd is just a variable |
|
148 | 148 | |
|
149 | 149 | In [3]: cd .. # and doesn't work as a function anymore |
|
150 | 150 | File "<ipython-input-3-9fedb3aff56c>", line 1 |
|
151 | 151 | cd .. |
|
152 | 152 | ^ |
|
153 | 153 | SyntaxError: invalid syntax |
|
154 | 154 | |
|
155 | 155 | |
|
156 | 156 | In [4]: %cd .. # but %cd always works |
|
157 | 157 | /home/fperez |
|
158 | 158 | |
|
159 | 159 | In [5]: del cd # if you remove the cd variable, automagic works again |
|
160 | 160 | |
|
161 | 161 | In [6]: cd ipython |
|
162 | 162 | |
|
163 | 163 | /home/fperez/ipython |
|
164 | 164 | |
|
165 | 165 | Line magics, if they return a value, can be assigned to a variable using the |
|
166 | 166 | syntax ``l = %sx ls`` (which in this particular case returns the result of `ls` |
|
167 | 167 | as a python list). See :ref:`below <manual_capture>` for more information. |
|
168 | 168 | |
|
169 | 169 | Type ``%magic`` for more information, including a list of all available magic |
|
170 | 170 | functions at any time and their docstrings. You can also type |
|
171 | 171 | ``%magic_function_name?`` (see :ref:`below <dynamic_object_info>` for |
|
172 | 172 | information on the '?' system) to get information about any particular magic |
|
173 | 173 | function you are interested in. |
|
174 | 174 | |
|
175 | 175 | The API documentation for the :mod:`IPython.core.magic` module contains the full |
|
176 | 176 | docstrings of all currently available magic commands. |
|
177 | 177 | |
|
178 | 178 | .. seealso:: |
|
179 | 179 | |
|
180 | 180 | :doc:`magics` |
|
181 | 181 | A list of the line and cell magics available in IPython by default |
|
182 | 182 | |
|
183 | 183 | :ref:`defining_magics` |
|
184 | 184 | How to define and register additional magic functions |
|
185 | 185 | |
|
186 | 186 | |
|
187 | 187 | Access to the standard Python help |
|
188 | 188 | ---------------------------------- |
|
189 | 189 | |
|
190 | 190 | Simply type ``help()`` to access Python's standard help system. You can |
|
191 | 191 | also type ``help(object)`` for information about a given object, or |
|
192 | 192 | ``help('keyword')`` for information on a keyword. You may need to configure your |
|
193 | 193 | PYTHONDOCS environment variable for this feature to work correctly. |
|
194 | 194 | |
|
195 | 195 | .. _dynamic_object_info: |
|
196 | 196 | |
|
197 | 197 | Dynamic object information |
|
198 | 198 | -------------------------- |
|
199 | 199 | |
|
200 | 200 | Typing ``?word`` or ``word?`` prints detailed information about an object. If |
|
201 | 201 | certain strings in the object are too long (e.g. function signatures) they get |
|
202 | 202 | snipped in the center for brevity. This system gives access variable types and |
|
203 | 203 | values, docstrings, function prototypes and other useful information. |
|
204 | 204 | |
|
205 | 205 | If the information will not fit in the terminal, it is displayed in a pager |
|
206 | 206 | (``less`` if available, otherwise a basic internal pager). |
|
207 | 207 | |
|
208 | 208 | Typing ``??word`` or ``word??`` gives access to the full information, including |
|
209 | 209 | the source code where possible. Long strings are not snipped. |
|
210 | 210 | |
|
211 | 211 | The following magic functions are particularly useful for gathering |
|
212 | 212 | information about your working environment: |
|
213 | 213 | |
|
214 | 214 | * :magic:`pdoc` **<object>**: Print (or run through a pager if too long) the |
|
215 | 215 | docstring for an object. If the given object is a class, it will |
|
216 | 216 | print both the class and the constructor docstrings. |
|
217 | 217 | * :magic:`pdef` **<object>**: Print the call signature for any callable |
|
218 | 218 | object. If the object is a class, print the constructor information. |
|
219 | 219 | * :magic:`psource` **<object>**: Print (or run through a pager if too long) |
|
220 | 220 | the source code for an object. |
|
221 | 221 | * :magic:`pfile` **<object>**: Show the entire source file where an object was |
|
222 | 222 | defined via a pager, opening it at the line where the object |
|
223 | 223 | definition begins. |
|
224 | 224 | * :magic:`who`/:magic:`whos`: These functions give information about identifiers |
|
225 | 225 | you have defined interactively (not things you loaded or defined |
|
226 | 226 | in your configuration files). %who just prints a list of |
|
227 | 227 | identifiers and %whos prints a table with some basic details about |
|
228 | 228 | each identifier. |
|
229 | 229 | |
|
230 | 230 | The dynamic object information functions (?/??, ``%pdoc``, |
|
231 | 231 | ``%pfile``, ``%pdef``, ``%psource``) work on object attributes, as well as |
|
232 | 232 | directly on variables. For example, after doing ``import os``, you can use |
|
233 | 233 | ``os.path.abspath??``. |
|
234 | 234 | |
|
235 | 235 | |
|
236 | 236 | Command line completion |
|
237 | 237 | +++++++++++++++++++++++ |
|
238 | 238 | |
|
239 | 239 | At any time, hitting TAB will complete any available python commands or |
|
240 | 240 | variable names, and show you a list of the possible completions if |
|
241 | 241 | there's no unambiguous one. It will also complete filenames in the |
|
242 | 242 | current directory if no python names match what you've typed so far. |
|
243 | 243 | |
|
244 | 244 | |
|
245 | 245 | Search command history |
|
246 | 246 | ++++++++++++++++++++++ |
|
247 | 247 | |
|
248 | 248 | IPython provides two ways for searching through previous input and thus |
|
249 | 249 | reduce the need for repetitive typing: |
|
250 | 250 | |
|
251 | 251 | 1. Start typing, and then use the up and down arrow keys (or :kbd:`Ctrl-p` |
|
252 | 252 | and :kbd:`Ctrl-n`) to search through only the history items that match |
|
253 | 253 | what you've typed so far. |
|
254 | 254 | 2. Hit :kbd:`Ctrl-r`: to open a search prompt. Begin typing and the system |
|
255 | 255 | searches your history for lines that contain what you've typed so |
|
256 | 256 | far, completing as much as it can. |
|
257 | 257 | |
|
258 | 258 | IPython will save your input history when it leaves and reload it next |
|
259 | 259 | time you restart it. By default, the history file is named |
|
260 | 260 | :file:`.ipython/profile_{name}/history.sqlite`. |
|
261 | 261 | |
|
262 | 262 | Autoindent |
|
263 | 263 | ++++++++++ |
|
264 | 264 | |
|
265 | 265 | Starting with 5.0, IPython uses `prompt_toolkit` in place of ``readline``, |
|
266 | 266 | it thus can recognize lines ending in ':' and indent the next line, |
|
267 | 267 | while also un-indenting automatically after 'raise' or 'return', |
|
268 | 268 | and support real multi-line editing as well as syntactic coloration |
|
269 | 269 | during edition. |
|
270 | 270 | |
|
271 | 271 | This feature does not use the ``readline`` library anymore, so it will |
|
272 | 272 | not honor your :file:`~/.inputrc` configuration (or whatever |
|
273 | 273 | file your :envvar:`INPUTRC` environment variable points to). |
|
274 | 274 | |
|
275 | 275 | In particular if you want to change the input mode to ``vi``, you will need to |
|
276 | 276 | set the ``TerminalInteractiveShell.editing_mode`` configuration option of IPython. |
|
277 | 277 | |
|
278 | 278 | Session logging and restoring |
|
279 | 279 | ----------------------------- |
|
280 | 280 | |
|
281 | 281 | You can log all input from a session either by starting IPython with the |
|
282 | 282 | command line switch ``--logfile=foo.py`` (see :ref:`here <command_line_options>`) |
|
283 | 283 | or by activating the logging at any moment with the magic function :magic:`logstart`. |
|
284 | 284 | |
|
285 | 285 | Log files can later be reloaded by running them as scripts and IPython |
|
286 | 286 | will attempt to 'replay' the log by executing all the lines in it, thus |
|
287 | 287 | restoring the state of a previous session. This feature is not quite |
|
288 | 288 | perfect, but can still be useful in many cases. |
|
289 | 289 | |
|
290 | 290 | The log files can also be used as a way to have a permanent record of |
|
291 | 291 | any code you wrote while experimenting. Log files are regular text files |
|
292 | 292 | which you can later open in your favorite text editor to extract code or |
|
293 | 293 | to 'clean them up' before using them to replay a session. |
|
294 | 294 | |
|
295 | 295 | The :magic:`logstart` function for activating logging in mid-session is used as |
|
296 | 296 | follows:: |
|
297 | 297 | |
|
298 | 298 | %logstart [log_name [log_mode]] |
|
299 | 299 | |
|
300 | 300 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
301 | 301 | current working directory, in 'rotate' mode (see below). |
|
302 | 302 | |
|
303 | 303 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
304 | 304 | history up to that point and then continues logging. |
|
305 | 305 | |
|
306 | 306 | %logstart takes a second optional parameter: logging mode. This can be |
|
307 | 307 | one of (note that the modes are given unquoted): |
|
308 | 308 | |
|
309 | 309 | * [over:] overwrite existing log_name. |
|
310 | 310 | * [backup:] rename (if exists) to log_name~ and start log_name. |
|
311 | 311 | * [append:] well, that says it. |
|
312 | 312 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. |
|
313 | 313 | |
|
314 | 314 | Adding the '-o' flag to '%logstart' magic (as in '%logstart -o [log_name [log_mode]]') |
|
315 | 315 | will also include output from iPython in the log file. |
|
316 | 316 | |
|
317 | 317 | The :magic:`logoff` and :magic:`logon` functions allow you to temporarily stop and |
|
318 | 318 | resume logging to a file which had previously been started with |
|
319 | 319 | %logstart. They will fail (with an explanation) if you try to use them |
|
320 | 320 | before logging has been started. |
|
321 | 321 | |
|
322 | 322 | .. _system_shell_access: |
|
323 | 323 | |
|
324 | 324 | System shell access |
|
325 | 325 | ------------------- |
|
326 | 326 | |
|
327 | 327 | Any input line beginning with a ``!`` character is passed verbatim (minus |
|
328 | 328 | the ``!``, of course) to the underlying operating system. For example, |
|
329 | 329 | typing ``!ls`` will run 'ls' in the current directory. |
|
330 | 330 | |
|
331 | 331 | .. _manual_capture: |
|
332 | 332 | |
|
333 | 333 | Manual capture of command output and magic output |
|
334 | 334 | ------------------------------------------------- |
|
335 | 335 | |
|
336 | 336 | You can assign the result of a system command to a Python variable with the |
|
337 | 337 | syntax ``myfiles = !ls``. Similarly, the result of a magic (as long as it returns |
|
338 | 338 | a value) can be assigned to a variable. For example, the syntax ``myfiles = %sx ls`` |
|
339 | 339 | is equivalent to the above system command example (the :magic:`sx` magic runs a shell command |
|
340 | 340 | and captures the output). Each of these gets machine |
|
341 | 341 | readable output from stdout (e.g. without colours), and splits on newlines. To |
|
342 | 342 | explicitly get this sort of output without assigning to a variable, use two |
|
343 | 343 | exclamation marks (``!!ls``) or the :magic:`sx` magic command without an assignment. |
|
344 | 344 | (However, ``!!`` commands cannot be assigned to a variable.) |
|
345 | 345 | |
|
346 | 346 | The captured list in this example has some convenience features. ``myfiles.n`` or ``myfiles.s`` |
|
347 | 347 | returns a string delimited by newlines or spaces, respectively. ``myfiles.p`` |
|
348 | 348 | produces `path objects <http://pypi.python.org/pypi/path.py>`_ from the list items. |
|
349 | 349 | See :ref:`string_lists` for details. |
|
350 | 350 | |
|
351 | 351 | IPython also allows you to expand the value of python variables when |
|
352 | 352 | making system calls. Wrap variables or expressions in {braces}:: |
|
353 | 353 | |
|
354 | 354 | In [1]: pyvar = 'Hello world' |
|
355 | 355 | In [2]: !echo "A python variable: {pyvar}" |
|
356 | 356 | A python variable: Hello world |
|
357 | 357 | In [3]: import math |
|
358 | 358 | In [4]: x = 8 |
|
359 | 359 | In [5]: !echo {math.factorial(x)} |
|
360 | 360 | 40320 |
|
361 | 361 | |
|
362 | 362 | For simple cases, you can alternatively prepend $ to a variable name:: |
|
363 | 363 | |
|
364 | 364 | In [6]: !echo $sys.argv |
|
365 | 365 | [/home/fperez/usr/bin/ipython] |
|
366 | 366 | In [7]: !echo "A system variable: $$HOME" # Use $$ for literal $ |
|
367 | 367 | A system variable: /home/fperez |
|
368 | 368 | |
|
369 | 369 | Note that `$$` is used to represent a literal `$`. |
|
370 | 370 | |
|
371 | 371 | System command aliases |
|
372 | 372 | ---------------------- |
|
373 | 373 | |
|
374 | 374 | The :magic:`alias` magic function allows you to define magic functions which are in fact |
|
375 | 375 | system shell commands. These aliases can have parameters. |
|
376 | 376 | |
|
377 | 377 | ``%alias alias_name cmd`` defines 'alias_name' as an alias for 'cmd' |
|
378 | 378 | |
|
379 | 379 | Then, typing ``alias_name params`` will execute the system command 'cmd |
|
380 | 380 | params' (from your underlying operating system). |
|
381 | 381 | |
|
382 | 382 | You can also define aliases with parameters using ``%s`` specifiers (one per |
|
383 | 383 | parameter). The following example defines the parts function as an |
|
384 | 384 | alias to the command ``echo first %s second %s`` where each ``%s`` will be |
|
385 | 385 | replaced by a positional parameter to the call to %parts:: |
|
386 | 386 | |
|
387 | 387 | In [1]: %alias parts echo first %s second %s |
|
388 | 388 | In [2]: parts A B |
|
389 | 389 | first A second B |
|
390 | 390 | In [3]: parts A |
|
391 | 391 | ERROR: Alias <parts> requires 2 arguments, 1 given. |
|
392 | 392 | |
|
393 | 393 | If called with no parameters, :magic:`alias` prints the table of currently |
|
394 | 394 | defined aliases. |
|
395 | 395 | |
|
396 | 396 | The :magic:`rehashx` magic allows you to load your entire $PATH as |
|
397 | 397 | ipython aliases. See its docstring for further details. |
|
398 | 398 | |
|
399 | 399 | |
|
400 | 400 | .. _dreload: |
|
401 | 401 | |
|
402 | 402 | Recursive reload |
|
403 | 403 | ---------------- |
|
404 | 404 | |
|
405 | 405 | The :mod:`IPython.lib.deepreload` module allows you to recursively reload a |
|
406 | 406 | module: changes made to any of its dependencies will be reloaded without |
|
407 | 407 | having to exit. To start using it, do:: |
|
408 | 408 | |
|
409 | 409 | from IPython.lib.deepreload import reload as dreload |
|
410 | 410 | |
|
411 | 411 | |
|
412 | 412 | Verbose and colored exception traceback printouts |
|
413 | 413 | ------------------------------------------------- |
|
414 | 414 | |
|
415 | 415 | IPython provides the option to see very detailed exception tracebacks, |
|
416 | 416 | which can be especially useful when debugging large programs. You can |
|
417 | 417 | run any Python file with the %run function to benefit from these |
|
418 | 418 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can |
|
419 | 419 | be colored (if your terminal supports it) which makes them much easier |
|
420 | 420 | to parse visually. |
|
421 | 421 | |
|
422 | 422 | See the magic :magic:`xmode` and :magic:`colors` functions for details. |
|
423 | 423 | |
|
424 | 424 | These features are basically a terminal version of Ka-Ping Yee's cgitb |
|
425 | 425 | module, now part of the standard Python library. |
|
426 | 426 | |
|
427 | 427 | |
|
428 | 428 | .. _input_caching: |
|
429 | 429 | |
|
430 | 430 | Input caching system |
|
431 | 431 | -------------------- |
|
432 | 432 | |
|
433 | 433 | IPython offers numbered prompts (In/Out) with input and output caching |
|
434 | 434 | (also referred to as 'input history'). All input is saved and can be |
|
435 | 435 | retrieved as variables (besides the usual arrow key recall), in |
|
436 | 436 | addition to the :magic:`rep` magic command that brings a history entry |
|
437 | 437 | up for editing on the next command line. |
|
438 | 438 | |
|
439 | 439 | The following variables always exist: |
|
440 | 440 | |
|
441 | 441 | * ``_i``, ``_ii``, ``_iii``: store previous, next previous and next-next |
|
442 | 442 | previous inputs. |
|
443 | 443 | |
|
444 | 444 | * ``In``, ``_ih`` : a list of all inputs; ``_ih[n]`` is the input from line |
|
445 | 445 | ``n``. If you overwrite In with a variable of your own, you can remake the |
|
446 | 446 | assignment to the internal list with a simple ``In=_ih``. |
|
447 | 447 | |
|
448 | 448 | Additionally, global variables named ``_i<n>`` are dynamically created (``<n>`` |
|
449 | 449 | being the prompt counter), so ``_i<n> == _ih[<n>] == In[<n>]``. |
|
450 | 450 | |
|
451 | 451 | For example, what you typed at prompt 14 is available as ``_i14``, ``_ih[14]`` |
|
452 | 452 | and ``In[14]``. |
|
453 | 453 | |
|
454 | 454 | This allows you to easily cut and paste multi line interactive prompts |
|
455 | 455 | by printing them out: they print like a clean string, without prompt |
|
456 | 456 | characters. You can also manipulate them like regular variables (they |
|
457 | 457 | are strings), modify or exec them. |
|
458 | 458 | |
|
459 | 459 | You can also re-execute multiple lines of input easily by using the magic |
|
460 | 460 | :magic:`rerun` or :magic:`macro` functions. The macro system also allows you to |
|
461 | 461 | re-execute previous lines which include magic function calls (which require |
|
462 | 462 | special processing). Type %macro? for more details on the macro system. |
|
463 | 463 | |
|
464 | 464 | A history function :magic:`history` allows you to see any part of your input |
|
465 | 465 | history by printing a range of the _i variables. |
|
466 | 466 | |
|
467 | 467 | You can also search ('grep') through your history by typing |
|
468 | 468 | ``%hist -g somestring``. This is handy for searching for URLs, IP addresses, |
|
469 | 469 | etc. You can bring history entries listed by '%hist -g' up for editing |
|
470 | 470 | with the %recall command, or run them immediately with :magic:`rerun`. |
|
471 | 471 | |
|
472 | 472 | .. _output_caching: |
|
473 | 473 | |
|
474 | 474 | Output caching system |
|
475 | 475 | --------------------- |
|
476 | 476 | |
|
477 | 477 | For output that is returned from actions, a system similar to the input |
|
478 | 478 | cache exists but using _ instead of _i. Only actions that produce a |
|
479 | 479 | result (NOT assignments, for example) are cached. If you are familiar |
|
480 | 480 | with Mathematica, IPython's _ variables behave exactly like |
|
481 | 481 | Mathematica's % variables. |
|
482 | 482 | |
|
483 | 483 | The following variables always exist: |
|
484 | 484 | |
|
485 | 485 | * [_] (a single underscore): stores previous output, like Python's |
|
486 | 486 | default interpreter. |
|
487 | 487 | * [__] (two underscores): next previous. |
|
488 | 488 | * [___] (three underscores): next-next previous. |
|
489 | 489 | |
|
490 | 490 | Additionally, global variables named _<n> are dynamically created (<n> |
|
491 | 491 | being the prompt counter), such that the result of output <n> is always |
|
492 | 492 | available as _<n> (don't use the angle brackets, just the number, e.g. |
|
493 | 493 | ``_21``). |
|
494 | 494 | |
|
495 | 495 | These variables are also stored in a global dictionary (not a |
|
496 | 496 | list, since it only has entries for lines which returned a result) |
|
497 | 497 | available under the names _oh and Out (similar to _ih and In). So the |
|
498 | 498 | output from line 12 can be obtained as ``_12``, ``Out[12]`` or ``_oh[12]``. If you |
|
499 | 499 | accidentally overwrite the Out variable you can recover it by typing |
|
500 | 500 | ``Out=_oh`` at the prompt. |
|
501 | 501 | |
|
502 | 502 | This system obviously can potentially put heavy memory demands on your |
|
503 | 503 | system, since it prevents Python's garbage collector from removing any |
|
504 | 504 | previously computed results. You can control how many results are kept |
|
505 | 505 | in memory with the configuration option ``InteractiveShell.cache_size``. |
|
506 | 506 | If you set it to 0, output caching is disabled. You can also use the :magic:`reset` |
|
507 | 507 | and :magic:`xdel` magics to clear large items from memory. |
|
508 | 508 | |
|
509 | 509 | Directory history |
|
510 | 510 | ----------------- |
|
511 | 511 | |
|
512 | 512 | Your history of visited directories is kept in the global list _dh, and |
|
513 | 513 | the magic :magic:`cd` command can be used to go to any entry in that list. The |
|
514 | 514 | :magic:`dhist` command allows you to view this history. Do ``cd -<TAB>`` to |
|
515 | 515 | conveniently view the directory history. |
|
516 | 516 | |
|
517 | 517 | |
|
518 | 518 | Automatic parentheses and quotes |
|
519 | 519 | -------------------------------- |
|
520 | 520 | |
|
521 | 521 | These features were adapted from Nathan Gray's LazyPython. They are |
|
522 | 522 | meant to allow less typing for common situations. |
|
523 | 523 | |
|
524 | 524 | Callable objects (i.e. functions, methods, etc) can be invoked like this |
|
525 | 525 | (notice the commas between the arguments):: |
|
526 | 526 | |
|
527 | 527 | In [1]: callable_ob arg1, arg2, arg3 |
|
528 | 528 | ------> callable_ob(arg1, arg2, arg3) |
|
529 | 529 | |
|
530 | 530 | .. note:: |
|
531 | 531 | This feature is disabled by default. To enable it, use the ``%autocall`` |
|
532 | 532 | magic command. The commands below with special prefixes will always work, |
|
533 | 533 | however. |
|
534 | 534 | |
|
535 | 535 | You can force automatic parentheses by using '/' as the first character |
|
536 | 536 | of a line. For example:: |
|
537 | 537 | |
|
538 | 538 | In [2]: /globals # becomes 'globals()' |
|
539 | 539 | |
|
540 | 540 | Note that the '/' MUST be the first character on the line! This won't work:: |
|
541 | 541 | |
|
542 | 542 | In [3]: print /globals # syntax error |
|
543 | 543 | |
|
544 | 544 | In most cases the automatic algorithm should work, so you should rarely |
|
545 | 545 | need to explicitly invoke /. One notable exception is if you are trying |
|
546 | 546 | to call a function with a list of tuples as arguments (the parenthesis |
|
547 | 547 | will confuse IPython):: |
|
548 | 548 | |
|
549 | 549 | In [4]: zip (1,2,3),(4,5,6) # won't work |
|
550 | 550 | |
|
551 | 551 | but this will work:: |
|
552 | 552 | |
|
553 | 553 | In [5]: /zip (1,2,3),(4,5,6) |
|
554 | 554 | ------> zip ((1,2,3),(4,5,6)) |
|
555 | 555 | Out[5]: [(1, 4), (2, 5), (3, 6)] |
|
556 | 556 | |
|
557 | 557 | IPython tells you that it has altered your command line by displaying |
|
558 | 558 | the new command line preceded by ``--->``. |
|
559 | 559 | |
|
560 | 560 | You can force automatic quoting of a function's arguments by using ``,`` |
|
561 | 561 | or ``;`` as the first character of a line. For example:: |
|
562 | 562 | |
|
563 | 563 | In [1]: ,my_function /home/me # becomes my_function("/home/me") |
|
564 | 564 | |
|
565 | 565 | If you use ';' the whole argument is quoted as a single string, while ',' splits |
|
566 | 566 | on whitespace:: |
|
567 | 567 | |
|
568 | 568 | In [2]: ,my_function a b c # becomes my_function("a","b","c") |
|
569 | 569 | |
|
570 | 570 | In [3]: ;my_function a b c # becomes my_function("a b c") |
|
571 | 571 | |
|
572 | 572 | Note that the ',' or ';' MUST be the first character on the line! This |
|
573 | 573 | won't work:: |
|
574 | 574 | |
|
575 | 575 | In [4]: x = ,my_function /home/me # syntax error |
|
576 | 576 | |
|
577 | 577 | IPython as your default Python environment |
|
578 | 578 | ========================================== |
|
579 | 579 | |
|
580 | 580 | Python honors the environment variable :envvar:`PYTHONSTARTUP` and will |
|
581 | 581 | execute at startup the file referenced by this variable. If you put the |
|
582 | 582 | following code at the end of that file, then IPython will be your working |
|
583 | 583 | environment anytime you start Python:: |
|
584 | 584 | |
|
585 | 585 | import os, IPython |
|
586 | 586 | os.environ['PYTHONSTARTUP'] = '' # Prevent running this again |
|
587 | 587 | IPython.start_ipython() |
|
588 | 588 | raise SystemExit |
|
589 | 589 | |
|
590 | 590 | The ``raise SystemExit`` is needed to exit Python when |
|
591 | 591 | it finishes, otherwise you'll be back at the normal Python ``>>>`` |
|
592 | 592 | prompt. |
|
593 | 593 | |
|
594 | 594 | This is probably useful to developers who manage multiple Python |
|
595 | 595 | versions and don't want to have correspondingly multiple IPython |
|
596 | 596 | versions. Note that in this mode, there is no way to pass IPython any |
|
597 | 597 | command-line options, as those are trapped first by Python itself. |
|
598 | 598 | |
|
599 | 599 | .. _Embedding: |
|
600 | 600 | |
|
601 | 601 | Embedding IPython |
|
602 | 602 | ================= |
|
603 | 603 | |
|
604 | 604 | You can start a regular IPython session with |
|
605 | 605 | |
|
606 | 606 | .. sourcecode:: python |
|
607 | 607 | |
|
608 | 608 | import IPython |
|
609 | 609 | IPython.start_ipython(argv=[]) |
|
610 | 610 | |
|
611 | 611 | at any point in your program. This will load IPython configuration, |
|
612 | 612 | startup files, and everything, just as if it were a normal IPython session. |
|
613 | 613 | For information on setting configuration options when running IPython from |
|
614 | 614 | python, see :ref:`configure_start_ipython`. |
|
615 | 615 | |
|
616 | 616 | It is also possible to embed an IPython shell in a namespace in your Python |
|
617 | 617 | code. This allows you to evaluate dynamically the state of your code, operate |
|
618 | 618 | with your variables, analyze them, etc. For example, if you run the following |
|
619 | 619 | code snippet:: |
|
620 | 620 | |
|
621 | 621 | import IPython |
|
622 | 622 | |
|
623 | 623 | a = 42 |
|
624 | 624 | IPython.embed() |
|
625 | 625 | |
|
626 | 626 |
and within the IPython shell, you reassign `a` to `23` to do further testing of |
|
627 | 627 | some sort, you can then exit:: |
|
628 | 628 | |
|
629 | 629 | >>> IPython.embed() |
|
630 | 630 |
Python 3.6.2 (default, Jul 17 2017, 16:44:45) |
|
631 | 631 | Type 'copyright', 'credits' or 'license' for more information |
|
632 | 632 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. |
|
633 | 633 | |
|
634 | 634 | In [1]: a = 23 |
|
635 | 635 | |
|
636 | 636 | In [2]: exit() |
|
637 | 637 | |
|
638 | 638 | Once you exit and print `a`, the value 23 will be shown:: |
|
639 | 639 | |
|
640 | 640 | |
|
641 | 641 | In: print(a) |
|
642 | 642 | 23 |
|
643 | 643 | |
|
644 | 644 |
It's important to note that the code run in the embedded IPython shell will |
|
645 | 645 |
*not* change the state of your code and variables, **unless** the shell is |
|
646 | 646 |
contained within the global namespace. In the above example, `a` is changed |
|
647 | 647 | because this is true. |
|
648 | 648 | |
|
649 | 649 | To further exemplify this, consider the following example:: |
|
650 | 650 | |
|
651 | 651 | import IPython |
|
652 | 652 | def do(): |
|
653 | 653 | a = 42 |
|
654 | 654 | print(a) |
|
655 | 655 | IPython.embed() |
|
656 | 656 | print(a) |
|
657 | 657 | |
|
658 | 658 | Now if call the function and complete the state changes as we did above, the |
|
659 | 659 | value `42` will be printed. Again, this is because it's not in the global |
|
660 | 660 |
namespace:: |
|
661 | 661 |
|
|
662 | 662 | do() |
|
663 | 663 | |
|
664 | 664 | Running a file with the above code can lead to the following session:: |
|
665 | 665 | |
|
666 | 666 | >>> do() |
|
667 | 667 | 42 |
|
668 | 668 |
Python 3.6.2 (default, Jul 17 2017, 16:44:45) |
|
669 | 669 | Type 'copyright', 'credits' or 'license' for more information |
|
670 | 670 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. |
|
671 | 671 | |
|
672 | 672 | In [1]: a = 23 |
|
673 | 673 | |
|
674 | 674 | In [2]: exit() |
|
675 | 675 | 42 |
|
676 | 676 | |
|
677 | 677 | .. note:: |
|
678 | 678 | |
|
679 | 679 | At present, embedding IPython cannot be done from inside IPython. |
|
680 | 680 | Run the code samples below outside IPython. |
|
681 | 681 | |
|
682 | 682 | This feature allows you to easily have a fully functional python |
|
683 | 683 | environment for doing object introspection anywhere in your code with a |
|
684 | 684 | simple function call. In some cases a simple print statement is enough, |
|
685 | 685 | but if you need to do more detailed analysis of a code fragment this |
|
686 | 686 | feature can be very valuable. |
|
687 | 687 | |
|
688 | 688 | It can also be useful in scientific computing situations where it is |
|
689 | 689 | common to need to do some automatic, computationally intensive part and |
|
690 | 690 | then stop to look at data, plots, etc. |
|
691 | 691 | Opening an IPython instance will give you full access to your data and |
|
692 | 692 | functions, and you can resume program execution once you are done with |
|
693 | 693 | the interactive part (perhaps to stop again later, as many times as |
|
694 | 694 | needed). |
|
695 | 695 | |
|
696 | 696 | The following code snippet is the bare minimum you need to include in |
|
697 | 697 | your Python programs for this to work (detailed examples follow later):: |
|
698 | 698 | |
|
699 | 699 | from IPython import embed |
|
700 | 700 | |
|
701 | 701 | embed() # this call anywhere in your program will start IPython |
|
702 | 702 | |
|
703 | 703 | You can also embed an IPython *kernel*, for use with qtconsole, etc. via |
|
704 | 704 | ``IPython.embed_kernel()``. This should work the same way, but you can |
|
705 | 705 | connect an external frontend (``ipython qtconsole`` or ``ipython console``), |
|
706 | 706 | rather than interacting with it in the terminal. |
|
707 | 707 | |
|
708 | 708 | You can run embedded instances even in code which is itself being run at |
|
709 | 709 | the IPython interactive prompt with '%run <filename>'. Since it's easy |
|
710 | 710 | to get lost as to where you are (in your top-level IPython or in your |
|
711 | 711 | embedded one), it's a good idea in such cases to set the in/out prompts |
|
712 | 712 | to something different for the embedded instances. The code examples |
|
713 | 713 | below illustrate this. |
|
714 | 714 | |
|
715 | 715 | You can also have multiple IPython instances in your program and open |
|
716 | 716 | them separately, for example with different options for data |
|
717 | 717 | presentation. If you close and open the same instance multiple times, |
|
718 | 718 | its prompt counters simply continue from each execution to the next. |
|
719 | 719 | |
|
720 | 720 | Please look at the docstrings in the :mod:`~IPython.frontend.terminal.embed` |
|
721 | 721 | module for more details on the use of this system. |
|
722 | 722 | |
|
723 | 723 | The following sample file illustrating how to use the embedding |
|
724 | 724 | functionality is provided in the examples directory as embed_class_long.py. |
|
725 | 725 | It should be fairly self-explanatory: |
|
726 | 726 | |
|
727 | 727 | .. literalinclude:: ../../../examples/Embedding/embed_class_long.py |
|
728 | 728 | :language: python |
|
729 | 729 | |
|
730 | 730 | Once you understand how the system functions, you can use the following |
|
731 | 731 | code fragments in your programs which are ready for cut and paste: |
|
732 | 732 | |
|
733 | 733 | .. literalinclude:: ../../../examples/Embedding/embed_class_short.py |
|
734 | 734 | :language: python |
|
735 | 735 | |
|
736 | 736 | Using the Python debugger (pdb) |
|
737 | 737 | =============================== |
|
738 | 738 | |
|
739 | 739 | Running entire programs via pdb |
|
740 | 740 | ------------------------------- |
|
741 | 741 | |
|
742 | 742 | pdb, the Python debugger, is a powerful interactive debugger which |
|
743 | 743 | allows you to step through code, set breakpoints, watch variables, |
|
744 | 744 | etc. IPython makes it very easy to start any script under the control |
|
745 | 745 | of pdb, regardless of whether you have wrapped it into a 'main()' |
|
746 | 746 | function or not. For this, simply type ``%run -d myscript`` at an |
|
747 | 747 | IPython prompt. See the :magic:`run` command's documentation for more details, including |
|
748 | 748 | how to control where pdb will stop execution first. |
|
749 | 749 | |
|
750 | 750 | For more information on the use of the pdb debugger, see :ref:`debugger-commands` |
|
751 | 751 | in the Python documentation. |
|
752 | 752 | |
|
753 | 753 | IPython extends the debugger with a few useful additions, like coloring of |
|
754 | 754 | tracebacks. The debugger will adopt the color scheme selected for IPython. |
|
755 | 755 | |
|
756 | 756 | The ``where`` command has also been extended to take as argument the number of |
|
757 | 757 | context line to show. This allows to a many line of context on shallow stack trace: |
|
758 | 758 | |
|
759 | 759 | .. code:: |
|
760 | 760 | |
|
761 | 761 | In [5]: def foo(x): |
|
762 | 762 | ...: 1 |
|
763 | 763 | ...: 2 |
|
764 | 764 | ...: 3 |
|
765 | 765 | ...: return 1/x+foo(x-1) |
|
766 | 766 | ...: 5 |
|
767 | 767 | ...: 6 |
|
768 | 768 | ...: 7 |
|
769 | 769 | ...: |
|
770 | 770 | |
|
771 | 771 | In[6]: foo(1) |
|
772 | 772 | # ... |
|
773 | 773 | ipdb> where 8 |
|
774 | 774 | <ipython-input-6-9e45007b2b59>(1)<module> |
|
775 | 775 | ----> 1 foo(1) |
|
776 | 776 | |
|
777 | 777 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
778 | 778 | 1 def foo(x): |
|
779 | 779 | 2 1 |
|
780 | 780 | 3 2 |
|
781 | 781 | 4 3 |
|
782 | 782 | ----> 5 return 1/x+foo(x-1) |
|
783 | 783 | 6 5 |
|
784 | 784 | 7 6 |
|
785 | 785 | 8 7 |
|
786 | 786 | |
|
787 | 787 | > <ipython-input-5-7baadc3d1465>(5)foo() |
|
788 | 788 | 1 def foo(x): |
|
789 | 789 | 2 1 |
|
790 | 790 | 3 2 |
|
791 | 791 | 4 3 |
|
792 | 792 | ----> 5 return 1/x+foo(x-1) |
|
793 | 793 | 6 5 |
|
794 | 794 | 7 6 |
|
795 | 795 | 8 7 |
|
796 | 796 | |
|
797 | 797 | |
|
798 | 798 | And less context on shallower Stack Trace: |
|
799 | 799 | |
|
800 | 800 | .. code:: |
|
801 | 801 | |
|
802 | 802 | ipdb> where 1 |
|
803 | 803 | <ipython-input-13-afa180a57233>(1)<module> |
|
804 | 804 | ----> 1 foo(7) |
|
805 | 805 | |
|
806 | 806 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
807 | 807 | ----> 5 return 1/x+foo(x-1) |
|
808 | 808 | |
|
809 | 809 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
810 | 810 | ----> 5 return 1/x+foo(x-1) |
|
811 | 811 | |
|
812 | 812 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
813 | 813 | ----> 5 return 1/x+foo(x-1) |
|
814 | 814 | |
|
815 | 815 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
816 | 816 | ----> 5 return 1/x+foo(x-1) |
|
817 | 817 | |
|
818 | 818 | |
|
819 | 819 | Post-mortem debugging |
|
820 | 820 | --------------------- |
|
821 | 821 | |
|
822 | 822 | Going into a debugger when an exception occurs can be |
|
823 | 823 | extremely useful in order to find the origin of subtle bugs, because pdb |
|
824 | 824 | opens up at the point in your code which triggered the exception, and |
|
825 | 825 | while your program is at this point 'dead', all the data is still |
|
826 | 826 | available and you can walk up and down the stack frame and understand |
|
827 | 827 | the origin of the problem. |
|
828 | 828 | |
|
829 | 829 | You can use the :magic:`debug` magic after an exception has occurred to start |
|
830 | 830 | post-mortem debugging. IPython can also call debugger every time your code |
|
831 | 831 | triggers an uncaught exception. This feature can be toggled with the :magic:`pdb` magic |
|
832 | 832 | command, or you can start IPython with the ``--pdb`` option. |
|
833 | 833 | |
|
834 | 834 | For a post-mortem debugger in your programs outside IPython, |
|
835 | 835 | put the following lines toward the top of your 'main' routine:: |
|
836 | 836 | |
|
837 | 837 | import sys |
|
838 | 838 | from IPython.core import ultratb |
|
839 | 839 | sys.excepthook = ultratb.FormattedTB(mode='Verbose', |
|
840 | 840 | color_scheme='Linux', call_pdb=1) |
|
841 | 841 | |
|
842 | 842 | The mode keyword can be either 'Verbose' or 'Plain', giving either very |
|
843 | 843 | detailed or normal tracebacks respectively. The color_scheme keyword can |
|
844 | 844 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same |
|
845 | 845 | options which can be set in IPython with ``--colors`` and ``--xmode``. |
|
846 | 846 | |
|
847 | 847 | This will give any of your programs detailed, colored tracebacks with |
|
848 | 848 | automatic invocation of pdb. |
|
849 | 849 | |
|
850 | 850 | .. _pasting_with_prompts: |
|
851 | 851 | |
|
852 | 852 | Pasting of code starting with Python or IPython prompts |
|
853 | 853 | ======================================================= |
|
854 | 854 | |
|
855 | 855 | IPython is smart enough to filter out input prompts, be they plain Python ones |
|
856 | 856 | (``>>>`` and ``...``) or IPython ones (``In [N]:`` and ``...:``). You can |
|
857 | 857 | therefore copy and paste from existing interactive sessions without worry. |
|
858 | 858 | |
|
859 | 859 | The following is a 'screenshot' of how things work, copying an example from the |
|
860 | 860 | standard Python tutorial:: |
|
861 | 861 | |
|
862 | 862 | In [1]: >>> # Fibonacci series: |
|
863 | 863 | |
|
864 | 864 | In [2]: ... # the sum of two elements defines the next |
|
865 | 865 | |
|
866 | 866 | In [3]: ... a, b = 0, 1 |
|
867 | 867 | |
|
868 | 868 | In [4]: >>> while b < 10: |
|
869 | 869 | ...: ... print(b) |
|
870 | 870 | ...: ... a, b = b, a+b |
|
871 | 871 | ...: |
|
872 | 872 | 1 |
|
873 | 873 | 1 |
|
874 | 874 | 2 |
|
875 | 875 | 3 |
|
876 | 876 | 5 |
|
877 | 877 | 8 |
|
878 | 878 | |
|
879 | 879 | And pasting from IPython sessions works equally well:: |
|
880 | 880 | |
|
881 | 881 | In [1]: In [5]: def f(x): |
|
882 | 882 | ...: ...: "A simple function" |
|
883 | 883 | ...: ...: return x**2 |
|
884 | 884 | ...: ...: |
|
885 | 885 | |
|
886 | 886 | In [2]: f(3) |
|
887 | 887 | Out[2]: 9 |
|
888 | 888 | |
|
889 | 889 | .. _gui_support: |
|
890 | 890 | |
|
891 | 891 | GUI event loop support |
|
892 | 892 | ====================== |
|
893 | 893 | |
|
894 | 894 | IPython has excellent support for working interactively with Graphical User |
|
895 | 895 | Interface (GUI) toolkits, such as wxPython, PyQt/PySide, PyGTK and Tk. This is |
|
896 | 896 | implemented by running the toolkit's event loop while IPython is waiting for |
|
897 | 897 | input. |
|
898 | 898 | |
|
899 | 899 | For users, enabling GUI event loop integration is simple. You simple use the |
|
900 | 900 | :magic:`gui` magic as follows:: |
|
901 | 901 | |
|
902 | 902 | %gui [GUINAME] |
|
903 | 903 | |
|
904 | 904 | With no arguments, ``%gui`` removes all GUI support. Valid ``GUINAME`` |
|
905 |
arguments include ``wx``, ``qt``, ``qt5``, ``qt6``, `` |
|
|
905 | arguments include ``wx``, ``qt``, ``qt5``, ``qt6``, ``gtk3`` ``gtk4``, and | |
|
906 | 906 | ``tk``. |
|
907 | 907 | |
|
908 | 908 | Thus, to use wxPython interactively and create a running :class:`wx.App` |
|
909 | 909 | object, do:: |
|
910 | 910 | |
|
911 | 911 | %gui wx |
|
912 | 912 | |
|
913 | 913 | You can also start IPython with an event loop set up using the `--gui` |
|
914 | 914 | flag:: |
|
915 | 915 | |
|
916 | 916 | $ ipython --gui=qt |
|
917 | 917 | |
|
918 | 918 | For information on IPython's matplotlib_ integration (and the ``matplotlib`` |
|
919 | 919 | mode) see :ref:`this section <matplotlib_support>`. |
|
920 | 920 | |
|
921 | 921 | For developers that want to integrate additional event loops with IPython, see |
|
922 | 922 | :doc:`/config/eventloops`. |
|
923 | 923 | |
|
924 | 924 | When running inside IPython with an integrated event loop, a GUI application |
|
925 | 925 | should *not* start its own event loop. This means that applications that are |
|
926 | 926 | meant to be used both |
|
927 | 927 | in IPython and as standalone apps need to have special code to detects how the |
|
928 | 928 | application is being run. We highly recommend using IPython's support for this. |
|
929 | 929 | Since the details vary slightly between toolkits, we point you to the various |
|
930 | 930 | examples in our source directory :file:`examples/IPython Kernel/gui/` that |
|
931 | 931 | demonstrate these capabilities. |
|
932 | 932 | |
|
933 | 933 | PyQt and PySide |
|
934 | 934 | --------------- |
|
935 | 935 | |
|
936 | 936 | .. attempt at explanation of the complete mess that is Qt support |
|
937 | 937 | |
|
938 | 938 | When you use ``--gui=qt`` or ``--matplotlib=qt``, IPython can work with either |
|
939 | 939 | PyQt or PySide. ``qt`` implies "use the latest version available", and it favors |
|
940 |
PyQt over PySide. To request a specific version, use ``qt5`` or ``qt6``. |
|
|
941 | Qt4 is not supported with the ``--gui`` switch (and has not been for some time now). | |
|
940 | PyQt over PySide. To request a specific version, use ``qt5`` or ``qt6``. | |
|
942 | 941 | |
|
943 |
If specified, IPython will respect the environment variable ``QT_API`` |
|
|
944 | by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires | |
|
945 | PyQt4 to use its v2 API. So if ``QT_API=pyside`` PySide will be used, | |
|
946 | and if ``QT_API=pyqt`` then PyQt4 will be used *with the v2 API* for | |
|
947 | QString and QVariant, so ETS codes like MayaVi will also work with IPython. | |
|
948 | ||
|
949 | If you launch IPython in matplotlib mode with ``ipython --matplotlib=qt``, | |
|
950 | then IPython will ask matplotlib which Qt library to use (only if QT_API is | |
|
951 | *not set*), via the 'backend.qt4' rcParam. If matplotlib is version 1.0.1 or | |
|
952 | older, then IPython will always use PyQt4 without setting the v2 APIs, since | |
|
953 | neither v2 PyQt nor PySide work. | |
|
954 | ||
|
955 | .. warning:: | |
|
956 | ||
|
957 | Note that this means for ETS 4 to work with PyQt4, ``QT_API`` *must* be set | |
|
958 | to work with IPython's qt integration, because otherwise PyQt4 will be | |
|
959 | loaded in an incompatible mode. | |
|
960 | ||
|
961 | It also means that you must *not* have ``QT_API`` set if you want to | |
|
962 | use ``--gui=qt`` with code that requires PyQt4 API v1. | |
|
942 | If specified, IPython will respect the environment variable ``QT_API``. If | |
|
943 | ``QT_API`` is not specified and you launch IPython in matplotlib mode with | |
|
944 | ``ipython --matplotlib=qt`` then IPython will ask matplotlib which Qt library | |
|
945 | to use. See the matplotlib_ documentation on ``QT_API`` for further details. | |
|
963 | 946 | |
|
964 | 947 | |
|
965 | 948 | .. _matplotlib_support: |
|
966 | 949 | |
|
967 | 950 | Plotting with matplotlib |
|
968 | 951 | ======================== |
|
969 | 952 | |
|
970 | 953 | matplotlib_ provides high quality 2D and 3D plotting for Python. matplotlib_ |
|
971 | 954 | can produce plots on screen using a variety of GUI toolkits, including Tk, |
|
972 |
PyGTK, PyQt |
|
|
955 | PyGTK, PyQt6 and wxPython. It also provides a number of commands useful for | |
|
973 | 956 | scientific computing, all with a syntax compatible with that of the popular |
|
974 | 957 | Matlab program. |
|
975 | 958 | |
|
976 | 959 | To start IPython with matplotlib support, use the ``--matplotlib`` switch. If |
|
977 | 960 | IPython is already running, you can run the :magic:`matplotlib` magic. If no |
|
978 | 961 | arguments are given, IPython will automatically detect your choice of |
|
979 |
matplotlib backend. |
|
|
980 | ``%matplotlib backend``, where ``backend`` must be one of: 'tk', 'qt', 'wx', | |
|
981 | 'gtk', 'osx'. In the web notebook and Qt console, 'inline' is also a valid | |
|
982 | backend value, which produces static figures inlined inside the application | |
|
983 | window instead of matplotlib's interactive figures that live in separate | |
|
984 | windows. | |
|
962 | matplotlib backend. For information on matplotlib backends see | |
|
963 | :ref:`matplotlib_magic`. | |
|
964 | ||
|
985 | 965 | |
|
986 | 966 | .. _interactive_demos: |
|
987 | 967 | |
|
988 | 968 | Interactive demos with IPython |
|
989 | 969 | ============================== |
|
990 | 970 | |
|
991 | 971 | IPython ships with a basic system for running scripts interactively in |
|
992 | 972 | sections, useful when presenting code to audiences. A few tags embedded |
|
993 | 973 | in comments (so that the script remains valid Python code) divide a file |
|
994 | 974 | into separate blocks, and the demo can be run one block at a time, with |
|
995 | 975 | IPython printing (with syntax highlighting) the block before executing |
|
996 | 976 | it, and returning to the interactive prompt after each block. The |
|
997 | 977 | interactive namespace is updated after each block is run with the |
|
998 | 978 | contents of the demo's namespace. |
|
999 | 979 | |
|
1000 | 980 | This allows you to show a piece of code, run it and then execute |
|
1001 | 981 | interactively commands based on the variables just created. Once you |
|
1002 | 982 | want to continue, you simply execute the next block of the demo. The |
|
1003 | 983 | following listing shows the markup necessary for dividing a script into |
|
1004 | 984 | sections for execution as a demo: |
|
1005 | 985 | |
|
1006 | 986 | .. literalinclude:: ../../../examples/IPython Kernel/example-demo.py |
|
1007 | 987 | :language: python |
|
1008 | 988 | |
|
1009 | 989 | In order to run a file as a demo, you must first make a Demo object out |
|
1010 | 990 | of it. If the file is named myscript.py, the following code will make a |
|
1011 | 991 | demo:: |
|
1012 | 992 | |
|
1013 | 993 | from IPython.lib.demo import Demo |
|
1014 | 994 | |
|
1015 | 995 | mydemo = Demo('myscript.py') |
|
1016 | 996 | |
|
1017 | 997 | This creates the mydemo object, whose blocks you run one at a time by |
|
1018 | 998 | simply calling the object with no arguments. Then call it to run each step |
|
1019 | 999 | of the demo:: |
|
1020 | 1000 | |
|
1021 | 1001 | mydemo() |
|
1022 | 1002 | |
|
1023 | 1003 | Demo objects can be |
|
1024 | 1004 | restarted, you can move forward or back skipping blocks, re-execute the |
|
1025 | 1005 | last block, etc. See the :mod:`IPython.lib.demo` module and the |
|
1026 | 1006 | :class:`~IPython.lib.demo.Demo` class for details. |
|
1027 | 1007 | |
|
1028 | 1008 | Limitations: These demos are limited to |
|
1029 | 1009 | fairly simple uses. In particular, you cannot break up sections within |
|
1030 | 1010 | indented code (loops, if statements, function definitions, etc.) |
|
1031 | 1011 | Supporting something like this would basically require tracking the |
|
1032 | 1012 | internal execution state of the Python interpreter, so only top-level |
|
1033 | 1013 | divisions are allowed. If you want to be able to open an IPython |
|
1034 | 1014 | instance at an arbitrary point in a program, you can use IPython's |
|
1035 | 1015 | :ref:`embedding facilities <Embedding>`. |
|
1036 | 1016 | |
|
1037 | 1017 | .. include:: ../links.txt |
|
1 | NO CONTENT: modified file |
General Comments 0
You need to be logged in to leave comments.
Login now