##// END OF EJS Templates
default values for RichOutput attributes
MinRK -
Show More
@@ -1,173 +1,173 b''
1 1 # encoding: utf-8
2 2 """
3 3 IO capturing utilities.
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2013 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12 from __future__ import print_function
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 import sys
19 19 from StringIO import StringIO
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Classes and functions
23 23 #-----------------------------------------------------------------------------
24 24
25 25
26 26 class RichOutput(object):
27 def __init__(self, source, data, metadata):
27 def __init__(self, source="", data=None, metadata=None):
28 28 self.source = source
29 29 self.data = data or {}
30 30 self.metadata = metadata or {}
31 31
32 32 def display(self):
33 33 from IPython.display import publish_display_data
34 34 publish_display_data(self.source, self.data, self.metadata)
35 35
36 36 def _repr_mime_(self, mime):
37 37 if mime not in self.data:
38 38 return
39 39 data = self.data[mime]
40 40 if mime in self.metadata:
41 41 return data, self.metadata[mime]
42 42 else:
43 43 return data
44 44
45 45 def _repr_html_(self):
46 46 return self._repr_mime_("text/html")
47 47
48 48 def _repr_latex_(self):
49 49 return self._repr_mime_("text/latex")
50 50
51 51 def _repr_json_(self):
52 52 return self._repr_mime_("application/json")
53 53
54 54 def _repr_javascript_(self):
55 55 return self._repr_mime_("application/javascript")
56 56
57 57 def _repr_png_(self):
58 58 return self._repr_mime_("image/png")
59 59
60 60 def _repr_jpeg_(self):
61 return self._repr_mime_("image/jpg")
61 return self._repr_mime_("image/jpeg")
62 62
63 63 def _repr_svg_(self):
64 64 return self._repr_mime_("image/svg+xml")
65 65
66 66
67 67 class CapturedIO(object):
68 68 """Simple object for containing captured stdout/err and rich display StringIO objects
69 69
70 70 Each instance `c` has three attributes:
71 71
72 72 c.stdout : standard output as a string
73 73 c.stderr : standard error as a string
74 74 c.outputs: a list of rich display outputs
75 75
76 76 Additionally, there's a `c.show()` method which will print all of the
77 77 above in the same order, and can be invoked simply via `c()`.
78 78 """
79 79
80 80 def __init__(self, stdout, stderr, outputs=None):
81 81 self._stdout = stdout
82 82 self._stderr = stderr
83 83 if outputs is None:
84 84 outputs = []
85 85 self._outputs = outputs
86 86
87 87 def __str__(self):
88 88 return self.stdout
89 89
90 90 @property
91 91 def stdout(self):
92 92 "Captured standard output"
93 93 if not self._stdout:
94 94 return ''
95 95 return self._stdout.getvalue()
96 96
97 97 @property
98 98 def stderr(self):
99 99 "Captured standard error"
100 100 if not self._stderr:
101 101 return ''
102 102 return self._stderr.getvalue()
103 103
104 104 @property
105 105 def outputs(self):
106 106 """A list of the captured rich display outputs, if any.
107 107
108 108 If you have a CapturedIO object `c`, these can be displayed in IPython
109 109 using:
110 110
111 111 from IPython.display import display
112 112 for o in c.outputs:
113 113 display(o)
114 114 """
115 115 return [ RichOutput(s, d, md) for s, d, md in self._outputs ]
116 116
117 117 def show(self):
118 118 """write my output to sys.stdout/err as appropriate"""
119 119 sys.stdout.write(self.stdout)
120 120 sys.stderr.write(self.stderr)
121 121 sys.stdout.flush()
122 122 sys.stderr.flush()
123 123 for source, data, metadata in self._outputs:
124 124 RichOutput(source, data, metadata).display()
125 125
126 126 __call__ = show
127 127
128 128
129 129 class capture_output(object):
130 130 """context manager for capturing stdout/err"""
131 131 stdout = True
132 132 stderr = True
133 133 display = True
134 134
135 135 def __init__(self, stdout=True, stderr=True, display=True):
136 136 self.stdout = stdout
137 137 self.stderr = stderr
138 138 self.display = display
139 139 self.shell = None
140 140
141 141 def __enter__(self):
142 142 from IPython.core.getipython import get_ipython
143 143 from IPython.core.displaypub import CapturingDisplayPublisher
144 144
145 145 self.sys_stdout = sys.stdout
146 146 self.sys_stderr = sys.stderr
147 147
148 148 if self.display:
149 149 self.shell = get_ipython()
150 150 if self.shell is None:
151 151 self.save_display_pub = None
152 152 self.display = False
153 153
154 154 stdout = stderr = outputs = None
155 155 if self.stdout:
156 156 stdout = sys.stdout = StringIO()
157 157 if self.stderr:
158 158 stderr = sys.stderr = StringIO()
159 159 if self.display:
160 160 self.save_display_pub = self.shell.display_pub
161 161 self.shell.display_pub = CapturingDisplayPublisher()
162 162 outputs = self.shell.display_pub.outputs
163 163
164 164
165 165 return CapturedIO(stdout, stderr, outputs)
166 166
167 167 def __exit__(self, exc_type, exc_value, traceback):
168 168 sys.stdout = self.sys_stdout
169 169 sys.stderr = self.sys_stderr
170 170 if self.display and self.shell:
171 171 self.shell.display_pub = self.save_display_pub
172 172
173 173
General Comments 0
You need to be logged in to leave comments. Login now