config.py 4.4 KB

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