Show More
@@ -17,6 +17,13 b' Authors:' | |||||
17 | # Imports |
|
17 | # Imports | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
|
20 | from .displaypub import ( | |||
|
21 | publish_pretty, publish_html, | |||
|
22 | publish_latex, publish_svg, | |||
|
23 | publish_png, publish_json, | |||
|
24 | publish_javascript | |||
|
25 | ) | |||
|
26 | ||||
20 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
21 | # Main functions |
|
28 | # Main functions | |
22 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
@@ -53,78 +60,215 b' def display(*objs, **kwargs):' | |||||
53 | publish('IPython.core.display.display', format_dict) |
|
60 | publish('IPython.core.display.display', format_dict) | |
54 |
|
61 | |||
55 |
|
62 | |||
56 | def display_pretty(*objs): |
|
63 | def display_pretty(*objs, **kwargs): | |
57 | """Display the pretty (default) representation of an object. |
|
64 | """Display the pretty (default) representation of an object. | |
58 |
|
65 | |||
59 | Parameters |
|
66 | Parameters | |
60 | ---------- |
|
67 | ---------- | |
61 | objs : tuple of objects |
|
68 | objs : tuple of objects | |
62 |
The Python objects to display |
|
69 | The Python objects to display, or if raw=True raw text data to | |
|
70 | display. | |||
|
71 | raw : bool | |||
|
72 | Are the data objects raw data or Python objects that need to be | |||
|
73 | formatted before display? [default: False] | |||
63 | """ |
|
74 | """ | |
64 | display(*objs, include=['text/plain']) |
|
75 | raw = kwargs.pop('raw',False) | |
|
76 | if raw: | |||
|
77 | for obj in objs: | |||
|
78 | publish_pretty(obj) | |||
|
79 | else: | |||
|
80 | display(*objs, include=['text/plain']) | |||
65 |
|
81 | |||
66 |
|
82 | |||
67 | def display_html(*objs): |
|
83 | def display_html(*objs, **kwargs): | |
68 | """Display the HTML representation of an object. |
|
84 | """Display the HTML representation of an object. | |
69 |
|
85 | |||
70 | Parameters |
|
86 | Parameters | |
71 | ---------- |
|
87 | ---------- | |
72 | objs : tuple of objects |
|
88 | objs : tuple of objects | |
73 |
The Python objects to display |
|
89 | The Python objects to display, or if raw=True raw html data to | |
74 | """ |
|
90 | display. | |
75 | display(*objs, include=['text/plain','text/html']) |
|
91 | raw : bool | |
|
92 | Are the data objects raw data or Python objects that need to be | |||
|
93 | formatted before display? [default: False] | |||
|
94 | """ | |||
|
95 | raw = kwargs.pop('raw',False) | |||
|
96 | if raw: | |||
|
97 | for obj in objs: | |||
|
98 | publish_html(obj) | |||
|
99 | else: | |||
|
100 | display(*objs, include=['text/plain','text/html']) | |||
76 |
|
101 | |||
77 |
|
102 | |||
78 | def display_svg(*objs): |
|
103 | def display_svg(*objs, **kwargs): | |
79 | """Display the SVG representation of an object. |
|
104 | """Display the SVG representation of an object. | |
80 |
|
105 | |||
81 | Parameters |
|
106 | Parameters | |
82 | ---------- |
|
107 | ---------- | |
83 | objs : tuple of objects |
|
108 | objs : tuple of objects | |
84 |
The Python objects to display |
|
109 | The Python objects to display, or if raw=True raw svg data to | |
|
110 | display. | |||
|
111 | raw : bool | |||
|
112 | Are the data objects raw data or Python objects that need to be | |||
|
113 | formatted before display? [default: False] | |||
85 | """ |
|
114 | """ | |
86 | display(*objs, include=['text/plain','image/svg+xml']) |
|
115 | raw = kwargs.pop('raw',False) | |
|
116 | if raw: | |||
|
117 | for obj in objs: | |||
|
118 | publish_svg(obj) | |||
|
119 | else: | |||
|
120 | display(*objs, include=['text/plain','image/svg+xml']) | |||
87 |
|
121 | |||
88 |
|
122 | |||
89 | def display_png(*objs): |
|
123 | def display_png(*objs, **kwargs): | |
90 | """Display the PNG representation of an object. |
|
124 | """Display the PNG representation of an object. | |
91 |
|
125 | |||
92 | Parameters |
|
126 | Parameters | |
93 | ---------- |
|
127 | ---------- | |
94 | objs : tuple of objects |
|
128 | objs : tuple of objects | |
95 |
The Python objects to display |
|
129 | The Python objects to display, or if raw=True raw png data to | |
|
130 | display. | |||
|
131 | raw : bool | |||
|
132 | Are the data objects raw data or Python objects that need to be | |||
|
133 | formatted before display? [default: False] | |||
96 | """ |
|
134 | """ | |
97 | display(*objs, include=['text/plain','image/png']) |
|
135 | raw = kwargs.pop('raw',False) | |
|
136 | if raw: | |||
|
137 | for obj in objs: | |||
|
138 | publish_png(obj) | |||
|
139 | else: | |||
|
140 | display(*objs, include=['text/plain','image/png']) | |||
98 |
|
141 | |||
99 |
|
142 | |||
100 | def display_latex(*objs): |
|
143 | def display_latex(*objs, **kwargs): | |
101 | """Display the LaTeX representation of an object. |
|
144 | """Display the LaTeX representation of an object. | |
102 |
|
145 | |||
103 | Parameters |
|
146 | Parameters | |
104 | ---------- |
|
147 | ---------- | |
105 | objs : tuple of objects |
|
148 | objs : tuple of objects | |
106 |
The Python objects to display |
|
149 | The Python objects to display, or if raw=True raw latex data to | |
|
150 | display. | |||
|
151 | raw : bool | |||
|
152 | Are the data objects raw data or Python objects that need to be | |||
|
153 | formatted before display? [default: False] | |||
107 | """ |
|
154 | """ | |
108 | display(*objs, include=['text/plain','text/latex']) |
|
155 | raw = kwargs.pop('raw',False) | |
|
156 | if raw: | |||
|
157 | for obj in objs: | |||
|
158 | publish_latex(obj) | |||
|
159 | else: | |||
|
160 | display(*objs, include=['text/plain','text/latex']) | |||
109 |
|
161 | |||
110 |
|
162 | |||
111 | def display_json(*objs): |
|
163 | def display_json(*objs, **kwargs): | |
112 | """Display the JSON representation of an object. |
|
164 | """Display the JSON representation of an object. | |
113 |
|
165 | |||
114 | Parameters |
|
166 | Parameters | |
115 | ---------- |
|
167 | ---------- | |
116 | objs : tuple of objects |
|
168 | objs : tuple of objects | |
117 |
The Python objects to display |
|
169 | The Python objects to display, or if raw=True raw json data to | |
|
170 | display. | |||
|
171 | raw : bool | |||
|
172 | Are the data objects raw data or Python objects that need to be | |||
|
173 | formatted before display? [default: False] | |||
118 | """ |
|
174 | """ | |
119 | display(*objs, include=['text/plain','application/json']) |
|
175 | raw = kwargs.pop('raw',False) | |
|
176 | if raw: | |||
|
177 | for obj in objs: | |||
|
178 | publish_json(obj) | |||
|
179 | else: | |||
|
180 | display(*objs, include=['text/plain','application/json']) | |||
120 |
|
181 | |||
121 |
|
182 | |||
122 | def display_javascript(*objs): |
|
183 | def display_javascript(*objs, **kwargs): | |
123 | """Display the Javascript representation of an object. |
|
184 | """Display the Javascript representation of an object. | |
124 |
|
185 | |||
125 | Parameters |
|
186 | Parameters | |
126 | ---------- |
|
187 | ---------- | |
127 | objs : tuple of objects |
|
188 | objs : tuple of objects | |
128 |
The Python objects to display |
|
189 | The Python objects to display, or if raw=True raw javascript data to | |
|
190 | display. | |||
|
191 | raw : bool | |||
|
192 | Are the data objects raw data or Python objects that need to be | |||
|
193 | formatted before display? [default: False] | |||
129 | """ |
|
194 | """ | |
130 | display(*objs, include=['text/plain','application/javascript']) |
|
195 | raw = kwargs.pop('raw',False) | |
|
196 | if raw: | |||
|
197 | for obj in objs: | |||
|
198 | publish_javascript(obj) | |||
|
199 | else: | |||
|
200 | display(*objs, include=['text/plain','application/javascript']) | |||
|
201 | ||||
|
202 | #----------------------------------------------------------------------------- | |||
|
203 | # Smart classes | |||
|
204 | #----------------------------------------------------------------------------- | |||
|
205 | ||||
|
206 | ||||
|
207 | class DisplayObject(object): | |||
|
208 | """An object that wraps data to be displayed.""" | |||
|
209 | ||||
|
210 | def __init__(self, data): | |||
|
211 | """Create a display object given raw data of a MIME type or a URL. | |||
|
212 | ||||
|
213 | When this object is returned by an expression or passed to the | |||
|
214 | display function, it will result in the data being displayed | |||
|
215 | in the frontend. The MIME type of the data should match the | |||
|
216 | subclasses used, so the Png subclass should be used for 'image/png' | |||
|
217 | data. If the data is a URL, the data will first be downloaded | |||
|
218 | and then displayed. | |||
|
219 | ||||
|
220 | Parameters | |||
|
221 | ---------- | |||
|
222 | data : unicode, str or bytes | |||
|
223 | The raw data or a URL to download the data from. | |||
|
224 | """ | |||
|
225 | if data.startswith('http'): | |||
|
226 | import urllib2 | |||
|
227 | response = urllib2.urlopen(data) | |||
|
228 | self.data = response.read() | |||
|
229 | else: | |||
|
230 | self.data = data | |||
|
231 | ||||
|
232 | ||||
|
233 | class Pretty(DisplayObject): | |||
|
234 | ||||
|
235 | def _repr_pretty_(self): | |||
|
236 | return self.data | |||
|
237 | ||||
|
238 | ||||
|
239 | class Html(DisplayObject): | |||
|
240 | ||||
|
241 | def _repr_html_(self): | |||
|
242 | return self.data | |||
|
243 | ||||
|
244 | ||||
|
245 | class Latex(DisplayObject): | |||
|
246 | ||||
|
247 | def _repr_latex_(self): | |||
|
248 | return self.data | |||
|
249 | ||||
|
250 | ||||
|
251 | class Png(DisplayObject): | |||
|
252 | ||||
|
253 | def _repr_png_(self): | |||
|
254 | return self.data | |||
|
255 | ||||
|
256 | ||||
|
257 | class Svg(DisplayObject): | |||
|
258 | ||||
|
259 | def _repr_svg_(self): | |||
|
260 | return self.data | |||
|
261 | ||||
|
262 | ||||
|
263 | class Json(DisplayObject): | |||
|
264 | ||||
|
265 | def _repr_json_(self): | |||
|
266 | return self.data | |||
|
267 | ||||
|
268 | ||||
|
269 | class Javscript(DisplayObject): | |||
|
270 | ||||
|
271 | def _repr_javascript_(self): | |||
|
272 | return self.data | |||
|
273 | ||||
|
274 |
@@ -56,7 +56,7 b' class DisplayPublisher(Configurable):' | |||||
56 | Any metadata for the data. |
|
56 | Any metadata for the data. | |
57 | """ |
|
57 | """ | |
58 |
|
58 | |||
59 | if not isinstance(source, str): |
|
59 | if not isinstance(source, (str,unicode)): | |
60 | raise TypeError('source must be a str, got: %r' % source) |
|
60 | raise TypeError('source must be a str, got: %r' % source) | |
61 | if not isinstance(data, dict): |
|
61 | if not isinstance(data, dict): | |
62 | raise TypeError('data must be a dict, got: %r' % data) |
|
62 | raise TypeError('data must be a dict, got: %r' % data) | |
@@ -76,8 +76,9 b' class DisplayPublisher(Configurable):' | |||||
76 | * text/html |
|
76 | * text/html | |
77 | * text/latex |
|
77 | * text/latex | |
78 | * application/json |
|
78 | * application/json | |
|
79 | * application/javascript | |||
79 | * image/png |
|
80 | * image/png | |
80 |
* im |
|
81 | * image/svg+xml | |
81 |
|
82 | |||
82 | Parameters |
|
83 | Parameters | |
83 | ---------- |
|
84 | ---------- | |
@@ -103,7 +104,7 b' class DisplayPublisher(Configurable):' | |||||
103 | print(data['text/plain'], file=io.stdout) |
|
104 | print(data['text/plain'], file=io.stdout) | |
104 |
|
105 | |||
105 |
|
106 | |||
106 |
def publish_display_data( |
|
107 | def publish_display_data(source, data, metadata=None): | |
107 | """Publish data and metadata to all frontends. |
|
108 | """Publish data and metadata to all frontends. | |
108 |
|
109 | |||
109 | See the ``display_data`` message in the messaging documentation for |
|
110 | See the ``display_data`` message in the messaging documentation for | |
@@ -115,8 +116,9 b' def publish_display_data(self, source, data, metadata=None):' | |||||
115 | * text/html |
|
116 | * text/html | |
116 | * text/latex |
|
117 | * text/latex | |
117 | * application/json |
|
118 | * application/json | |
|
119 | * application/javascript | |||
118 | * image/png |
|
120 | * image/png | |
119 |
* im |
|
121 | * image/svg+xml | |
120 |
|
122 | |||
121 | Parameters |
|
123 | Parameters | |
122 | ---------- |
|
124 | ---------- | |
@@ -143,3 +145,132 b' def publish_display_data(self, source, data, metadata=None):' | |||||
143 | metadata |
|
145 | metadata | |
144 | ) |
|
146 | ) | |
145 |
|
147 | |||
|
148 | ||||
|
149 | def publish_pretty(data, metadata=None): | |||
|
150 | """Publish raw text data to all frontends. | |||
|
151 | ||||
|
152 | Parameters | |||
|
153 | ---------- | |||
|
154 | data : unicode | |||
|
155 | The raw text data to publish. | |||
|
156 | metadata : dict | |||
|
157 | A dictionary for metadata related to the data. This can contain | |||
|
158 | arbitrary key, value pairs that frontends can use to interpret | |||
|
159 | the data. | |||
|
160 | """ | |||
|
161 | publish_display_data( | |||
|
162 | u'IPython.core.displaypub.publish_pretty', | |||
|
163 | {'text/plain':data}, | |||
|
164 | metadata=metadata | |||
|
165 | ) | |||
|
166 | ||||
|
167 | ||||
|
168 | def publish_html(data, metadata=None): | |||
|
169 | """Publish raw html data to all frontends. | |||
|
170 | ||||
|
171 | Parameters | |||
|
172 | ---------- | |||
|
173 | data : unicode | |||
|
174 | The raw html data to publish. | |||
|
175 | metadata : dict | |||
|
176 | A dictionary for metadata related to the data. This can contain | |||
|
177 | arbitrary key, value pairs that frontends can use to interpret | |||
|
178 | the data. | |||
|
179 | """ | |||
|
180 | publish_display_data( | |||
|
181 | u'IPython.core.displaypub.publish_html', | |||
|
182 | {'text/html':data}, | |||
|
183 | metadata=metadata | |||
|
184 | ) | |||
|
185 | ||||
|
186 | ||||
|
187 | def publish_latex(data, metadata=None): | |||
|
188 | """Publish raw latex data to all frontends. | |||
|
189 | ||||
|
190 | Parameters | |||
|
191 | ---------- | |||
|
192 | data : unicode | |||
|
193 | The raw latex data to publish. | |||
|
194 | metadata : dict | |||
|
195 | A dictionary for metadata related to the data. This can contain | |||
|
196 | arbitrary key, value pairs that frontends can use to interpret | |||
|
197 | the data. | |||
|
198 | """ | |||
|
199 | publish_display_data( | |||
|
200 | u'IPython.core.displaypub.publish_latex', | |||
|
201 | {'text/latex':data}, | |||
|
202 | metadata=metadata | |||
|
203 | ) | |||
|
204 | ||||
|
205 | def publish_png(data, metadata=None): | |||
|
206 | """Publish raw binary png data to all frontends. | |||
|
207 | ||||
|
208 | Parameters | |||
|
209 | ---------- | |||
|
210 | data : str/bytes | |||
|
211 | The raw binary png data to publish. | |||
|
212 | metadata : dict | |||
|
213 | A dictionary for metadata related to the data. This can contain | |||
|
214 | arbitrary key, value pairs that frontends can use to interpret | |||
|
215 | the data. | |||
|
216 | """ | |||
|
217 | publish_display_data( | |||
|
218 | u'IPython.core.displaypub.publish_png', | |||
|
219 | {'image/png':data}, | |||
|
220 | metadata=metadata | |||
|
221 | ) | |||
|
222 | ||||
|
223 | def publish_svg(data, metadata=None): | |||
|
224 | """Publish raw svg data to all frontends. | |||
|
225 | ||||
|
226 | Parameters | |||
|
227 | ---------- | |||
|
228 | data : unicode | |||
|
229 | The raw svg data to publish. | |||
|
230 | metadata : dict | |||
|
231 | A dictionary for metadata related to the data. This can contain | |||
|
232 | arbitrary key, value pairs that frontends can use to interpret | |||
|
233 | the data. | |||
|
234 | """ | |||
|
235 | publish_display_data( | |||
|
236 | u'IPython.core.displaypub.publish_svg', | |||
|
237 | {'image/svg+xml':data}, | |||
|
238 | metadata=metadata | |||
|
239 | ) | |||
|
240 | ||||
|
241 | def publish_json(data, metadata=None): | |||
|
242 | """Publish raw json data to all frontends. | |||
|
243 | ||||
|
244 | Parameters | |||
|
245 | ---------- | |||
|
246 | data : unicode | |||
|
247 | The raw json data to publish. | |||
|
248 | metadata : dict | |||
|
249 | A dictionary for metadata related to the data. This can contain | |||
|
250 | arbitrary key, value pairs that frontends can use to interpret | |||
|
251 | the data. | |||
|
252 | """ | |||
|
253 | publish_display_data( | |||
|
254 | u'IPython.core.displaypub.publish_json', | |||
|
255 | {'application/json':data}, | |||
|
256 | metadata=metadata | |||
|
257 | ) | |||
|
258 | ||||
|
259 | def publish_javascript(data, metadata=None): | |||
|
260 | """Publish raw javascript data to all frontends. | |||
|
261 | ||||
|
262 | Parameters | |||
|
263 | ---------- | |||
|
264 | data : unicode | |||
|
265 | The raw javascript data to publish. | |||
|
266 | metadata : dict | |||
|
267 | A dictionary for metadata related to the data. This can contain | |||
|
268 | arbitrary key, value pairs that frontends can use to interpret | |||
|
269 | the data. | |||
|
270 | """ | |||
|
271 | publish_display_data( | |||
|
272 | u'IPython.core.displaypub.publish_javascript', | |||
|
273 | {'application/javascript':data}, | |||
|
274 | metadata=metadata | |||
|
275 | ) | |||
|
276 |
@@ -72,8 +72,9 b' class DisplayFormatter(Configurable):' | |||||
72 | * text/html |
|
72 | * text/html | |
73 | * text/latex |
|
73 | * text/latex | |
74 | * application/json |
|
74 | * application/json | |
|
75 | * application/javascript | |||
75 | * image/png |
|
76 | * image/png | |
76 |
* im |
|
77 | * image/svg+xml | |
77 |
|
78 | |||
78 | Parameters |
|
79 | Parameters | |
79 | ---------- |
|
80 | ---------- | |
@@ -562,8 +563,9 b' def format_display_data(obj, include=None, exclude=None):' | |||||
562 | * text/html |
|
563 | * text/html | |
563 | * text/latex |
|
564 | * text/latex | |
564 | * application/json |
|
565 | * application/json | |
|
566 | * application/javascript | |||
565 | * image/png |
|
567 | * image/png | |
566 |
* im |
|
568 | * image/svg+xml | |
567 |
|
569 | |||
568 | Parameters |
|
570 | Parameters | |
569 | ---------- |
|
571 | ---------- |
General Comments 0
You need to be logged in to leave comments.
Login now