Show More
@@ -34,6 +34,7 b' from IPython.config.configurable import MultipleInstanceError' | |||||
34 | from IPython.core.application import BaseIPythonApplication |
|
34 | from IPython.core.application import BaseIPythonApplication | |
35 | from IPython.core.profiledir import ProfileDir, ProfileDirError |
|
35 | from IPython.core.profiledir import ProfileDir, ProfileDirError | |
36 |
|
36 | |||
|
37 | from IPython.utils.capture import RichOutput | |||
37 | from IPython.utils.coloransi import TermColors |
|
38 | from IPython.utils.coloransi import TermColors | |
38 | from IPython.utils.jsonutil import rekey |
|
39 | from IPython.utils.jsonutil import rekey | |
39 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS |
|
40 | from IPython.utils.localinterfaces import LOCALHOST, LOCAL_IPS | |
@@ -75,7 +76,7 b' def spin_first(f, self, *args, **kwargs):' | |||||
75 | #-------------------------------------------------------------------------- |
|
76 | #-------------------------------------------------------------------------- | |
76 |
|
77 | |||
77 |
|
78 | |||
78 |
class ExecuteReply( |
|
79 | class ExecuteReply(RichOutput): | |
79 | """wrapper for finished Execute results""" |
|
80 | """wrapper for finished Execute results""" | |
80 | def __init__(self, msg_id, content, metadata): |
|
81 | def __init__(self, msg_id, content, metadata): | |
81 | self.msg_id = msg_id |
|
82 | self.msg_id = msg_id | |
@@ -83,6 +84,39 b' class ExecuteReply(object):' | |||||
83 | self.execution_count = content['execution_count'] |
|
84 | self.execution_count = content['execution_count'] | |
84 | self.metadata = metadata |
|
85 | self.metadata = metadata | |
85 |
|
86 | |||
|
87 | # RichOutput overrides | |||
|
88 | ||||
|
89 | @property | |||
|
90 | def source(self): | |||
|
91 | pyout = self.metadata['pyout'] | |||
|
92 | if pyout: | |||
|
93 | return pyout.get('source', '') | |||
|
94 | ||||
|
95 | @property | |||
|
96 | def data(self): | |||
|
97 | pyout = self.metadata['pyout'] | |||
|
98 | if pyout: | |||
|
99 | return pyout.get('data', {}) | |||
|
100 | ||||
|
101 | @property | |||
|
102 | def _metadata(self): | |||
|
103 | pyout = self.metadata['pyout'] | |||
|
104 | if pyout: | |||
|
105 | return pyout.get('metadata', {}) | |||
|
106 | ||||
|
107 | def display(self): | |||
|
108 | from IPython.display import publish_display_data | |||
|
109 | publish_display_data(self.source, self.data, self.metadata) | |||
|
110 | ||||
|
111 | def _repr_mime_(self, mime): | |||
|
112 | if mime not in self.data: | |||
|
113 | return | |||
|
114 | data = self.data[mime] | |||
|
115 | if mime in self._metadata: | |||
|
116 | return data, self._metadata[mime] | |||
|
117 | else: | |||
|
118 | return data | |||
|
119 | ||||
86 | def __getitem__(self, key): |
|
120 | def __getitem__(self, key): | |
87 | return self.metadata[key] |
|
121 | return self.metadata[key] | |
88 |
|
122 | |||
@@ -129,34 +163,6 b' class ExecuteReply(object):' | |||||
129 | ) + normal + text_out |
|
163 | ) + normal + text_out | |
130 | ) |
|
164 | ) | |
131 |
|
165 | |||
132 | def _repr_html_(self): |
|
|||
133 | pyout = self.metadata['pyout'] or {'data':{}} |
|
|||
134 | return pyout['data'].get("text/html") |
|
|||
135 |
|
||||
136 | def _repr_latex_(self): |
|
|||
137 | pyout = self.metadata['pyout'] or {'data':{}} |
|
|||
138 | return pyout['data'].get("text/latex") |
|
|||
139 |
|
||||
140 | def _repr_json_(self): |
|
|||
141 | pyout = self.metadata['pyout'] or {'data':{}} |
|
|||
142 | return pyout['data'].get("application/json") |
|
|||
143 |
|
||||
144 | def _repr_javascript_(self): |
|
|||
145 | pyout = self.metadata['pyout'] or {'data':{}} |
|
|||
146 | return pyout['data'].get("application/javascript") |
|
|||
147 |
|
||||
148 | def _repr_png_(self): |
|
|||
149 | pyout = self.metadata['pyout'] or {'data':{}} |
|
|||
150 | return pyout['data'].get("image/png") |
|
|||
151 |
|
||||
152 | def _repr_jpeg_(self): |
|
|||
153 | pyout = self.metadata['pyout'] or {'data':{}} |
|
|||
154 | return pyout['data'].get("image/jpeg") |
|
|||
155 |
|
||||
156 | def _repr_svg_(self): |
|
|||
157 | pyout = self.metadata['pyout'] or {'data':{}} |
|
|||
158 | return pyout['data'].get("image/svg+xml") |
|
|||
159 |
|
||||
160 |
|
166 | |||
161 | class Metadata(dict): |
|
167 | class Metadata(dict): | |
162 | """Subclass of dict for initializing metadata values. |
|
168 | """Subclass of dict for initializing metadata values. |
@@ -16,6 +16,7 b' Authors:' | |||||
16 | # Imports |
|
16 | # Imports | |
17 | #------------------------------------------------------------------------------- |
|
17 | #------------------------------------------------------------------------------- | |
18 |
|
18 | |||
|
19 | import base64 | |||
19 | import sys |
|
20 | import sys | |
20 | import platform |
|
21 | import platform | |
21 | import time |
|
22 | import time | |
@@ -536,6 +537,18 b' class TestView(ClusterTestCase, ParametricTestCase):' | |||||
536 | self.assertEqual(str(er), "<ExecuteReply[%i]: 5>" % er.execution_count) |
|
537 | self.assertEqual(str(er), "<ExecuteReply[%i]: 5>" % er.execution_count) | |
537 | self.assertEqual(er.pyout['data']['text/plain'], '5') |
|
538 | self.assertEqual(er.pyout['data']['text/plain'], '5') | |
538 |
|
539 | |||
|
540 | def test_execute_reply_rich(self): | |||
|
541 | e0 = self.client[self.client.ids[0]] | |||
|
542 | e0.block = True | |||
|
543 | e0.execute("from IPython.display import Image, HTML") | |||
|
544 | ar = e0.execute("Image(data=b'garbage', format='png', width=10)", silent=False) | |||
|
545 | er = ar.get() | |||
|
546 | b64data = base64.encodestring(b'garbage').decode('ascii') | |||
|
547 | self.assertEqual(er._repr_png_(), (b64data, dict(width=10))) | |||
|
548 | ar = e0.execute("HTML('<b>bold</b>')", silent=False) | |||
|
549 | er = ar.get() | |||
|
550 | self.assertEqual(er._repr_html_(), "<b>bold</b>") | |||
|
551 | ||||
539 | def test_execute_reply_stdout(self): |
|
552 | def test_execute_reply_stdout(self): | |
540 | e0 = self.client[self.client.ids[0]] |
|
553 | e0 = self.client[self.client.ids[0]] | |
541 | e0.block = True |
|
554 | e0.block = True |
General Comments 0
You need to be logged in to leave comments.
Login now