config.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Configurable for configuring the IPython inline backend
  2. This module does not import anything from matplotlib.
  3. """
  4. # Copyright (c) IPython Development Team.
  5. # Distributed under the terms of the BSD 3-Clause License.
  6. from traitlets.config.configurable import SingletonConfigurable
  7. from traitlets import (
  8. Dict, Instance, Set, Bool, TraitError, Unicode
  9. )
  10. # Configurable for inline backend options
  11. def pil_available():
  12. """Test if PIL/Pillow is available"""
  13. out = False
  14. try:
  15. from PIL import Image # noqa
  16. out = True
  17. except ImportError:
  18. pass
  19. return out
  20. # Inherit from InlineBackendConfig for deprecation purposes
  21. class InlineBackendConfig(SingletonConfigurable):
  22. pass
  23. class InlineBackend(InlineBackendConfig):
  24. """An object to store configuration of the inline backend."""
  25. # While we are deprecating overriding matplotlib defaults out of the
  26. # box, this structure should remain here (empty) for API compatibility
  27. # and the use of other tools that may need it. Specifically Spyder takes
  28. # advantage of it.
  29. # See https://github.com/ipython/ipython/issues/10383 for details.
  30. rc = Dict(
  31. {},
  32. help="""Dict to manage matplotlib configuration defaults in the inline
  33. backend. As of v0.1.4 IPython/Jupyter do not override defaults out of
  34. the box, but third-party tools may use it to manage rc data. To change
  35. personal defaults for matplotlib, use matplotlib's configuration
  36. tools, or customize this class in your `ipython_config.py` file for
  37. IPython/Jupyter-specific usage.""").tag(config=True)
  38. figure_formats = Set(
  39. {'png'},
  40. help="""A set of figure formats to enable: 'png',
  41. 'retina', 'jpeg', 'svg', 'pdf'.""").tag(config=True)
  42. def _update_figure_formatters(self):
  43. if self.shell is not None:
  44. from IPython.core.pylabtools import select_figure_formats
  45. select_figure_formats(self.shell, self.figure_formats, **self.print_figure_kwargs)
  46. def _figure_formats_changed(self, name, old, new):
  47. if 'jpg' in new or 'jpeg' in new:
  48. if not pil_available():
  49. raise TraitError("Requires PIL/Pillow for JPG figures")
  50. self._update_figure_formatters()
  51. figure_format = Unicode(help="""The figure format to enable (deprecated
  52. use `figure_formats` instead)""").tag(config=True)
  53. def _figure_format_changed(self, name, old, new):
  54. if new:
  55. self.figure_formats = {new}
  56. print_figure_kwargs = Dict(
  57. {'bbox_inches': 'tight'},
  58. help="""Extra kwargs to be passed to fig.canvas.print_figure.
  59. Logical examples include: bbox_inches, quality (for jpeg figures), etc.
  60. """
  61. ).tag(config=True)
  62. _print_figure_kwargs_changed = _update_figure_formatters
  63. close_figures = Bool(
  64. True,
  65. help="""Close all figures at the end of each cell.
  66. When True, ensures that each cell starts with no active figures, but it
  67. also means that one must keep track of references in order to edit or
  68. redraw figures in subsequent cells. This mode is ideal for the notebook,
  69. where residual plots from other cells might be surprising.
  70. When False, one must call figure() to create new figures. This means
  71. that gcf() and getfigs() can reference figures created in other cells,
  72. and the active figure can continue to be edited with pylab/pyplot
  73. methods that reference the current active figure. This mode facilitates
  74. iterative editing of figures, and behaves most consistently with
  75. other matplotlib backends, but figure barriers between cells must
  76. be explicit.
  77. """).tag(config=True)
  78. shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
  79. allow_none=True)