displaypub.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """An interface for publishing rich data to frontends.
  2. There are two components of the display system:
  3. * Display formatters, which take a Python object and compute the
  4. representation of the object in various formats (text, HTML, SVG, etc.).
  5. * The display publisher that is used to send the representation data to the
  6. various frontends.
  7. This module defines the logic display publishing. The display publisher uses
  8. the ``display_data`` message type that is defined in the IPython messaging
  9. spec.
  10. """
  11. # Copyright (c) IPython Development Team.
  12. # Distributed under the terms of the Modified BSD License.
  13. from __future__ import print_function
  14. import sys
  15. from traitlets.config.configurable import Configurable
  16. from traitlets import List
  17. # This used to be defined here - it is imported for backwards compatibility
  18. from .display import publish_display_data
  19. #-----------------------------------------------------------------------------
  20. # Main payload class
  21. #-----------------------------------------------------------------------------
  22. class DisplayPublisher(Configurable):
  23. """A traited class that publishes display data to frontends.
  24. Instances of this class are created by the main IPython object and should
  25. be accessed there.
  26. """
  27. def _validate_data(self, data, metadata=None):
  28. """Validate the display data.
  29. Parameters
  30. ----------
  31. data : dict
  32. The formata data dictionary.
  33. metadata : dict
  34. Any metadata for the data.
  35. """
  36. if not isinstance(data, dict):
  37. raise TypeError('data must be a dict, got: %r' % data)
  38. if metadata is not None:
  39. if not isinstance(metadata, dict):
  40. raise TypeError('metadata must be a dict, got: %r' % data)
  41. def publish(self, data, metadata=None, source=None):
  42. """Publish data and metadata to all frontends.
  43. See the ``display_data`` message in the messaging documentation for
  44. more details about this message type.
  45. The following MIME types are currently implemented:
  46. * text/plain
  47. * text/html
  48. * text/markdown
  49. * text/latex
  50. * application/json
  51. * application/javascript
  52. * image/png
  53. * image/jpeg
  54. * image/svg+xml
  55. Parameters
  56. ----------
  57. data : dict
  58. A dictionary having keys that are valid MIME types (like
  59. 'text/plain' or 'image/svg+xml') and values that are the data for
  60. that MIME type. The data itself must be a JSON'able data
  61. structure. Minimally all data should have the 'text/plain' data,
  62. which can be displayed by all frontends. If more than the plain
  63. text is given, it is up to the frontend to decide which
  64. representation to use.
  65. metadata : dict
  66. A dictionary for metadata related to the data. This can contain
  67. arbitrary key, value pairs that frontends can use to interpret
  68. the data. Metadata specific to each mime-type can be specified
  69. in the metadata dict with the same mime-type keys as
  70. the data itself.
  71. source : str, deprecated
  72. Unused.
  73. """
  74. # The default is to simply write the plain text data using sys.stdout.
  75. if 'text/plain' in data:
  76. print(data['text/plain'])
  77. def clear_output(self, wait=False):
  78. """Clear the output of the cell receiving output."""
  79. print('\033[2K\r', end='')
  80. sys.stdout.flush()
  81. print('\033[2K\r', end='')
  82. sys.stderr.flush()
  83. class CapturingDisplayPublisher(DisplayPublisher):
  84. """A DisplayPublisher that stores"""
  85. outputs = List()
  86. def publish(self, data, metadata=None, source=None):
  87. self.outputs.append((data, metadata))
  88. def clear_output(self, wait=False):
  89. super(CapturingDisplayPublisher, self).clear_output(wait)
  90. # empty the list, *do not* reassign a new list
  91. del self.outputs[:]