Show More
@@ -34,6 +34,25 b' def _safe_exists(path):' | |||||
34 | except Exception: |
|
34 | except Exception: | |
35 | return False |
|
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 | # Main functions |
|
57 | # Main functions | |
39 | #----------------------------------------------------------------------------- |
|
58 | #----------------------------------------------------------------------------- | |
@@ -56,38 +75,63 b' def display(*objs, **kwargs):' | |||||
56 | A list of format type strings (MIME types) to exclude in the format |
|
75 | A list of format type strings (MIME types) to exclude in the format | |
57 | data dict. If this is set all format types will be computed, |
|
76 | data dict. If this is set all format types will be computed, | |
58 | except for those included in this argument. |
|
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 | include = kwargs.get('include') |
|
83 | include = kwargs.get('include') | |
61 | exclude = kwargs.get('exclude') |
|
84 | exclude = kwargs.get('exclude') | |
|
85 | metadata = kwargs.get('metadata') | |||
62 |
|
86 | |||
63 | from IPython.core.interactiveshell import InteractiveShell |
|
87 | from IPython.core.interactiveshell import InteractiveShell | |
64 |
|
|
88 | format = InteractiveShell.instance().display_formatter.format | |
65 | format = inst.display_formatter.format |
|
|||
66 | publish = inst.display_pub.publish |
|
|||
67 |
|
89 | |||
68 | for obj in objs: |
|
90 | for obj in objs: | |
69 | format_dict, md_dict = format(obj, include=include, exclude=exclude) |
|
91 | format_dict, md_dict = format(obj, include=include, exclude=exclude) | |
70 | publish('IPython.core.display.display', format_dict, md_dict) |
|
92 | if metadata: | |
|
93 | # kwarg-specified metadata gets precedence | |||
|
94 | _merge(md_dict, metadata) | |||
|
95 | _publish(format_dict, md_dict) | |||
71 |
|
96 | |||
72 |
|
97 | def _display_mimetype(mimetype, objs, raw=False, metadata=None): | ||
73 | def display_pretty(*objs, **kwargs): |
|
98 | """internal implementation of all display_foo methods | |
74 | """Display the pretty (default) representation of an object. |
|
|||
75 |
|
99 | |||
76 | Parameters |
|
100 | Parameters | |
77 | ---------- |
|
101 | ---------- | |
|
102 | mimetype : str | |||
|
103 | The mimetype to be published (e.g. 'image/png') | |||
78 | objs : tuple of objects |
|
104 | objs : tuple of objects | |
79 | The Python objects to display, or if raw=True raw text data to |
|
105 | The Python objects to display, or if raw=True raw text data to | |
80 | display. |
|
106 | display. | |
81 | raw : bool |
|
107 | raw : bool | |
82 | Are the data objects raw data or Python objects that need to be |
|
108 | Are the data objects raw data or Python objects that need to be | |
83 | formatted before display? [default: False] |
|
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 | if raw: |
|
113 | if raw: | |
87 | for obj in objs: |
|
114 | for obj in objs: | |
88 |
publish |
|
115 | _publish({mimetype : obj}, metadata) | |
89 | else: |
|
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 | def display_html(*objs, **kwargs): |
|
137 | def display_html(*objs, **kwargs): | |
@@ -101,13 +145,10 b' def display_html(*objs, **kwargs):' | |||||
101 | raw : bool |
|
145 | raw : bool | |
102 | Are the data objects raw data or Python objects that need to be |
|
146 | Are the data objects raw data or Python objects that need to be | |
103 | formatted before display? [default: False] |
|
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) |
|
151 | _display_mimetype('text/html', objs, **kwargs) | |
106 | if raw: |
|
|||
107 | for obj in objs: |
|
|||
108 | publish_html(obj) |
|
|||
109 | else: |
|
|||
110 | display(*objs, include=['text/plain','text/html']) |
|
|||
111 |
|
152 | |||
112 |
|
153 | |||
113 | def display_svg(*objs, **kwargs): |
|
154 | def display_svg(*objs, **kwargs): | |
@@ -121,13 +162,10 b' def display_svg(*objs, **kwargs):' | |||||
121 | raw : bool |
|
162 | raw : bool | |
122 | Are the data objects raw data or Python objects that need to be |
|
163 | Are the data objects raw data or Python objects that need to be | |
123 | formatted before display? [default: False] |
|
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) |
|
168 | _display_mimetype('image/svg+xml', objs, **kwargs) | |
126 | if raw: |
|
|||
127 | for obj in objs: |
|
|||
128 | publish_svg(obj) |
|
|||
129 | else: |
|
|||
130 | display(*objs, include=['text/plain','image/svg+xml']) |
|
|||
131 |
|
169 | |||
132 |
|
170 | |||
133 | def display_png(*objs, **kwargs): |
|
171 | def display_png(*objs, **kwargs): | |
@@ -141,13 +179,10 b' def display_png(*objs, **kwargs):' | |||||
141 | raw : bool |
|
179 | raw : bool | |
142 | Are the data objects raw data or Python objects that need to be |
|
180 | Are the data objects raw data or Python objects that need to be | |
143 | formatted before display? [default: False] |
|
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) |
|
185 | _display_mimetype('image/png', objs, **kwargs) | |
146 | if raw: |
|
|||
147 | for obj in objs: |
|
|||
148 | publish_png(obj) |
|
|||
149 | else: |
|
|||
150 | display(*objs, include=['text/plain','image/png']) |
|
|||
151 |
|
186 | |||
152 |
|
187 | |||
153 | def display_jpeg(*objs, **kwargs): |
|
188 | def display_jpeg(*objs, **kwargs): | |
@@ -161,13 +196,10 b' def display_jpeg(*objs, **kwargs):' | |||||
161 | raw : bool |
|
196 | raw : bool | |
162 | Are the data objects raw data or Python objects that need to be |
|
197 | Are the data objects raw data or Python objects that need to be | |
163 | formatted before display? [default: False] |
|
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) |
|
202 | _display_mimetype('image/jpeg', objs, **kwargs) | |
166 | if raw: |
|
|||
167 | for obj in objs: |
|
|||
168 | publish_jpeg(obj) |
|
|||
169 | else: |
|
|||
170 | display(*objs, include=['text/plain','image/jpeg']) |
|
|||
171 |
|
203 | |||
172 |
|
204 | |||
173 | def display_latex(*objs, **kwargs): |
|
205 | def display_latex(*objs, **kwargs): | |
@@ -181,13 +213,10 b' def display_latex(*objs, **kwargs):' | |||||
181 | raw : bool |
|
213 | raw : bool | |
182 | Are the data objects raw data or Python objects that need to be |
|
214 | Are the data objects raw data or Python objects that need to be | |
183 | formatted before display? [default: False] |
|
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) |
|
219 | _display_mimetype('text/latex', objs, **kwargs) | |
186 | if raw: |
|
|||
187 | for obj in objs: |
|
|||
188 | publish_latex(obj) |
|
|||
189 | else: |
|
|||
190 | display(*objs, include=['text/plain','text/latex']) |
|
|||
191 |
|
220 | |||
192 |
|
221 | |||
193 | def display_json(*objs, **kwargs): |
|
222 | def display_json(*objs, **kwargs): | |
@@ -203,13 +232,10 b' def display_json(*objs, **kwargs):' | |||||
203 | raw : bool |
|
232 | raw : bool | |
204 | Are the data objects raw data or Python objects that need to be |
|
233 | Are the data objects raw data or Python objects that need to be | |
205 | formatted before display? [default: False] |
|
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) |
|
238 | _display_mimetype('application/json', objs, **kwargs) | |
208 | if raw: |
|
|||
209 | for obj in objs: |
|
|||
210 | publish_json(obj) |
|
|||
211 | else: |
|
|||
212 | display(*objs, include=['text/plain','application/json']) |
|
|||
213 |
|
239 | |||
214 |
|
240 | |||
215 | def display_javascript(*objs, **kwargs): |
|
241 | def display_javascript(*objs, **kwargs): | |
@@ -223,13 +249,10 b' def display_javascript(*objs, **kwargs):' | |||||
223 | raw : bool |
|
249 | raw : bool | |
224 | Are the data objects raw data or Python objects that need to be |
|
250 | Are the data objects raw data or Python objects that need to be | |
225 | formatted before display? [default: False] |
|
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) |
|
255 | _display_mimetype('application/javascript', objs, **kwargs) | |
228 | if raw: |
|
|||
229 | for obj in objs: |
|
|||
230 | publish_javascript(obj) |
|
|||
231 | else: |
|
|||
232 | display(*objs, include=['text/plain','application/javascript']) |
|
|||
233 |
|
256 | |||
234 | #----------------------------------------------------------------------------- |
|
257 | #----------------------------------------------------------------------------- | |
235 | # Smart classes |
|
258 | # Smart classes |
General Comments 0
You need to be logged in to leave comments.
Login now