Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd7267c

Browse files
committed
Use matplotlib-inline instead of ipykernel.pylab
1 parent eb9b2bd commit fd7267c

File tree

6 files changed

+53
-83
lines changed

6 files changed

+53
-83
lines changed

IPython/core/display.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def reload(self):
371371
with gzip.open(BytesIO(data), 'rt', encoding=encoding) as fp:
372372
encoding = None
373373
data = fp.read()
374-
374+
375375
# decode data, if an encoding was specified
376376
# We only touch self.data once since
377377
# subclasses such as SVG have @data.setter methods
@@ -1155,7 +1155,10 @@ def reload(self):
11551155

11561156
@skip_doctest
11571157
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.
11591162
11601163
For example, this enables PNG and JPEG output with a JPEG quality of 90%::
11611164
@@ -1173,20 +1176,19 @@ def set_matplotlib_formats(*formats, **kwargs):
11731176
**kwargs
11741177
Keyword args will be relayed to ``figure.canvas.print_figure``.
11751178
"""
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("`set_matplotlib_formats` is deprecated, directly use "
1180+
"`matplotlib_inline.backend_inline.set_matplotlib_formats()`",
1181+
DeprecationWarning, stacklevel=2)
1182+
1183+
from matplotlib_inline.backend_inline import set_matplotlib_formats as set_matplotlib_formats_orig
1184+
set_matplotlib_formats_orig(*formats, **kwargs)
11861185

11871186
@skip_doctest
11881187
def set_matplotlib_close(close=True):
1189-
"""Set whether the inline backend closes all figures automatically or not.
1188+
"""
1189+
DEPRECATED
1190+
1191+
Set whether the inline backend closes all figures automatically or not.
11901192
11911193
By default, the inline backend used in the IPython Notebook will close all
11921194
matplotlib figures automatically after each cell is run. This means that
@@ -1206,6 +1208,9 @@ def set_matplotlib_close(close=True):
12061208
Should all matplotlib figures be automatically closed after each cell is
12071209
run?
12081210
"""
1209-
from ipykernel.pylab.config import InlineBackend
1210-
cfg = InlineBackend.instance()
1211-
cfg.close_figures = close
1211+
warnings.warn("`set_matplotlib_close` is deprecated, directly use "
1212+
"`matplotlib_inline.backend_inline.set_matplotlib_close()`",
1213+
DeprecationWarning, stacklevel=2)
1214+
1215+
from matplotlib_inline.backend_inline import set_matplotlib_close as set_matplotlib_close_orig
1216+
set_matplotlib_close_orig(close)

IPython/core/interactiveshell.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3514,6 +3514,7 @@ def enable_matplotlib(self, gui=None):
35143514
display figures inline.
35153515
"""
35163516
from IPython.core import pylabtools as pt
3517+
from matplotlib_inline.backend_inline import configure_inline_support
35173518
gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
35183519

35193520
if gui != 'inline':
@@ -3527,7 +3528,7 @@ def enable_matplotlib(self, gui=None):
35273528
gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
35283529

35293530
pt.activate_matplotlib(backend)
3530-
pt.configure_inline_support(self, backend)
3531+
configure_inline_support(self, backend)
35313532

35323533
# Now we must activate the gui pylab wants to use, and fix %run to take
35333534
# plot updates into account

IPython/core/pylabtools.py

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Distributed under the terms of the Modified BSD License.
66

77
from io import BytesIO
8+
import warnings
89

910
from IPython.core.display import _pngxy
1011
from IPython.utils.decorators import flag_calls
@@ -25,7 +26,7 @@
2526
'svg': 'svg',
2627
'pdf': 'pdf',
2728
'ps': 'ps',
28-
'inline': 'module://ipykernel.pylab.backend_inline',
29+
'inline': 'module://matplotlib_inline.backend_inline',
2930
'ipympl': 'module://ipympl.backend_nbagg',
3031
'widget': 'module://ipympl.backend_nbagg',
3132
}
@@ -49,7 +50,7 @@
4950
del backend2gui['svg']
5051
del backend2gui['pdf']
5152
del backend2gui['ps']
52-
del backend2gui['module://ipykernel.pylab.backend_inline']
53+
del backend2gui['module://matplotlib_inline.backend_inline']
5354

5455
#-----------------------------------------------------------------------------
5556
# Matplotlib utilities
@@ -96,10 +97,10 @@ def figsize(sizex, sizey):
9697

9798
def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs):
9899
"""Print a figure to an image, and return the resulting file data
99-
100+
100101
Returned data will be bytes unless ``fmt='svg'``,
101102
in which case it will be unicode.
102-
103+
103104
Any keyword args are passed to fig.canvas.print_figure,
104105
such as ``quality`` or ``bbox_inches``.
105106
"""
@@ -112,7 +113,7 @@ def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs):
112113
if fmt == 'retina':
113114
dpi = dpi * 2
114115
fmt = 'png'
115-
116+
116117
# build keyword args
117118
kw = {
118119
"format":fmt,
@@ -162,7 +163,7 @@ def mpl_runner(safe_execfile):
162163
A function suitable for use as the ``runner`` argument of the %run magic
163164
function.
164165
"""
165-
166+
166167
def mpl_execfile(fname,*where,**kw):
167168
"""matplotlib-aware wrapper around safe_execfile.
168169
@@ -243,7 +244,7 @@ def select_figure_formats(shell, formats, **kwargs):
243244
bs = "%s" % ','.join([repr(f) for f in bad])
244245
gs = "%s" % ','.join([repr(f) for f in supported])
245246
raise ValueError("supported formats are: %s not %s" % (gs, bs))
246-
247+
247248
if 'png' in formats:
248249
png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
249250
if 'retina' in formats or 'png2x' in formats:
@@ -274,7 +275,7 @@ def find_gui_and_backend(gui=None, gui_select=None):
274275
Returns
275276
-------
276277
A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
277-
'WXAgg','Qt4Agg','module://ipykernel.pylab.backend_inline','agg').
278+
'WXAgg','Qt4Agg','module://matplotlib_inline.backend_inline','agg').
278279
"""
279280

280281
import matplotlib
@@ -308,7 +309,7 @@ def activate_matplotlib(backend):
308309

309310
import matplotlib
310311
matplotlib.interactive(True)
311-
312+
312313
# Matplotlib had a bug where even switch_backend could not force
313314
# the rcParam to update. This needs to be set *before* the module
314315
# magic of switch_backend().
@@ -329,11 +330,11 @@ def activate_matplotlib(backend):
329330

330331
def import_pylab(user_ns, import_all=True):
331332
"""Populate the namespace with pylab-related values.
332-
333+
333334
Imports matplotlib, pylab, numpy, and everything from pylab and numpy.
334-
335+
335336
Also imports a few names from IPython (figsize, display, getfigs)
336-
337+
337338
"""
338339

339340
# Import numpy as np/pyplot as plt are conventions we're trying to
@@ -346,12 +347,12 @@ def import_pylab(user_ns, import_all=True):
346347
"plt = pyplot\n"
347348
)
348349
exec(s, user_ns)
349-
350+
350351
if import_all:
351352
s = ("from matplotlib.pylab import *\n"
352353
"from numpy import *\n")
353354
exec(s, user_ns)
354-
355+
355356
# IPython symbols to add
356357
user_ns['figsize'] = figsize
357358
from IPython.display import display
@@ -361,59 +362,20 @@ def import_pylab(user_ns, import_all=True):
361362

362363

363364
def configure_inline_support(shell, backend):
364-
"""Configure an IPython shell object for matplotlib use.
365+
"""
366+
DEPRECATED
367+
368+
Configure an IPython shell object for matplotlib use.
365369
366370
Parameters
367371
----------
368372
shell : InteractiveShell instance
369373
370374
backend : matplotlib backend
371375
"""
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.
375-
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
376+
warnings.warn("`configure_inline_support` is deprecated, directly use "
377+
"`matplotlib_inline.backend_inline.configure_inline_support()`",
378+
DeprecationWarning, stacklevel=2)
384379

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
380+
from matplotlib_inline.backend_inline import configure_inline_support_orig
381+
configure_inline_support_orig(shell, backend)

IPython/core/tests/test_display.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_image_filename_defaults():
135135
nt.assert_is_none(img._repr_jpeg_())
136136

137137
def _get_inline_config():
138-
from ipykernel.pylab.config import InlineBackend
138+
from matplotlib_inline.config import InlineBackend
139139
return InlineBackend.instance()
140140

141141

IPython/core/tests/test_pylabtools.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import nose.tools as nt
1616

1717
from matplotlib import pyplot as plt
18+
import matplotlib_inline
1819
import numpy as np
1920

2021
from IPython.core.getipython import get_ipython
@@ -167,13 +168,13 @@ def act_mpl(backend):
167168
pt.activate_matplotlib = act_mpl
168169
self._save_ip = pt.import_pylab
169170
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 = lambda *a,**kw: None
172173

173174
def teardown(self):
174175
pt.activate_matplotlib = self._save_am
175176
pt.import_pylab = self._save_ip
176-
pt.configure_inline_support = self._save_cis
177+
matplotlib_inline.backend_inline.configure_inline_support = self._save_cis
177178
import matplotlib
178179
matplotlib.rcParams = self._saved_rcParams
179180
matplotlib.rcParamsOrig = self._saved_rcParamsOrig

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
'pygments',
201201
'backcall',
202202
'stack_data',
203+
'matplotlib-inline',
203204
]
204205

205206
# Platform-specific dependencies:

0 commit comments

Comments
 (0)