config.py 3.9 KB

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