##// END OF EJS Templates
Use matplotlib-inline instead of ipykernel.pylab
martinRenou -
Show More
@@ -371,7 +371,7 b' class DisplayObject(object):'
371 371 with gzip.open(BytesIO(data), 'rt', encoding=encoding) as fp:
372 372 encoding = None
373 373 data = fp.read()
374
374
375 375 # decode data, if an encoding was specified
376 376 # We only touch self.data once since
377 377 # subclasses such as SVG have @data.setter methods
@@ -1155,7 +1155,10 b' class Video(DisplayObject):'
1155 1155
1156 1156 @skip_doctest
1157 1157 def set_matplotlib_formats(*formats, **kwargs):
1158 """Select figure formats for the inline backend. Optionally pass quality for JPEG.
1158 """
1159 DEPRECATED
1160
1161 Select figure formats for the inline backend. Optionally pass quality for JPEG.
1159 1162
1160 1163 For example, this enables PNG and JPEG output with a JPEG quality of 90%::
1161 1164
@@ -1173,20 +1176,25 b' def set_matplotlib_formats(*formats, **kwargs):'
1173 1176 **kwargs
1174 1177 Keyword args will be relayed to ``figure.canvas.print_figure``.
1175 1178 """
1176 from IPython.core.interactiveshell import InteractiveShell
1177 from IPython.core.pylabtools import select_figure_formats
1178 # build kwargs, starting with InlineBackend config
1179 kw = {}
1180 from ipykernel.pylab.config import InlineBackend
1181 cfg = InlineBackend.instance()
1182 kw.update(cfg.print_figure_kwargs)
1183 kw.update(**kwargs)
1184 shell = InteractiveShell.instance()
1185 select_figure_formats(shell, formats, **kw)
1179 warnings.warn(
1180 "`set_matplotlib_formats` is deprecated, directly use "
1181 "`matplotlib_inline.backend_inline.set_matplotlib_formats()`",
1182 DeprecationWarning,
1183 stacklevel=2,
1184 )
1185
1186 from matplotlib_inline.backend_inline import (
1187 set_matplotlib_formats as set_matplotlib_formats_orig,
1188 )
1189
1190 set_matplotlib_formats_orig(*formats, **kwargs)
1186 1191
1187 1192 @skip_doctest
1188 1193 def set_matplotlib_close(close=True):
1189 """Set whether the inline backend closes all figures automatically or not.
1194 """
1195 DEPRECATED
1196
1197 Set whether the inline backend closes all figures automatically or not.
1190 1198
1191 1199 By default, the inline backend used in the IPython Notebook will close all
1192 1200 matplotlib figures automatically after each cell is run. This means that
@@ -1206,6 +1214,15 b' def set_matplotlib_close(close=True):'
1206 1214 Should all matplotlib figures be automatically closed after each cell is
1207 1215 run?
1208 1216 """
1209 from ipykernel.pylab.config import InlineBackend
1210 cfg = InlineBackend.instance()
1211 cfg.close_figures = close
1217 warnings.warn(
1218 "`set_matplotlib_close` is deprecated, directly use "
1219 "`matplotlib_inline.backend_inline.set_matplotlib_close()`",
1220 DeprecationWarning,
1221 stacklevel=2,
1222 )
1223
1224 from matplotlib_inline.backend_inline import (
1225 set_matplotlib_close as set_matplotlib_close_orig,
1226 )
1227
1228 set_matplotlib_close_orig(close)
@@ -3514,6 +3514,7 b' class InteractiveShell(SingletonConfigurable):'
3514 3514 display figures inline.
3515 3515 """
3516 3516 from IPython.core import pylabtools as pt
3517 from matplotlib_inline.backend_inline import configure_inline_support
3517 3518 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
3518 3519
3519 3520 if gui != 'inline':
@@ -3527,7 +3528,7 b' class InteractiveShell(SingletonConfigurable):'
3527 3528 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
3528 3529
3529 3530 pt.activate_matplotlib(backend)
3530 pt.configure_inline_support(self, backend)
3531 configure_inline_support(self, backend)
3531 3532
3532 3533 # Now we must activate the gui pylab wants to use, and fix %run to take
3533 3534 # plot updates into account
@@ -5,30 +5,32 b''
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 from io import BytesIO
8 import warnings
8 9
9 10 from IPython.core.display import _pngxy
10 11 from IPython.utils.decorators import flag_calls
11 12
12 13 # If user specifies a GUI, that dictates the backend, otherwise we read the
13 14 # user's mpl default from the mpl rc structure
14 backends = {'tk': 'TkAgg',
15 'gtk': 'GTKAgg',
16 'gtk3': 'GTK3Agg',
17 'wx': 'WXAgg',
18 'qt4': 'Qt4Agg',
19 'qt5': 'Qt5Agg',
20 'qt': 'Qt5Agg',
21 'osx': 'MacOSX',
22 'nbagg': 'nbAgg',
23 'notebook': 'nbAgg',
24 'agg': 'agg',
25 'svg': 'svg',
26 'pdf': 'pdf',
27 'ps': 'ps',
28 'inline': 'module://ipykernel.pylab.backend_inline',
29 'ipympl': 'module://ipympl.backend_nbagg',
30 'widget': 'module://ipympl.backend_nbagg',
31 }
15 backends = {
16 "tk": "TkAgg",
17 "gtk": "GTKAgg",
18 "gtk3": "GTK3Agg",
19 "wx": "WXAgg",
20 "qt4": "Qt4Agg",
21 "qt5": "Qt5Agg",
22 "qt": "Qt5Agg",
23 "osx": "MacOSX",
24 "nbagg": "nbAgg",
25 "notebook": "nbAgg",
26 "agg": "agg",
27 "svg": "svg",
28 "pdf": "pdf",
29 "ps": "ps",
30 "inline": "module://matplotlib_inline.backend_inline",
31 "ipympl": "module://ipympl.backend_nbagg",
32 "widget": "module://ipympl.backend_nbagg",
33 }
32 34
33 35 # We also need a reverse backends2guis mapping that will properly choose which
34 36 # GUI support to activate based on the desired matplotlib backend. For the
@@ -44,12 +46,12 b" backend2gui['GTK3Cairo'] = 'gtk3'"
44 46 backend2gui['WX'] = 'wx'
45 47 backend2gui['CocoaAgg'] = 'osx'
46 48 # And some backends that don't need GUI integration
47 del backend2gui['nbAgg']
48 del backend2gui['agg']
49 del backend2gui['svg']
50 del backend2gui['pdf']
51 del backend2gui['ps']
52 del backend2gui['module://ipykernel.pylab.backend_inline']
49 del backend2gui["nbAgg"]
50 del backend2gui["agg"]
51 del backend2gui["svg"]
52 del backend2gui["pdf"]
53 del backend2gui["ps"]
54 del backend2gui["module://matplotlib_inline.backend_inline"]
53 55
54 56 #-----------------------------------------------------------------------------
55 57 # Matplotlib utilities
@@ -96,10 +98,10 b' def figsize(sizex, sizey):'
96 98
97 99 def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs):
98 100 """Print a figure to an image, and return the resulting file data
99
101
100 102 Returned data will be bytes unless ``fmt='svg'``,
101 103 in which case it will be unicode.
102
104
103 105 Any keyword args are passed to fig.canvas.print_figure,
104 106 such as ``quality`` or ``bbox_inches``.
105 107 """
@@ -112,7 +114,7 b" def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs):"
112 114 if fmt == 'retina':
113 115 dpi = dpi * 2
114 116 fmt = 'png'
115
117
116 118 # build keyword args
117 119 kw = {
118 120 "format":fmt,
@@ -162,7 +164,7 b' def mpl_runner(safe_execfile):'
162 164 A function suitable for use as the ``runner`` argument of the %run magic
163 165 function.
164 166 """
165
167
166 168 def mpl_execfile(fname,*where,**kw):
167 169 """matplotlib-aware wrapper around safe_execfile.
168 170
@@ -243,7 +245,7 b' def select_figure_formats(shell, formats, **kwargs):'
243 245 bs = "%s" % ','.join([repr(f) for f in bad])
244 246 gs = "%s" % ','.join([repr(f) for f in supported])
245 247 raise ValueError("supported formats are: %s not %s" % (gs, bs))
246
248
247 249 if 'png' in formats:
248 250 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
249 251 if 'retina' in formats or 'png2x' in formats:
@@ -274,7 +276,7 b' def find_gui_and_backend(gui=None, gui_select=None):'
274 276 Returns
275 277 -------
276 278 A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
277 'WXAgg','Qt4Agg','module://ipykernel.pylab.backend_inline','agg').
279 'WXAgg','Qt4Agg','module://matplotlib_inline.backend_inline','agg').
278 280 """
279 281
280 282 import matplotlib
@@ -308,7 +310,7 b' def activate_matplotlib(backend):'
308 310
309 311 import matplotlib
310 312 matplotlib.interactive(True)
311
313
312 314 # Matplotlib had a bug where even switch_backend could not force
313 315 # the rcParam to update. This needs to be set *before* the module
314 316 # magic of switch_backend().
@@ -329,11 +331,11 b' def activate_matplotlib(backend):'
329 331
330 332 def import_pylab(user_ns, import_all=True):
331 333 """Populate the namespace with pylab-related values.
332
334
333 335 Imports matplotlib, pylab, numpy, and everything from pylab and numpy.
334
336
335 337 Also imports a few names from IPython (figsize, display, getfigs)
336
338
337 339 """
338 340
339 341 # Import numpy as np/pyplot as plt are conventions we're trying to
@@ -346,12 +348,12 b' def import_pylab(user_ns, import_all=True):'
346 348 "plt = pyplot\n"
347 349 )
348 350 exec(s, user_ns)
349
351
350 352 if import_all:
351 353 s = ("from matplotlib.pylab import *\n"
352 354 "from numpy import *\n")
353 355 exec(s, user_ns)
354
356
355 357 # IPython symbols to add
356 358 user_ns['figsize'] = figsize
357 359 from IPython.display import display
@@ -361,7 +363,10 b' def import_pylab(user_ns, import_all=True):'
361 363
362 364
363 365 def configure_inline_support(shell, backend):
364 """Configure an IPython shell object for matplotlib use.
366 """
367 DEPRECATED
368
369 Configure an IPython shell object for matplotlib use.
365 370
366 371 Parameters
367 372 ----------
@@ -369,51 +374,13 b' def configure_inline_support(shell, backend):'
369 374
370 375 backend : matplotlib backend
371 376 """
372 # If using our svg payload backend, register the post-execution
373 # function that will pick up the results for display. This can only be
374 # done with access to the real shell object.
377 warnings.warn(
378 "`configure_inline_support` is deprecated, directly use "
379 "`matplotlib_inline.backend_inline.configure_inline_support()`",
380 DeprecationWarning,
381 stacklevel=2,
382 )
375 383
376 # Note: if we can't load the inline backend, then there's no point
377 # continuing (such as in terminal-only shells in environments without
378 # zeromq available).
379 try:
380 from ipykernel.pylab.backend_inline import InlineBackend
381 except ImportError:
382 return
383 import matplotlib
384 from matplotlib_inline.backend_inline import configure_inline_support_orig
384 385
385 cfg = InlineBackend.instance(parent=shell)
386 cfg.shell = shell
387 if cfg not in shell.configurables:
388 shell.configurables.append(cfg)
389
390 if backend == backends['inline']:
391 from ipykernel.pylab.backend_inline import flush_figures
392 shell.events.register('post_execute', flush_figures)
393
394 # Save rcParams that will be overwrittern
395 shell._saved_rcParams = {}
396 for k in cfg.rc:
397 shell._saved_rcParams[k] = matplotlib.rcParams[k]
398 # load inline_rc
399 matplotlib.rcParams.update(cfg.rc)
400 new_backend_name = "inline"
401 else:
402 from ipykernel.pylab.backend_inline import flush_figures
403 try:
404 shell.events.unregister('post_execute', flush_figures)
405 except ValueError:
406 pass
407 if hasattr(shell, '_saved_rcParams'):
408 matplotlib.rcParams.update(shell._saved_rcParams)
409 del shell._saved_rcParams
410 new_backend_name = "other"
411
412 # only enable the formats once -> don't change the enabled formats (which the user may
413 # has changed) when getting another "%matplotlib inline" call.
414 # See https://github.com/ipython/ipykernel/issues/29
415 cur_backend = getattr(configure_inline_support, "current_backend", "unset")
416 if new_backend_name != cur_backend:
417 # Setup the default figure format
418 select_figure_formats(shell, cfg.figure_formats, **cfg.print_figure_kwargs)
419 configure_inline_support.current_backend = new_backend_name
386 configure_inline_support_orig(shell, backend)
@@ -135,7 +135,7 b' def test_image_filename_defaults():'
135 135 nt.assert_is_none(img._repr_jpeg_())
136 136
137 137 def _get_inline_config():
138 from ipykernel.pylab.config import InlineBackend
138 from matplotlib_inline.config import InlineBackend
139 139 return InlineBackend.instance()
140 140
141 141
@@ -15,6 +15,7 b' from nose import SkipTest'
15 15 import nose.tools as nt
16 16
17 17 from matplotlib import pyplot as plt
18 import matplotlib_inline
18 19 import numpy as np
19 20
20 21 from IPython.core.getipython import get_ipython
@@ -167,13 +168,15 b' class TestPylabSwitch(object):'
167 168 pt.activate_matplotlib = act_mpl
168 169 self._save_ip = pt.import_pylab
169 170 pt.import_pylab = lambda *a,**kw:None
170 self._save_cis = pt.configure_inline_support
171 pt.configure_inline_support = lambda *a,**kw:None
171 self._save_cis = matplotlib_inline.backend_inline.configure_inline_support
172 matplotlib_inline.backend_inline.configure_inline_support = (
173 lambda *a, **kw: None
174 )
172 175
173 176 def teardown(self):
174 177 pt.activate_matplotlib = self._save_am
175 178 pt.import_pylab = self._save_ip
176 pt.configure_inline_support = self._save_cis
179 matplotlib_inline.backend_inline.configure_inline_support = self._save_cis
177 180 import matplotlib
178 181 matplotlib.rcParams = self._saved_rcParams
179 182 matplotlib.rcParamsOrig = self._saved_rcParamsOrig
@@ -191,15 +191,16 b' extras_require = dict('
191 191 )
192 192
193 193 install_requires = [
194 'setuptools>=18.5',
195 'jedi>=0.16',
196 'decorator',
197 'pickleshare',
198 'traitlets>=4.2',
199 'prompt_toolkit>=2.0.0,<3.1.0,!=3.0.0,!=3.0.1',
200 'pygments',
201 'backcall',
202 'stack_data',
194 "setuptools>=18.5",
195 "jedi>=0.16",
196 "decorator",
197 "pickleshare",
198 "traitlets>=4.2",
199 "prompt_toolkit>=2.0.0,<3.1.0,!=3.0.0,!=3.0.1",
200 "pygments",
201 "backcall",
202 "stack_data",
203 "matplotlib-inline",
203 204 ]
204 205
205 206 # Platform-specific dependencies:
General Comments 0
You need to be logged in to leave comments. Login now