##// END OF EJS Templates
Fixed typo in displaypub...
Jonathan Frederic -
Show More
@@ -1,177 +1,177 b''
1 1 """An interface for publishing rich data to frontends.
2 2
3 3 There are two components of the display system:
4 4
5 5 * Display formatters, which take a Python object and compute the
6 6 representation of the object in various formats (text, HTML, SVG, etc.).
7 7 * The display publisher that is used to send the representation data to the
8 8 various frontends.
9 9
10 10 This module defines the logic display publishing. The display publisher uses
11 11 the ``display_data`` message type that is defined in the IPython messaging
12 12 spec.
13 13
14 14 Authors:
15 15
16 16 * Brian Granger
17 17 """
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Copyright (C) 2008-2011 The IPython Development Team
21 21 #
22 22 # Distributed under the terms of the BSD License. The full license is in
23 23 # the file COPYING, distributed as part of this software.
24 24 #-----------------------------------------------------------------------------
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Imports
28 28 #-----------------------------------------------------------------------------
29 29
30 30 from __future__ import print_function
31 31
32 32 from IPython.config.configurable import Configurable
33 33 from IPython.utils import io
34 34 from IPython.utils.py3compat import string_types
35 35 from IPython.utils.traitlets import List
36 36
37 37 #-----------------------------------------------------------------------------
38 38 # Main payload class
39 39 #-----------------------------------------------------------------------------
40 40
41 41 class DisplayPublisher(Configurable):
42 42 """A traited class that publishes display data to frontends.
43 43
44 44 Instances of this class are created by the main IPython object and should
45 45 be accessed there.
46 46 """
47 47
48 48 def _validate_data(self, source, data, metadata=None):
49 49 """Validate the display data.
50 50
51 51 Parameters
52 52 ----------
53 53 source : str
54 54 The fully dotted name of the callable that created the data, like
55 55 :func:`foo.bar.my_formatter`.
56 56 data : dict
57 57 The formata data dictionary.
58 58 metadata : dict
59 59 Any metadata for the data.
60 60 """
61 61
62 62 if not isinstance(source, string_types):
63 63 raise TypeError('source must be a str, got: %r' % source)
64 64 if not isinstance(data, dict):
65 65 raise TypeError('data must be a dict, got: %r' % data)
66 66 if metadata is not None:
67 67 if not isinstance(metadata, dict):
68 68 raise TypeError('metadata must be a dict, got: %r' % data)
69 69
70 70 def publish(self, source, data, metadata=None):
71 71 """Publish data and metadata to all frontends.
72 72
73 73 See the ``display_data`` message in the messaging documentation for
74 74 more details about this message type.
75 75
76 76 The following MIME types are currently implemented:
77 77
78 78 * text/plain
79 79 * text/html
80 80 * text/latex
81 81 * application/json
82 82 * application/javascript
83 83 * image/png
84 84 * image/jpeg
85 85 * image/svg+xml
86 86
87 87 Parameters
88 88 ----------
89 89 source : str
90 90 A string that give the function or method that created the data,
91 91 such as 'IPython.core.page'.
92 92 data : dict
93 93 A dictionary having keys that are valid MIME types (like
94 94 'text/plain' or 'image/svg+xml') and values that are the data for
95 95 that MIME type. The data itself must be a JSON'able data
96 96 structure. Minimally all data should have the 'text/plain' data,
97 97 which can be displayed by all frontends. If more than the plain
98 98 text is given, it is up to the frontend to decide which
99 99 representation to use.
100 100 metadata : dict
101 101 A dictionary for metadata related to the data. This can contain
102 102 arbitrary key, value pairs that frontends can use to interpret
103 103 the data. Metadata specific to each mime-type can be specified
104 104 in the metadata dict with the same mime-type keys as
105 105 the data itself.
106 106 """
107 107
108 108 # The default is to simply write the plain text data using io.stdout.
109 109 if 'text/plain' in data:
110 110 print(data['text/plain'], file=io.stdout)
111 111
112 112 def clear_output(self, wait=False):
113 113 """Clear the output of the cell receiving output."""
114 114 print('\033[2K\r', file=io.stdout, end='')
115 115 io.stdout.flush()
116 116 print('\033[2K\r', file=io.stderr, end='')
117 117 io.stderr.flush()
118 118
119 119
120 120 class CapturingDisplayPublisher(DisplayPublisher):
121 121 """A DisplayPublisher that stores"""
122 122 outputs = List()
123 123
124 124 def publish(self, source, data, metadata=None):
125 125 self.outputs.append((source, data, metadata))
126 126
127 127 def clear_output(self, wait=False):
128 128 super(CapturingDisplayPublisher, self).clear_output(wait)
129 if other:
130 # empty the list, *do not* reassign a new list
131 del self.outputs[:]
129
130 # empty the list, *do not* reassign a new list
131 del self.outputs[:]
132 132
133 133
134 134 def publish_display_data(source, data, metadata=None):
135 135 """Publish data and metadata to all frontends.
136 136
137 137 See the ``display_data`` message in the messaging documentation for
138 138 more details about this message type.
139 139
140 140 The following MIME types are currently implemented:
141 141
142 142 * text/plain
143 143 * text/html
144 144 * text/latex
145 145 * application/json
146 146 * application/javascript
147 147 * image/png
148 148 * image/jpeg
149 149 * image/svg+xml
150 150
151 151 Parameters
152 152 ----------
153 153 source : str
154 154 A string that give the function or method that created the data,
155 155 such as 'IPython.core.page'.
156 156 data : dict
157 157 A dictionary having keys that are valid MIME types (like
158 158 'text/plain' or 'image/svg+xml') and values that are the data for
159 159 that MIME type. The data itself must be a JSON'able data
160 160 structure. Minimally all data should have the 'text/plain' data,
161 161 which can be displayed by all frontends. If more than the plain
162 162 text is given, it is up to the frontend to decide which
163 163 representation to use.
164 164 metadata : dict
165 165 A dictionary for metadata related to the data. This can contain
166 166 arbitrary key, value pairs that frontends can use to interpret
167 167 the data. mime-type keys matching those in data can be used
168 168 to specify metadata about particular representations.
169 169 """
170 170 from IPython.core.interactiveshell import InteractiveShell
171 171 InteractiveShell.instance().display_pub.publish(
172 172 source,
173 173 data,
174 174 metadata
175 175 )
176 176
177 177
General Comments 0
You need to be logged in to leave comments. Login now