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