##// END OF EJS Templates
Fix local import of select_figure_formats....
Thomas Kluyver -
Show More
@@ -1,117 +1,117 b''
1 """Configurable for configuring the IPython inline backend
1 """Configurable for configuring the IPython inline backend
2
2
3 This module does not import anything from matplotlib.
3 This module does not import anything from matplotlib.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2011 The IPython Development Team
6 # Copyright (C) 2011 The IPython Development Team
7 #
7 #
8 # Distributed under the terms of the BSD License. The full license is in
8 # Distributed under the terms of the BSD License. The full license is in
9 # the file COPYING, distributed as part of this software.
9 # the file COPYING, distributed as part of this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 from IPython.config.configurable import SingletonConfigurable
16 from IPython.config.configurable import SingletonConfigurable
17 from IPython.utils.traitlets import (
17 from IPython.utils.traitlets import (
18 Dict, Instance, CaselessStrEnum, Set, Bool, Int, TraitError, Unicode
18 Dict, Instance, CaselessStrEnum, Set, Bool, Int, TraitError, Unicode
19 )
19 )
20 from IPython.utils.warn import warn
20 from IPython.utils.warn import warn
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Configurable for inline backend options
23 # Configurable for inline backend options
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 def pil_available():
26 def pil_available():
27 """Test if PIL/Pillow is available"""
27 """Test if PIL/Pillow is available"""
28 out = False
28 out = False
29 try:
29 try:
30 from PIL import Image
30 from PIL import Image
31 out = True
31 out = True
32 except:
32 except:
33 pass
33 pass
34 return out
34 return out
35
35
36 # inherit from InlineBackendConfig for deprecation purposes
36 # inherit from InlineBackendConfig for deprecation purposes
37 class InlineBackendConfig(SingletonConfigurable):
37 class InlineBackendConfig(SingletonConfigurable):
38 pass
38 pass
39
39
40 class InlineBackend(InlineBackendConfig):
40 class InlineBackend(InlineBackendConfig):
41 """An object to store configuration of the inline backend."""
41 """An object to store configuration of the inline backend."""
42
42
43 def _config_changed(self, name, old, new):
43 def _config_changed(self, name, old, new):
44 # warn on change of renamed config section
44 # warn on change of renamed config section
45 if new.InlineBackendConfig != old.InlineBackendConfig:
45 if new.InlineBackendConfig != old.InlineBackendConfig:
46 warn("InlineBackendConfig has been renamed to InlineBackend")
46 warn("InlineBackendConfig has been renamed to InlineBackend")
47 super(InlineBackend, self)._config_changed(name, old, new)
47 super(InlineBackend, self)._config_changed(name, old, new)
48
48
49 # The typical default figure size is too large for inline use,
49 # The typical default figure size is too large for inline use,
50 # so we shrink the figure size to 6x4, and tweak fonts to
50 # so we shrink the figure size to 6x4, and tweak fonts to
51 # make that fit.
51 # make that fit.
52 rc = Dict({'figure.figsize': (6.0,4.0),
52 rc = Dict({'figure.figsize': (6.0,4.0),
53 # play nicely with white background in the Qt and notebook frontend
53 # play nicely with white background in the Qt and notebook frontend
54 'figure.facecolor': (1,1,1,0),
54 'figure.facecolor': (1,1,1,0),
55 'figure.edgecolor': (1,1,1,0),
55 'figure.edgecolor': (1,1,1,0),
56 # 12pt labels get cutoff on 6x4 logplots, so use 10pt.
56 # 12pt labels get cutoff on 6x4 logplots, so use 10pt.
57 'font.size': 10,
57 'font.size': 10,
58 # 72 dpi matches SVG/qtconsole
58 # 72 dpi matches SVG/qtconsole
59 # this only affects PNG export, as SVG has no dpi setting
59 # this only affects PNG export, as SVG has no dpi setting
60 'savefig.dpi': 72,
60 'savefig.dpi': 72,
61 # 10pt still needs a little more room on the xlabel:
61 # 10pt still needs a little more room on the xlabel:
62 'figure.subplot.bottom' : .125
62 'figure.subplot.bottom' : .125
63 }, config=True,
63 }, config=True,
64 help="""Subset of matplotlib rcParams that should be different for the
64 help="""Subset of matplotlib rcParams that should be different for the
65 inline backend."""
65 inline backend."""
66 )
66 )
67
67
68 figure_formats = Set({'png'}, config=True,
68 figure_formats = Set({'png'}, config=True,
69 help="""A set of figure formats to enable: 'png',
69 help="""A set of figure formats to enable: 'png',
70 'retina', 'jpeg', 'svg', 'pdf'.""")
70 'retina', 'jpeg', 'svg', 'pdf'.""")
71
71
72 def _update_figure_formatters(self):
72 def _update_figure_formatters(self):
73 if self.shell is not None:
73 if self.shell is not None:
74 from IPython.core.pylabtools import select_figure_formats
74 select_figure_formats(self.shell, self.figure_formats, **self.print_figure_kwargs)
75 select_figure_formats(self.shell, self.figure_formats, **self.print_figure_kwargs)
75
76
76 def _figure_formats_changed(self, name, old, new):
77 def _figure_formats_changed(self, name, old, new):
77 from IPython.core.pylabtools import select_figure_formats
78 if 'jpg' in new or 'jpeg' in new:
78 if 'jpg' in new or 'jpeg' in new:
79 if not pil_available():
79 if not pil_available():
80 raise TraitError("Requires PIL/Pillow for JPG figures")
80 raise TraitError("Requires PIL/Pillow for JPG figures")
81 self._update_figure_formatters()
81 self._update_figure_formatters()
82
82
83 figure_format = Unicode(config=True, help="""The figure format to enable (deprecated
83 figure_format = Unicode(config=True, help="""The figure format to enable (deprecated
84 use `figure_formats` instead)""")
84 use `figure_formats` instead)""")
85
85
86 def _figure_format_changed(self, name, old, new):
86 def _figure_format_changed(self, name, old, new):
87 if new:
87 if new:
88 self.figure_formats = {new}
88 self.figure_formats = {new}
89
89
90 print_figure_kwargs = Dict({'bbox_inches' : 'tight'}, config=True,
90 print_figure_kwargs = Dict({'bbox_inches' : 'tight'}, config=True,
91 help="""Extra kwargs to be passed to fig.canvas.print_figure.
91 help="""Extra kwargs to be passed to fig.canvas.print_figure.
92
92
93 Logical examples include: bbox_inches, quality (for jpeg figures), etc.
93 Logical examples include: bbox_inches, quality (for jpeg figures), etc.
94 """
94 """
95 )
95 )
96 _print_figure_kwargs_changed = _update_figure_formatters
96 _print_figure_kwargs_changed = _update_figure_formatters
97
97
98 close_figures = Bool(True, config=True,
98 close_figures = Bool(True, config=True,
99 help="""Close all figures at the end of each cell.
99 help="""Close all figures at the end of each cell.
100
100
101 When True, ensures that each cell starts with no active figures, but it
101 When True, ensures that each cell starts with no active figures, but it
102 also means that one must keep track of references in order to edit or
102 also means that one must keep track of references in order to edit or
103 redraw figures in subsequent cells. This mode is ideal for the notebook,
103 redraw figures in subsequent cells. This mode is ideal for the notebook,
104 where residual plots from other cells might be surprising.
104 where residual plots from other cells might be surprising.
105
105
106 When False, one must call figure() to create new figures. This means
106 When False, one must call figure() to create new figures. This means
107 that gcf() and getfigs() can reference figures created in other cells,
107 that gcf() and getfigs() can reference figures created in other cells,
108 and the active figure can continue to be edited with pylab/pyplot
108 and the active figure can continue to be edited with pylab/pyplot
109 methods that reference the current active figure. This mode facilitates
109 methods that reference the current active figure. This mode facilitates
110 iterative editing of figures, and behaves most consistently with
110 iterative editing of figures, and behaves most consistently with
111 other matplotlib backends, but figure barriers between cells must
111 other matplotlib backends, but figure barriers between cells must
112 be explicit.
112 be explicit.
113 """)
113 """)
114
114
115 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
115 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
116
116
117
117
General Comments 0
You need to be logged in to leave comments. Login now