##// END OF EJS Templates
Adding support for multiple figure formats in InlineBackend.
Brian E. Granger -
Show More
@@ -25,6 +25,7 b' from io import BytesIO'
25 25
26 26 from IPython.core.display import _pngxy
27 27 from IPython.utils.decorators import flag_calls
28 from IPython.utils import py3compat
28 29
29 30 # If user specifies a GUI, that dictates the backend, otherwise we read the
30 31 # user's mpl default from the mpl rc structure
@@ -165,10 +166,15 b' def mpl_runner(safe_execfile):'
165 166 return mpl_execfile
166 167
167 168
168 def select_figure_format(shell, fmt, quality=90):
169 """Select figure format for inline backend, can be 'png', 'retina', 'jpg', or 'svg'.
169 def select_figure_format(shell, formats, quality=90):
170 """Select figure formats for the inline backend.
170 171
171 Using this method ensures only one figure format is active at a time.
172 Parameters
173 ==========
174 shell : InteractiveShell
175 The main IPython instance
176 formats : list
177 One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
172 178 """
173 179 from matplotlib.figure import Figure
174 180 from IPython.kernel.zmq.pylab import backend_inline
@@ -176,22 +182,26 b' def select_figure_format(shell, fmt, quality=90):'
176 182 svg_formatter = shell.display_formatter.formatters['image/svg+xml']
177 183 png_formatter = shell.display_formatter.formatters['image/png']
178 184 jpg_formatter = shell.display_formatter.formatters['image/jpeg']
185 pdf_formatter = shell.display_formatter.formatters['application/pdf']
179 186
180 [ f.type_printers.pop(Figure, None) for f in {svg_formatter, png_formatter, jpg_formatter} ]
187 if isinstance(formats, py3compat.string_types):
188 formats = {formats}
181 189
182 if fmt == 'png':
183 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png'))
184 elif fmt in ('png2x', 'retina'):
185 png_formatter.for_type(Figure, retina_figure)
186 elif fmt in ('jpg', 'jpeg'):
187 jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', quality))
188 elif fmt == 'svg':
189 svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg'))
190 else:
191 raise ValueError("supported formats are: 'png', 'retina', 'svg', 'jpg', not %r" % fmt)
190 [ f.type_printers.pop(Figure, None) for f in {svg_formatter, png_formatter, jpg_formatter} ]
192 191
193 # set the format to be used in the backend()
194 backend_inline._figure_format = fmt
192 for fmt in formats:
193 if fmt == 'png':
194 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png'))
195 elif fmt in ('png2x', 'retina'):
196 png_formatter.for_type(Figure, retina_figure)
197 elif fmt in ('jpg', 'jpeg'):
198 jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', quality))
199 elif fmt == 'svg':
200 svg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'svg'))
201 elif fmt == 'pdf':
202 pdf_formatter.for_type(Figure, lambda fig: print_figure(fig, 'pdf'))
203 else:
204 raise ValueError("supported formats are: 'png', 'retina', 'svg', 'jpg', 'pdf' not %r" % fmt)
195 205
196 206 #-----------------------------------------------------------------------------
197 207 # Code for initializing matplotlib and importing pylab
@@ -342,5 +352,5 b' def configure_inline_support(shell, backend):'
342 352 del shell._saved_rcParams
343 353
344 354 # Setup the default figure format
345 select_figure_format(shell, cfg.figure_format, cfg.quality)
355 select_figure_format(shell, cfg.figure_formats, cfg.quality)
346 356
@@ -14,7 +14,9 b' This module does not import anything from matplotlib.'
14 14 #-----------------------------------------------------------------------------
15 15
16 16 from IPython.config.configurable import SingletonConfigurable
17 from IPython.utils.traitlets import Dict, Instance, CaselessStrEnum, Bool, Int, TraitError
17 from IPython.utils.traitlets import (
18 Dict, Instance, CaselessStrEnum, Set, Bool, Int, TraitError, Unicode
19 )
18 20 from IPython.utils.warn import warn
19 21
20 22 #-----------------------------------------------------------------------------
@@ -63,15 +65,13 b' class InlineBackend(InlineBackendConfig):'
63 65 inline backend."""
64 66 )
65 67
68 figure_formats = Set({'png'}, config=True,
69 help="""A set of figure formats to enable: 'png',
70 'retina', 'jpeg', 'svg', 'pdf'.""")
66 71
67 figure_format = CaselessStrEnum(['svg', 'png', 'retina', 'jpg'],
68 default_value='png', config=True,
69 help="""The image format for figures with the inline
70 backend. JPEG requires the PIL/Pillow library.""")
71
72 def _figure_format_changed(self, name, old, new):
72 def _figure_formats_changed(self, name, old, new):
73 73 from IPython.core.pylabtools import select_figure_format
74 if new in {"jpg", "jpeg"}:
74 if 'jpg' in new or 'jpeg' in new:
75 75 if not pil_available():
76 76 raise TraitError("Requires PIL/Pillow for JPG figures")
77 77 if self.shell is None:
@@ -79,6 +79,12 b' class InlineBackend(InlineBackendConfig):'
79 79 else:
80 80 select_figure_format(self.shell, new)
81 81
82 figure_format = Unicode()
83
84 def _figure_format_changed(self, name, old, new):
85 if new:
86 self.figure_formats = {new}
87
82 88 quality = Int(default_value=90, config=True,
83 89 help="Quality of compression [10-100], currently for lossy JPEG only.")
84 90
General Comments 0
You need to be logged in to leave comments. Login now