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