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