Show More
@@ -126,13 +126,13 b' def display(*objs, **kwargs):' | |||||
126 | else: |
|
126 | else: | |
127 | continue |
|
127 | continue | |
128 | if raw: |
|
128 | if raw: | |
129 |
publish_display_data( |
|
129 | publish_display_data(data=obj, metadata=metadata) | |
130 | else: |
|
130 | else: | |
131 | format_dict, md_dict = format(obj, include=include, exclude=exclude) |
|
131 | format_dict, md_dict = format(obj, include=include, exclude=exclude) | |
132 | if metadata: |
|
132 | if metadata: | |
133 | # kwarg-specified metadata gets precedence |
|
133 | # kwarg-specified metadata gets precedence | |
134 | _merge(md_dict, metadata) |
|
134 | _merge(md_dict, metadata) | |
135 |
publish_display_data( |
|
135 | publish_display_data(data=format_dict, metadata=md_dict) | |
136 |
|
136 | |||
137 |
|
137 | |||
138 | def display_pretty(*objs, **kwargs): |
|
138 | def display_pretty(*objs, **kwargs): |
@@ -10,22 +10,10 b' There are two components of the display system:' | |||||
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 |
|
||||
14 | Authors: |
|
|||
15 |
|
||||
16 | * Brian Granger |
|
|||
17 | """ |
|
13 | """ | |
18 |
|
14 | |||
19 | #----------------------------------------------------------------------------- |
|
15 | # Copyright (c) IPython Development Team. | |
20 | # Copyright (C) 2008-2011 The IPython Development Team |
|
16 | # Distributed under the terms of the Modified BSD License. | |
21 | # |
|
|||
22 | # Distributed under the terms of the BSD License. The full license is in |
|
|||
23 | # the file COPYING, distributed as part of this software. |
|
|||
24 | #----------------------------------------------------------------------------- |
|
|||
25 |
|
||||
26 | #----------------------------------------------------------------------------- |
|
|||
27 | # Imports |
|
|||
28 | #----------------------------------------------------------------------------- |
|
|||
29 |
|
17 | |||
30 | from __future__ import print_function |
|
18 | from __future__ import print_function | |
31 |
|
19 | |||
@@ -45,29 +33,24 b' class DisplayPublisher(Configurable):' | |||||
45 | be accessed there. |
|
33 | be accessed there. | |
46 | """ |
|
34 | """ | |
47 |
|
35 | |||
48 |
def _validate_data(self |
|
36 | def _validate_data(self, data, metadata=None): | |
49 | """Validate the display data. |
|
37 | """Validate the display data. | |
50 |
|
38 | |||
51 | Parameters |
|
39 | Parameters | |
52 | ---------- |
|
40 | ---------- | |
53 | source : str |
|
|||
54 | The fully dotted name of the callable that created the data, like |
|
|||
55 | :func:`foo.bar.my_formatter`. |
|
|||
56 | data : dict |
|
41 | data : dict | |
57 | The formata data dictionary. |
|
42 | The formata data dictionary. | |
58 | metadata : dict |
|
43 | metadata : dict | |
59 | Any metadata for the data. |
|
44 | Any metadata for the data. | |
60 | """ |
|
45 | """ | |
61 |
|
46 | |||
62 | if not isinstance(source, string_types): |
|
|||
63 | raise TypeError('source must be a str, got: %r' % source) |
|
|||
64 | if not isinstance(data, dict): |
|
47 | if not isinstance(data, dict): | |
65 | raise TypeError('data must be a dict, got: %r' % data) |
|
48 | raise TypeError('data must be a dict, got: %r' % data) | |
66 | if metadata is not None: |
|
49 | if metadata is not None: | |
67 | if not isinstance(metadata, dict): |
|
50 | if not isinstance(metadata, dict): | |
68 | raise TypeError('metadata must be a dict, got: %r' % data) |
|
51 | raise TypeError('metadata must be a dict, got: %r' % data) | |
69 |
|
52 | |||
70 |
def publish(self, |
|
53 | def publish(self, data, metadata=None, source=None): | |
71 | """Publish data and metadata to all frontends. |
|
54 | """Publish data and metadata to all frontends. | |
72 |
|
55 | |||
73 | See the ``display_data`` message in the messaging documentation for |
|
56 | See the ``display_data`` message in the messaging documentation for | |
@@ -87,9 +70,6 b' class DisplayPublisher(Configurable):' | |||||
87 |
|
70 | |||
88 | Parameters |
|
71 | Parameters | |
89 | ---------- |
|
72 | ---------- | |
90 | source : str |
|
|||
91 | A string that give the function or method that created the data, |
|
|||
92 | such as 'IPython.core.page'. |
|
|||
93 | data : dict |
|
73 | data : dict | |
94 | A dictionary having keys that are valid MIME types (like |
|
74 | A dictionary having keys that are valid MIME types (like | |
95 | 'text/plain' or 'image/svg+xml') and values that are the data for |
|
75 | 'text/plain' or 'image/svg+xml') and values that are the data for | |
@@ -104,6 +84,8 b' class DisplayPublisher(Configurable):' | |||||
104 | the data. Metadata specific to each mime-type can be specified |
|
84 | the data. Metadata specific to each mime-type can be specified | |
105 | in the metadata dict with the same mime-type keys as |
|
85 | in the metadata dict with the same mime-type keys as | |
106 | the data itself. |
|
86 | the data itself. | |
|
87 | source : str, deprecated | |||
|
88 | Unused. | |||
107 | """ |
|
89 | """ | |
108 |
|
90 | |||
109 | # The default is to simply write the plain text data using io.stdout. |
|
91 | # The default is to simply write the plain text data using io.stdout. | |
@@ -122,8 +104,8 b' class CapturingDisplayPublisher(DisplayPublisher):' | |||||
122 | """A DisplayPublisher that stores""" |
|
104 | """A DisplayPublisher that stores""" | |
123 | outputs = List() |
|
105 | outputs = List() | |
124 |
|
106 | |||
125 |
def publish(self, |
|
107 | def publish(self, data, metadata=None, source=None): | |
126 |
self.outputs.append(( |
|
108 | self.outputs.append((data, metadata)) | |
127 |
|
109 | |||
128 | def clear_output(self, wait=False): |
|
110 | def clear_output(self, wait=False): | |
129 | super(CapturingDisplayPublisher, self).clear_output(wait) |
|
111 | super(CapturingDisplayPublisher, self).clear_output(wait) | |
@@ -132,7 +114,7 b' class CapturingDisplayPublisher(DisplayPublisher):' | |||||
132 | del self.outputs[:] |
|
114 | del self.outputs[:] | |
133 |
|
115 | |||
134 |
|
116 | |||
135 |
def publish_display_data( |
|
117 | def publish_display_data(data, metadata=None, source=None): | |
136 | """Publish data and metadata to all frontends. |
|
118 | """Publish data and metadata to all frontends. | |
137 |
|
119 | |||
138 | See the ``display_data`` message in the messaging documentation for |
|
120 | See the ``display_data`` message in the messaging documentation for | |
@@ -152,9 +134,6 b' def publish_display_data(source, data, metadata=None):' | |||||
152 |
|
134 | |||
153 | Parameters |
|
135 | Parameters | |
154 | ---------- |
|
136 | ---------- | |
155 | source : str |
|
|||
156 | A string that give the function or method that created the data, |
|
|||
157 | such as 'IPython.core.page'. |
|
|||
158 | data : dict |
|
137 | data : dict | |
159 | A dictionary having keys that are valid MIME types (like |
|
138 | A dictionary having keys that are valid MIME types (like | |
160 | 'text/plain' or 'image/svg+xml') and values that are the data for |
|
139 | 'text/plain' or 'image/svg+xml') and values that are the data for | |
@@ -168,12 +147,13 b' def publish_display_data(source, data, metadata=None):' | |||||
168 | arbitrary key, value pairs that frontends can use to interpret |
|
147 | arbitrary key, value pairs that frontends can use to interpret | |
169 | the data. mime-type keys matching those in data can be used |
|
148 | the data. mime-type keys matching those in data can be used | |
170 | to specify metadata about particular representations. |
|
149 | to specify metadata about particular representations. | |
|
150 | source : str, deprecated | |||
|
151 | Unused. | |||
171 | """ |
|
152 | """ | |
172 | from IPython.core.interactiveshell import InteractiveShell |
|
153 | from IPython.core.interactiveshell import InteractiveShell | |
173 | InteractiveShell.instance().display_pub.publish( |
|
154 | InteractiveShell.instance().display_pub.publish( | |
174 | source, |
|
155 | data=data, | |
175 | data, |
|
156 | metadata=metadata, | |
176 | metadata |
|
|||
177 | ) |
|
157 | ) | |
178 |
|
158 | |||
179 |
|
159 |
@@ -165,7 +165,7 b' class Stream(Reference):' | |||||
165 |
|
165 | |||
166 |
|
166 | |||
167 | class DisplayData(MimeBundle): |
|
167 | class DisplayData(MimeBundle): | |
168 | source = Unicode() |
|
168 | pass | |
169 |
|
169 | |||
170 |
|
170 | |||
171 | class ExecuteResult(MimeBundle): |
|
171 | class ExecuteResult(MimeBundle): |
@@ -73,13 +73,12 b' class ZMQDisplayPublisher(DisplayPublisher):' | |||||
73 | sys.stdout.flush() |
|
73 | sys.stdout.flush() | |
74 | sys.stderr.flush() |
|
74 | sys.stderr.flush() | |
75 |
|
75 | |||
76 |
def publish(self, |
|
76 | def publish(self, data, metadata=None, source=None): | |
77 | self._flush_streams() |
|
77 | self._flush_streams() | |
78 | if metadata is None: |
|
78 | if metadata is None: | |
79 | metadata = {} |
|
79 | metadata = {} | |
80 |
self._validate_data( |
|
80 | self._validate_data(data, metadata) | |
81 | content = {} |
|
81 | content = {} | |
82 | content['source'] = source |
|
|||
83 | content['data'] = encode_images(data) |
|
82 | content['data'] = encode_images(data) | |
84 | content['metadata'] = metadata |
|
83 | content['metadata'] = metadata | |
85 | self.session.send( |
|
84 | self.session.send( |
@@ -90,7 +90,7 b' class ExecuteReply(RichOutput):' | |||||
90 |
|
90 | |||
91 | def display(self): |
|
91 | def display(self): | |
92 | from IPython.display import publish_display_data |
|
92 | from IPython.display import publish_display_data | |
93 |
publish_display_data( |
|
93 | publish_display_data(self.data, self.metadata) | |
94 |
|
94 | |||
95 | def _repr_mime_(self, mime): |
|
95 | def _repr_mime_(self, mime): | |
96 | if mime not in self.data: |
|
96 | if mime not in self.data: |
@@ -1,19 +1,10 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """IO capturing utilities.""" | |
3 | IO capturing utilities. |
|
|||
4 | """ |
|
|||
5 |
|
3 | |||
6 | #----------------------------------------------------------------------------- |
|
4 | # Copyright (c) IPython Development Team. | |
7 | # Copyright (C) 2013 The IPython Development Team |
|
5 | # Distributed under the terms of the Modified BSD License. | |
8 | # |
|
|||
9 | # Distributed under the terms of the BSD License. The full license is in |
|
|||
10 | # the file COPYING, distributed as part of this software. |
|
|||
11 | #----------------------------------------------------------------------------- |
|
|||
12 | from __future__ import print_function, absolute_import |
|
|||
13 |
|
6 | |||
14 | #----------------------------------------------------------------------------- |
|
7 | from __future__ import print_function, absolute_import | |
15 | # Imports |
|
|||
16 | #----------------------------------------------------------------------------- |
|
|||
17 |
|
8 | |||
18 | import sys |
|
9 | import sys | |
19 |
|
10 | |||
@@ -30,14 +21,13 b' else:' | |||||
30 |
|
21 | |||
31 |
|
22 | |||
32 | class RichOutput(object): |
|
23 | class RichOutput(object): | |
33 |
def __init__(self |
|
24 | def __init__(self, data=None, metadata=None): | |
34 | self.source = source |
|
|||
35 | self.data = data or {} |
|
25 | self.data = data or {} | |
36 | self.metadata = metadata or {} |
|
26 | self.metadata = metadata or {} | |
37 |
|
27 | |||
38 | def display(self): |
|
28 | def display(self): | |
39 | from IPython.display import publish_display_data |
|
29 | from IPython.display import publish_display_data | |
40 |
publish_display_data( |
|
30 | publish_display_data(data=self.data, metadata=self.metadata) | |
41 |
|
31 | |||
42 | def _repr_mime_(self, mime): |
|
32 | def _repr_mime_(self, mime): | |
43 | if mime not in self.data: |
|
33 | if mime not in self.data: | |
@@ -118,7 +108,7 b' class CapturedIO(object):' | |||||
118 | for o in c.outputs: |
|
108 | for o in c.outputs: | |
119 | display(o) |
|
109 | display(o) | |
120 | """ |
|
110 | """ | |
121 |
return [ RichOutput( |
|
111 | return [ RichOutput(d, md) for d, md in self._outputs ] | |
122 |
|
112 | |||
123 | def show(self): |
|
113 | def show(self): | |
124 | """write my output to sys.stdout/err as appropriate""" |
|
114 | """write my output to sys.stdout/err as appropriate""" | |
@@ -126,8 +116,8 b' class CapturedIO(object):' | |||||
126 | sys.stderr.write(self.stderr) |
|
116 | sys.stderr.write(self.stderr) | |
127 | sys.stdout.flush() |
|
117 | sys.stdout.flush() | |
128 | sys.stderr.flush() |
|
118 | sys.stderr.flush() | |
129 |
for |
|
119 | for data, metadata in self._outputs: | |
130 |
RichOutput( |
|
120 | RichOutput(data, metadata).display() | |
131 |
|
121 | |||
132 | __call__ = show |
|
122 | __call__ = show | |
133 |
|
123 |
@@ -78,8 +78,7 b' def test_rich_output():' | |||||
78 | """test RichOutput basics""" |
|
78 | """test RichOutput basics""" | |
79 | data = basic_data |
|
79 | data = basic_data | |
80 | metadata = basic_metadata |
|
80 | metadata = basic_metadata | |
81 |
rich = capture.RichOutput( |
|
81 | rich = capture.RichOutput(data=data, metadata=metadata) | |
82 | yield nt.assert_equal, rich.source, "test" |
|
|||
83 | yield nt.assert_equal, rich._repr_html_(), data['text/html'] |
|
82 | yield nt.assert_equal, rich._repr_html_(), data['text/html'] | |
84 | yield nt.assert_equal, rich._repr_png_(), (data['image/png'], metadata['image/png']) |
|
83 | yield nt.assert_equal, rich._repr_png_(), (data['image/png'], metadata['image/png']) | |
85 | yield nt.assert_equal, rich._repr_latex_(), None |
|
84 | yield nt.assert_equal, rich._repr_latex_(), None | |
@@ -89,7 +88,7 b' def test_rich_output():' | |||||
89 | def test_rich_output_no_metadata(): |
|
88 | def test_rich_output_no_metadata(): | |
90 | """test RichOutput with no metadata""" |
|
89 | """test RichOutput with no metadata""" | |
91 | data = full_data |
|
90 | data = full_data | |
92 |
rich = capture.RichOutput( |
|
91 | rich = capture.RichOutput(data=data) | |
93 | for method, mime in _mime_map.items(): |
|
92 | for method, mime in _mime_map.items(): | |
94 | yield nt.assert_equal, getattr(rich, method)(), data[mime] |
|
93 | yield nt.assert_equal, getattr(rich, method)(), data[mime] | |
95 |
|
94 | |||
@@ -97,7 +96,7 b' def test_rich_output_metadata():' | |||||
97 | """test RichOutput with metadata""" |
|
96 | """test RichOutput with metadata""" | |
98 | data = full_data |
|
97 | data = full_data | |
99 | metadata = full_metadata |
|
98 | metadata = full_metadata | |
100 |
rich = capture.RichOutput( |
|
99 | rich = capture.RichOutput(data=data, metadata=metadata) | |
101 | for method, mime in _mime_map.items(): |
|
100 | for method, mime in _mime_map.items(): | |
102 | yield nt.assert_equal, getattr(rich, method)(), (data[mime], metadata[mime]) |
|
101 | yield nt.assert_equal, getattr(rich, method)(), (data[mime], metadata[mime]) | |
103 |
|
102 |
General Comments 0
You need to be logged in to leave comments.
Login now