Show More
@@ -34,6 +34,25 b' def _safe_exists(path):' | |||
|
34 | 34 | except Exception: |
|
35 | 35 | return False |
|
36 | 36 | |
|
37 | def _publish(data, metadata): | |
|
38 | """publish a display message""" | |
|
39 | from IPython.core.interactiveshell import InteractiveShell | |
|
40 | return InteractiveShell.instance().display_pub.publish( | |
|
41 | "display", data, metadata | |
|
42 | ) | |
|
43 | ||
|
44 | def _merge(d1, d2): | |
|
45 | """Like update, but merges sub-dicts instead of clobbering at the top level. | |
|
46 | ||
|
47 | Updates d1 in-place | |
|
48 | """ | |
|
49 | ||
|
50 | if not isinstance(d2, dict) or not isinstance(d1, dict): | |
|
51 | return d2 | |
|
52 | for key, value in d2.items(): | |
|
53 | d1[key] = _merge(d1.get(key), value) | |
|
54 | return d1 | |
|
55 | ||
|
37 | 56 | #----------------------------------------------------------------------------- |
|
38 | 57 | # Main functions |
|
39 | 58 | #----------------------------------------------------------------------------- |
@@ -56,38 +75,63 b' def display(*objs, **kwargs):' | |||
|
56 | 75 | A list of format type strings (MIME types) to exclude in the format |
|
57 | 76 | data dict. If this is set all format types will be computed, |
|
58 | 77 | except for those included in this argument. |
|
78 | metadata : dict, optional | |
|
79 | A dictionary of metadata to associate with the output. | |
|
80 | mime-type keys in this dictionary will be associated with the individual | |
|
81 | representation formats, if they exist. | |
|
59 | 82 | """ |
|
60 | 83 | include = kwargs.get('include') |
|
61 | 84 | exclude = kwargs.get('exclude') |
|
85 | metadata = kwargs.get('metadata') | |
|
62 | 86 | |
|
63 | 87 | from IPython.core.interactiveshell import InteractiveShell |
|
64 |
|
|
|
65 | format = inst.display_formatter.format | |
|
66 | publish = inst.display_pub.publish | |
|
88 | format = InteractiveShell.instance().display_formatter.format | |
|
67 | 89 | |
|
68 | 90 | for obj in objs: |
|
69 | 91 | format_dict, md_dict = format(obj, include=include, exclude=exclude) |
|
70 | publish('IPython.core.display.display', format_dict, md_dict) | |
|
71 | ||
|
92 | if metadata: | |
|
93 | # kwarg-specified metadata gets precedence | |
|
94 | _merge(md_dict, metadata) | |
|
95 | _publish(format_dict, md_dict) | |
|
72 | 96 | |
|
73 | def display_pretty(*objs, **kwargs): | |
|
74 | """Display the pretty (default) representation of an object. | |
|
97 | def _display_mimetype(mimetype, objs, raw=False, metadata=None): | |
|
98 | """internal implementation of all display_foo methods | |
|
75 | 99 | |
|
76 | 100 | Parameters |
|
77 | 101 | ---------- |
|
102 | mimetype : str | |
|
103 | The mimetype to be published (e.g. 'image/png') | |
|
78 | 104 | objs : tuple of objects |
|
79 | 105 | The Python objects to display, or if raw=True raw text data to |
|
80 | 106 | display. |
|
81 | 107 | raw : bool |
|
82 | 108 | Are the data objects raw data or Python objects that need to be |
|
83 | 109 | formatted before display? [default: False] |
|
110 | metadata : dict (optional) | |
|
111 | Metadata to be associated with the output. | |
|
84 | 112 | """ |
|
85 | raw = kwargs.pop('raw',False) | |
|
86 | 113 | if raw: |
|
87 | 114 | for obj in objs: |
|
88 |
publish |
|
|
115 | _publish({mimetype : obj}, metadata) | |
|
89 | 116 | else: |
|
90 |
display(*objs, include=[ |
|
|
117 | display(*objs, metadata=metadata, include=[mimetype]) | |
|
118 | ||
|
119 | ||
|
120 | def display_pretty(*objs, **kwargs): | |
|
121 | """Display the pretty (default) representation of an object. | |
|
122 | ||
|
123 | Parameters | |
|
124 | ---------- | |
|
125 | objs : tuple of objects | |
|
126 | The Python objects to display, or if raw=True raw text data to | |
|
127 | display. | |
|
128 | raw : bool | |
|
129 | Are the data objects raw data or Python objects that need to be | |
|
130 | formatted before display? [default: False] | |
|
131 | metadata : dict (optional) | |
|
132 | Metadata to be associated with the output. | |
|
133 | """ | |
|
134 | _display_mimetype('text/plain', objs, **kwargs) | |
|
91 | 135 | |
|
92 | 136 | |
|
93 | 137 | def display_html(*objs, **kwargs): |
@@ -101,13 +145,10 b' def display_html(*objs, **kwargs):' | |||
|
101 | 145 | raw : bool |
|
102 | 146 | Are the data objects raw data or Python objects that need to be |
|
103 | 147 | formatted before display? [default: False] |
|
148 | metadata : dict (optional) | |
|
149 | Metadata to be associated with the output. | |
|
104 | 150 | """ |
|
105 | raw = kwargs.pop('raw',False) | |
|
106 | if raw: | |
|
107 | for obj in objs: | |
|
108 | publish_html(obj) | |
|
109 | else: | |
|
110 | display(*objs, include=['text/plain','text/html']) | |
|
151 | _display_mimetype('text/html', objs, **kwargs) | |
|
111 | 152 | |
|
112 | 153 | |
|
113 | 154 | def display_svg(*objs, **kwargs): |
@@ -121,13 +162,10 b' def display_svg(*objs, **kwargs):' | |||
|
121 | 162 | raw : bool |
|
122 | 163 | Are the data objects raw data or Python objects that need to be |
|
123 | 164 | formatted before display? [default: False] |
|
165 | metadata : dict (optional) | |
|
166 | Metadata to be associated with the output. | |
|
124 | 167 | """ |
|
125 | raw = kwargs.pop('raw',False) | |
|
126 | if raw: | |
|
127 | for obj in objs: | |
|
128 | publish_svg(obj) | |
|
129 | else: | |
|
130 | display(*objs, include=['text/plain','image/svg+xml']) | |
|
168 | _display_mimetype('image/svg+xml', objs, **kwargs) | |
|
131 | 169 | |
|
132 | 170 | |
|
133 | 171 | def display_png(*objs, **kwargs): |
@@ -141,13 +179,10 b' def display_png(*objs, **kwargs):' | |||
|
141 | 179 | raw : bool |
|
142 | 180 | Are the data objects raw data or Python objects that need to be |
|
143 | 181 | formatted before display? [default: False] |
|
182 | metadata : dict (optional) | |
|
183 | Metadata to be associated with the output. | |
|
144 | 184 | """ |
|
145 | raw = kwargs.pop('raw',False) | |
|
146 | if raw: | |
|
147 | for obj in objs: | |
|
148 | publish_png(obj) | |
|
149 | else: | |
|
150 | display(*objs, include=['text/plain','image/png']) | |
|
185 | _display_mimetype('image/png', objs, **kwargs) | |
|
151 | 186 | |
|
152 | 187 | |
|
153 | 188 | def display_jpeg(*objs, **kwargs): |
@@ -161,13 +196,10 b' def display_jpeg(*objs, **kwargs):' | |||
|
161 | 196 | raw : bool |
|
162 | 197 | Are the data objects raw data or Python objects that need to be |
|
163 | 198 | formatted before display? [default: False] |
|
199 | metadata : dict (optional) | |
|
200 | Metadata to be associated with the output. | |
|
164 | 201 | """ |
|
165 | raw = kwargs.pop('raw',False) | |
|
166 | if raw: | |
|
167 | for obj in objs: | |
|
168 | publish_jpeg(obj) | |
|
169 | else: | |
|
170 | display(*objs, include=['text/plain','image/jpeg']) | |
|
202 | _display_mimetype('image/jpeg', objs, **kwargs) | |
|
171 | 203 | |
|
172 | 204 | |
|
173 | 205 | def display_latex(*objs, **kwargs): |
@@ -181,13 +213,10 b' def display_latex(*objs, **kwargs):' | |||
|
181 | 213 | raw : bool |
|
182 | 214 | Are the data objects raw data or Python objects that need to be |
|
183 | 215 | formatted before display? [default: False] |
|
216 | metadata : dict (optional) | |
|
217 | Metadata to be associated with the output. | |
|
184 | 218 | """ |
|
185 | raw = kwargs.pop('raw',False) | |
|
186 | if raw: | |
|
187 | for obj in objs: | |
|
188 | publish_latex(obj) | |
|
189 | else: | |
|
190 | display(*objs, include=['text/plain','text/latex']) | |
|
219 | _display_mimetype('text/latex', objs, **kwargs) | |
|
191 | 220 | |
|
192 | 221 | |
|
193 | 222 | def display_json(*objs, **kwargs): |
@@ -203,13 +232,10 b' def display_json(*objs, **kwargs):' | |||
|
203 | 232 | raw : bool |
|
204 | 233 | Are the data objects raw data or Python objects that need to be |
|
205 | 234 | formatted before display? [default: False] |
|
235 | metadata : dict (optional) | |
|
236 | Metadata to be associated with the output. | |
|
206 | 237 | """ |
|
207 | raw = kwargs.pop('raw',False) | |
|
208 | if raw: | |
|
209 | for obj in objs: | |
|
210 | publish_json(obj) | |
|
211 | else: | |
|
212 | display(*objs, include=['text/plain','application/json']) | |
|
238 | _display_mimetype('application/json', objs, **kwargs) | |
|
213 | 239 | |
|
214 | 240 | |
|
215 | 241 | def display_javascript(*objs, **kwargs): |
@@ -223,13 +249,10 b' def display_javascript(*objs, **kwargs):' | |||
|
223 | 249 | raw : bool |
|
224 | 250 | Are the data objects raw data or Python objects that need to be |
|
225 | 251 | formatted before display? [default: False] |
|
252 | metadata : dict (optional) | |
|
253 | Metadata to be associated with the output. | |
|
226 | 254 | """ |
|
227 | raw = kwargs.pop('raw',False) | |
|
228 | if raw: | |
|
229 | for obj in objs: | |
|
230 | publish_javascript(obj) | |
|
231 | else: | |
|
232 | display(*objs, include=['text/plain','application/javascript']) | |
|
255 | _display_mimetype('application/javascript', objs, **kwargs) | |
|
233 | 256 | |
|
234 | 257 | #----------------------------------------------------------------------------- |
|
235 | 258 | # Smart classes |
General Comments 0
You need to be logged in to leave comments.
Login now