diff --git a/IPython/core/display.py b/IPython/core/display.py index 3af9699..301e8aa 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -751,7 +751,7 @@ def set_matplotlib_formats(*formats, **kwargs): select_figure_formats(shell, formats, **kw) @skip_doctest -def set_matplotlib_close(close): +def set_matplotlib_close(close=True): """Set whether the inline backend closes all figures automatically or not. By default, the inline backend used in the IPython Notebook will close all @@ -772,7 +772,7 @@ def set_matplotlib_close(close): Should all matplotlib figures be automatically closed after each cell is run? """ - from IPython.kernel.zmq.pylab.backend_inline import InlineBackend - ilbe = InlineBackend.instance() - ilbe.close_figures = close + from IPython.kernel.zmq.pylab.config import InlineBackend + cfg = InlineBackend.instance() + cfg.close_figures = close diff --git a/IPython/core/tests/test_display.py b/IPython/core/tests/test_display.py index 67fb620..8e55cc0 100644 --- a/IPython/core/tests/test_display.py +++ b/IPython/core/tests/test_display.py @@ -10,8 +10,11 @@ import os import nose.tools as nt from IPython.core import display +from IPython.core.getipython import get_ipython from IPython.utils import path as ipath +import IPython.testing.decorators as dec + def test_image_size(): """Simple test for display.Image(args, width=x,height=y)""" thisurl = 'http://www.google.fr/images/srpr/logo3w.png' @@ -58,3 +61,58 @@ def test_image_filename_defaults(): img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False) nt.assert_equal('jpeg', img.format) nt.assert_is_none(img._repr_jpeg_()) + +def _get_inline_config(): + from IPython.kernel.zmq.pylab.config import InlineBackend + return InlineBackend.instance() + +@dec.skip_without('matplotlib') +def test_set_matplotlib_close(): + cfg = _get_inline_config() + cfg.close_figures = False + display.set_matplotlib_close() + assert cfg.close_figures + display.set_matplotlib_close(False) + assert not cfg.close_figures + +_fmt_mime_map = { + 'png': 'image/png', + 'jpeg': 'image/jpeg', + 'pdf': 'application/pdf', + 'retina': 'image/png', + 'svg': 'image/svg+xml', +} + +@dec.skip_without('matplotlib') +def test_set_matplotlib_formats(): + from matplotlib.figure import Figure + formatters = get_ipython().display_formatter.formatters + for formats in [ + ('png',), + ('pdf', 'svg'), + ('jpeg', 'retina', 'png'), + (), + ]: + active_mimes = {_fmt_mime_map[fmt] for fmt in formats} + display.set_matplotlib_formats(*formats) + for mime, f in formatters.items(): + if mime in active_mimes: + nt.assert_in(Figure, f) + else: + nt.assert_not_in(Figure, f) + +@dec.skip_without('matplotlib') +def test_set_matplotlib_formats_kwargs(): + from matplotlib.figure import Figure + ip = get_ipython() + cfg = _get_inline_config() + cfg.print_figure_kwargs.update(dict(foo='bar')) + kwargs = dict(quality=10) + display.set_matplotlib_formats('png', **kwargs) + formatter = ip.display_formatter.formatters['image/png'] + f = formatter.lookup_by_type(Figure) + cell = f.__closure__[0].cell_contents + expected = kwargs + expected.update(cfg.print_figure_kwargs) + nt.assert_equal(cell, expected) +