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