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