##// END OF EJS Templates
Adding transient and update, change to dict for outputs
Henry Fredrick Schreiner -
Show More
@@ -129,10 +129,11 b' class CapturingDisplayPublisher(DisplayPublisher):'
129 transient = kwargs.pop('transient', None)
129 transient = kwargs.pop('transient', None)
130 update = kwargs.pop('update', False)
130 update = kwargs.pop('update', False)
131
131
132 self.outputs.append((data, metadata))
132 self.outputs.append({'data':data, 'metadata':metadata,
133 'transient':transient, 'update':update})
133
134
134 def clear_output(self, wait=False):
135 def clear_output(self, wait=False):
135 super(CapturingDisplayPublisher, self).clear_output(wait)
136 super(CapturingDisplayPublisher, self).clear_output(wait)
136
137
137 # empty the list, *do not* reassign a new list
138 # empty the list, *do not* reassign a new list
138 del self.outputs[:]
139 self.outputs.clear()
@@ -21,14 +21,17 b' else:'
21
21
22
22
23 class RichOutput(object):
23 class RichOutput(object):
24 def __init__(self, data=None, metadata=None):
24 def __init__(self, data=None, metadata=None, transient=None, update=False):
25 self.data = data or {}
25 self.data = data or {}
26 self.metadata = metadata or {}
26 self.metadata = metadata or {}
27
27 self.transient = transient or {}
28 self.update = update
29
28 def display(self):
30 def display(self):
29 from IPython.display import publish_display_data
31 from IPython.display import publish_display_data
30 publish_display_data(data=self.data, metadata=self.metadata)
32 publish_display_data(data=self.data, metadata=self.metadata,
31
33 transient=self.transient, update=self.update)
34
32 def _repr_mime_(self, mime):
35 def _repr_mime_(self, mime):
33 if mime not in self.data:
36 if mime not in self.data:
34 return
37 return
@@ -40,22 +43,22 b' class RichOutput(object):'
40
43
41 def _repr_html_(self):
44 def _repr_html_(self):
42 return self._repr_mime_("text/html")
45 return self._repr_mime_("text/html")
43
46
44 def _repr_latex_(self):
47 def _repr_latex_(self):
45 return self._repr_mime_("text/latex")
48 return self._repr_mime_("text/latex")
46
49
47 def _repr_json_(self):
50 def _repr_json_(self):
48 return self._repr_mime_("application/json")
51 return self._repr_mime_("application/json")
49
52
50 def _repr_javascript_(self):
53 def _repr_javascript_(self):
51 return self._repr_mime_("application/javascript")
54 return self._repr_mime_("application/javascript")
52
55
53 def _repr_png_(self):
56 def _repr_png_(self):
54 return self._repr_mime_("image/png")
57 return self._repr_mime_("image/png")
55
58
56 def _repr_jpeg_(self):
59 def _repr_jpeg_(self):
57 return self._repr_mime_("image/jpeg")
60 return self._repr_mime_("image/jpeg")
58
61
59 def _repr_svg_(self):
62 def _repr_svg_(self):
60 return self._repr_mime_("image/svg+xml")
63 return self._repr_mime_("image/svg+xml")
61
64
@@ -72,35 +75,35 b' class CapturedIO(object):'
72 Additionally, there's a ``c.show()`` method which will print all of the
75 Additionally, there's a ``c.show()`` method which will print all of the
73 above in the same order, and can be invoked simply via ``c()``.
76 above in the same order, and can be invoked simply via ``c()``.
74 """
77 """
75
78
76 def __init__(self, stdout, stderr, outputs=None):
79 def __init__(self, stdout, stderr, outputs=None):
77 self._stdout = stdout
80 self._stdout = stdout
78 self._stderr = stderr
81 self._stderr = stderr
79 if outputs is None:
82 if outputs is None:
80 outputs = []
83 outputs = []
81 self._outputs = outputs
84 self._outputs = outputs
82
85
83 def __str__(self):
86 def __str__(self):
84 return self.stdout
87 return self.stdout
85
88
86 @property
89 @property
87 def stdout(self):
90 def stdout(self):
88 "Captured standard output"
91 "Captured standard output"
89 if not self._stdout:
92 if not self._stdout:
90 return ''
93 return ''
91 return self._stdout.getvalue()
94 return self._stdout.getvalue()
92
95
93 @property
96 @property
94 def stderr(self):
97 def stderr(self):
95 "Captured standard error"
98 "Captured standard error"
96 if not self._stderr:
99 if not self._stderr:
97 return ''
100 return ''
98 return self._stderr.getvalue()
101 return self._stderr.getvalue()
99
102
100 @property
103 @property
101 def outputs(self):
104 def outputs(self):
102 """A list of the captured rich display outputs, if any.
105 """A list of the captured rich display outputs, if any.
103
106
104 If you have a CapturedIO object ``c``, these can be displayed in IPython
107 If you have a CapturedIO object ``c``, these can be displayed in IPython
105 using::
108 using::
106
109
@@ -108,17 +111,17 b' class CapturedIO(object):'
108 for o in c.outputs:
111 for o in c.outputs:
109 display(o)
112 display(o)
110 """
113 """
111 return [ RichOutput(d, md) for d, md in self._outputs ]
114 return [ RichOutput(**kargs) for kargs in self._outputs ]
112
115
113 def show(self):
116 def show(self):
114 """write my output to sys.stdout/err as appropriate"""
117 """write my output to sys.stdout/err as appropriate"""
115 sys.stdout.write(self.stdout)
118 sys.stdout.write(self.stdout)
116 sys.stderr.write(self.stderr)
119 sys.stderr.write(self.stderr)
117 sys.stdout.flush()
120 sys.stdout.flush()
118 sys.stderr.flush()
121 sys.stderr.flush()
119 for data, metadata in self._outputs:
122 for kargs in self._outputs:
120 RichOutput(data, metadata).display()
123 RichOutput(**kargs).display()
121
124
122 __call__ = show
125 __call__ = show
123
126
124
127
@@ -127,27 +130,27 b' class capture_output(object):'
127 stdout = True
130 stdout = True
128 stderr = True
131 stderr = True
129 display = True
132 display = True
130
133
131 def __init__(self, stdout=True, stderr=True, display=True):
134 def __init__(self, stdout=True, stderr=True, display=True):
132 self.stdout = stdout
135 self.stdout = stdout
133 self.stderr = stderr
136 self.stderr = stderr
134 self.display = display
137 self.display = display
135 self.shell = None
138 self.shell = None
136
139
137 def __enter__(self):
140 def __enter__(self):
138 from IPython.core.getipython import get_ipython
141 from IPython.core.getipython import get_ipython
139 from IPython.core.displaypub import CapturingDisplayPublisher
142 from IPython.core.displaypub import CapturingDisplayPublisher
140 from IPython.core.displayhook import CapturingDisplayHook
143 from IPython.core.displayhook import CapturingDisplayHook
141
144
142 self.sys_stdout = sys.stdout
145 self.sys_stdout = sys.stdout
143 self.sys_stderr = sys.stderr
146 self.sys_stderr = sys.stderr
144
147
145 if self.display:
148 if self.display:
146 self.shell = get_ipython()
149 self.shell = get_ipython()
147 if self.shell is None:
150 if self.shell is None:
148 self.save_display_pub = None
151 self.save_display_pub = None
149 self.display = False
152 self.display = False
150
153
151 stdout = stderr = outputs = None
154 stdout = stderr = outputs = None
152 if self.stdout:
155 if self.stdout:
153 stdout = sys.stdout = StringIO()
156 stdout = sys.stdout = StringIO()
@@ -160,9 +163,9 b' class capture_output(object):'
160 self.save_display_hook = sys.displayhook
163 self.save_display_hook = sys.displayhook
161 sys.displayhook = CapturingDisplayHook(shell=self.shell,
164 sys.displayhook = CapturingDisplayHook(shell=self.shell,
162 outputs=outputs)
165 outputs=outputs)
163
166
164 return CapturedIO(stdout, stderr, outputs)
167 return CapturedIO(stdout, stderr, outputs)
165
168
166 def __exit__(self, exc_type, exc_value, traceback):
169 def __exit__(self, exc_type, exc_value, traceback):
167 sys.stdout = self.sys_stdout
170 sys.stdout = self.sys_stdout
168 sys.stderr = self.sys_stderr
171 sys.stderr = self.sys_stderr
General Comments 0
You need to be logged in to leave comments. Login now