##// END OF EJS Templates
Fixing two minor things for review....
Brian E. Granger -
Show More
@@ -1,373 +1,373 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Top-level display functions for displaying object in different formats.
2 """Top-level display functions for displaying object in different formats.
3
3
4 Authors:
4 Authors:
5
5
6 * Brian Granger
6 * Brian Granger
7 """
7 """
8
8
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2008-2010 The IPython Development Team
10 # Copyright (C) 2008-2010 The IPython Development Team
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Imports
17 # Imports
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 from .displaypub import (
20 from .displaypub import (
21 publish_pretty, publish_html,
21 publish_pretty, publish_html,
22 publish_latex, publish_svg,
22 publish_latex, publish_svg,
23 publish_png, publish_json,
23 publish_png, publish_json,
24 publish_javascript, publish_jpeg
24 publish_javascript, publish_jpeg
25 )
25 )
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Main functions
28 # Main functions
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 def display(*objs, **kwargs):
31 def display(*objs, **kwargs):
32 """Display a Python object in all frontends.
32 """Display a Python object in all frontends.
33
33
34 By default all representations will be computed and sent to the frontends.
34 By default all representations will be computed and sent to the frontends.
35 Frontends can decide which representation is used and how.
35 Frontends can decide which representation is used and how.
36
36
37 Parameters
37 Parameters
38 ----------
38 ----------
39 objs : tuple of objects
39 objs : tuple of objects
40 The Python objects to display.
40 The Python objects to display.
41 include : list or tuple, optional
41 include : list or tuple, optional
42 A list of format type strings (MIME types) to include in the
42 A list of format type strings (MIME types) to include in the
43 format data dict. If this is set *only* the format types included
43 format data dict. If this is set *only* the format types included
44 in this list will be computed.
44 in this list will be computed.
45 exclude : list or tuple, optional
45 exclude : list or tuple, optional
46 A list of format type string (MIME types) to exclue in the format
46 A list of format type string (MIME types) to exclue in the format
47 data dict. If this is set all format types will be computed,
47 data dict. If this is set all format types will be computed,
48 except for those included in this argument.
48 except for those included in this argument.
49 """
49 """
50 include = kwargs.get('include')
50 include = kwargs.get('include')
51 exclude = kwargs.get('exclude')
51 exclude = kwargs.get('exclude')
52
52
53 from IPython.core.interactiveshell import InteractiveShell
53 from IPython.core.interactiveshell import InteractiveShell
54 inst = InteractiveShell.instance()
54 inst = InteractiveShell.instance()
55 format = inst.display_formatter.format
55 format = inst.display_formatter.format
56 publish = inst.display_pub.publish
56 publish = inst.display_pub.publish
57
57
58 for obj in objs:
58 for obj in objs:
59 format_dict = format(obj, include=include, exclude=exclude)
59 format_dict = format(obj, include=include, exclude=exclude)
60 publish('IPython.core.display.display', format_dict)
60 publish('IPython.core.display.display', format_dict)
61
61
62
62
63 def display_pretty(*objs, **kwargs):
63 def display_pretty(*objs, **kwargs):
64 """Display the pretty (default) representation of an object.
64 """Display the pretty (default) representation of an object.
65
65
66 Parameters
66 Parameters
67 ----------
67 ----------
68 objs : tuple of objects
68 objs : tuple of objects
69 The Python objects to display, or if raw=True raw text data to
69 The Python objects to display, or if raw=True raw text data to
70 display.
70 display.
71 raw : bool
71 raw : bool
72 Are the data objects raw data or Python objects that need to be
72 Are the data objects raw data or Python objects that need to be
73 formatted before display? [default: False]
73 formatted before display? [default: False]
74 """
74 """
75 raw = kwargs.pop('raw',False)
75 raw = kwargs.pop('raw',False)
76 if raw:
76 if raw:
77 for obj in objs:
77 for obj in objs:
78 publish_pretty(obj)
78 publish_pretty(obj)
79 else:
79 else:
80 display(*objs, include=['text/plain'])
80 display(*objs, include=['text/plain'])
81
81
82
82
83 def display_html(*objs, **kwargs):
83 def display_html(*objs, **kwargs):
84 """Display the HTML representation of an object.
84 """Display the HTML representation of an object.
85
85
86 Parameters
86 Parameters
87 ----------
87 ----------
88 objs : tuple of objects
88 objs : tuple of objects
89 The Python objects to display, or if raw=True raw HTML data to
89 The Python objects to display, or if raw=True raw HTML data to
90 display.
90 display.
91 raw : bool
91 raw : bool
92 Are the data objects raw data or Python objects that need to be
92 Are the data objects raw data or Python objects that need to be
93 formatted before display? [default: False]
93 formatted before display? [default: False]
94 """
94 """
95 raw = kwargs.pop('raw',False)
95 raw = kwargs.pop('raw',False)
96 if raw:
96 if raw:
97 for obj in objs:
97 for obj in objs:
98 publish_html(obj)
98 publish_html(obj)
99 else:
99 else:
100 display(*objs, include=['text/plain','text/html'])
100 display(*objs, include=['text/plain','text/html'])
101
101
102
102
103 def display_svg(*objs, **kwargs):
103 def display_svg(*objs, **kwargs):
104 """Display the SVG representation of an object.
104 """Display the SVG representation of an object.
105
105
106 Parameters
106 Parameters
107 ----------
107 ----------
108 objs : tuple of objects
108 objs : tuple of objects
109 The Python objects to display, or if raw=True raw svg data to
109 The Python objects to display, or if raw=True raw svg data to
110 display.
110 display.
111 raw : bool
111 raw : bool
112 Are the data objects raw data or Python objects that need to be
112 Are the data objects raw data or Python objects that need to be
113 formatted before display? [default: False]
113 formatted before display? [default: False]
114 """
114 """
115 raw = kwargs.pop('raw',False)
115 raw = kwargs.pop('raw',False)
116 if raw:
116 if raw:
117 for obj in objs:
117 for obj in objs:
118 publish_svg(obj)
118 publish_svg(obj)
119 else:
119 else:
120 display(*objs, include=['text/plain','image/svg+xml'])
120 display(*objs, include=['text/plain','image/svg+xml'])
121
121
122
122
123 def display_png(*objs, **kwargs):
123 def display_png(*objs, **kwargs):
124 """Display the PNG representation of an object.
124 """Display the PNG representation of an object.
125
125
126 Parameters
126 Parameters
127 ----------
127 ----------
128 objs : tuple of objects
128 objs : tuple of objects
129 The Python objects to display, or if raw=True raw png data to
129 The Python objects to display, or if raw=True raw png data to
130 display.
130 display.
131 raw : bool
131 raw : bool
132 Are the data objects raw data or Python objects that need to be
132 Are the data objects raw data or Python objects that need to be
133 formatted before display? [default: False]
133 formatted before display? [default: False]
134 """
134 """
135 raw = kwargs.pop('raw',False)
135 raw = kwargs.pop('raw',False)
136 if raw:
136 if raw:
137 for obj in objs:
137 for obj in objs:
138 publish_png(obj)
138 publish_png(obj)
139 else:
139 else:
140 display(*objs, include=['text/plain','image/png'])
140 display(*objs, include=['text/plain','image/png'])
141
141
142
142
143 def display_jpeg(*objs, **kwargs):
143 def display_jpeg(*objs, **kwargs):
144 """Display the JPEG representation of an object.
144 """Display the JPEG representation of an object.
145
145
146 Parameters
146 Parameters
147 ----------
147 ----------
148 objs : tuple of objects
148 objs : tuple of objects
149 The Python objects to display, or if raw=True raw JPEG data to
149 The Python objects to display, or if raw=True raw JPEG data to
150 display.
150 display.
151 raw : bool
151 raw : bool
152 Are the data objects raw data or Python objects that need to be
152 Are the data objects raw data or Python objects that need to be
153 formatted before display? [default: False]
153 formatted before display? [default: False]
154 """
154 """
155 raw = kwargs.pop('raw',False)
155 raw = kwargs.pop('raw',False)
156 if raw:
156 if raw:
157 for obj in objs:
157 for obj in objs:
158 publish_png(obj)
158 publish_jpeg(obj)
159 else:
159 else:
160 display(*objs, include=['text/plain','image/jpeg'])
160 display(*objs, include=['text/plain','image/jpeg'])
161
161
162
162
163 def display_latex(*objs, **kwargs):
163 def display_latex(*objs, **kwargs):
164 """Display the LaTeX representation of an object.
164 """Display the LaTeX representation of an object.
165
165
166 Parameters
166 Parameters
167 ----------
167 ----------
168 objs : tuple of objects
168 objs : tuple of objects
169 The Python objects to display, or if raw=True raw latex data to
169 The Python objects to display, or if raw=True raw latex data to
170 display.
170 display.
171 raw : bool
171 raw : bool
172 Are the data objects raw data or Python objects that need to be
172 Are the data objects raw data or Python objects that need to be
173 formatted before display? [default: False]
173 formatted before display? [default: False]
174 """
174 """
175 raw = kwargs.pop('raw',False)
175 raw = kwargs.pop('raw',False)
176 if raw:
176 if raw:
177 for obj in objs:
177 for obj in objs:
178 publish_latex(obj)
178 publish_latex(obj)
179 else:
179 else:
180 display(*objs, include=['text/plain','text/latex'])
180 display(*objs, include=['text/plain','text/latex'])
181
181
182
182
183 def display_json(*objs, **kwargs):
183 def display_json(*objs, **kwargs):
184 """Display the JSON representation of an object.
184 """Display the JSON representation of an object.
185
185
186 Parameters
186 Parameters
187 ----------
187 ----------
188 objs : tuple of objects
188 objs : tuple of objects
189 The Python objects to display, or if raw=True raw json data to
189 The Python objects to display, or if raw=True raw json data to
190 display.
190 display.
191 raw : bool
191 raw : bool
192 Are the data objects raw data or Python objects that need to be
192 Are the data objects raw data or Python objects that need to be
193 formatted before display? [default: False]
193 formatted before display? [default: False]
194 """
194 """
195 raw = kwargs.pop('raw',False)
195 raw = kwargs.pop('raw',False)
196 if raw:
196 if raw:
197 for obj in objs:
197 for obj in objs:
198 publish_json(obj)
198 publish_json(obj)
199 else:
199 else:
200 display(*objs, include=['text/plain','application/json'])
200 display(*objs, include=['text/plain','application/json'])
201
201
202
202
203 def display_javascript(*objs, **kwargs):
203 def display_javascript(*objs, **kwargs):
204 """Display the Javascript representation of an object.
204 """Display the Javascript representation of an object.
205
205
206 Parameters
206 Parameters
207 ----------
207 ----------
208 objs : tuple of objects
208 objs : tuple of objects
209 The Python objects to display, or if raw=True raw javascript data to
209 The Python objects to display, or if raw=True raw javascript data to
210 display.
210 display.
211 raw : bool
211 raw : bool
212 Are the data objects raw data or Python objects that need to be
212 Are the data objects raw data or Python objects that need to be
213 formatted before display? [default: False]
213 formatted before display? [default: False]
214 """
214 """
215 raw = kwargs.pop('raw',False)
215 raw = kwargs.pop('raw',False)
216 if raw:
216 if raw:
217 for obj in objs:
217 for obj in objs:
218 publish_javascript(obj)
218 publish_javascript(obj)
219 else:
219 else:
220 display(*objs, include=['text/plain','application/javascript'])
220 display(*objs, include=['text/plain','application/javascript'])
221
221
222 #-----------------------------------------------------------------------------
222 #-----------------------------------------------------------------------------
223 # Smart classes
223 # Smart classes
224 #-----------------------------------------------------------------------------
224 #-----------------------------------------------------------------------------
225
225
226
226
227 class DisplayObject(object):
227 class DisplayObject(object):
228 """An object that wraps data to be displayed."""
228 """An object that wraps data to be displayed."""
229
229
230 _read_flags = 'r'
230 _read_flags = 'r'
231
231
232 def __init__(self, data=None, url=None, filename=None):
232 def __init__(self, data=None, url=None, filename=None):
233 """Create a display object given raw data.
233 """Create a display object given raw data.
234
234
235 When this object is returned by an expression or passed to the
235 When this object is returned by an expression or passed to the
236 display function, it will result in the data being displayed
236 display function, it will result in the data being displayed
237 in the frontend. The MIME type of the data should match the
237 in the frontend. The MIME type of the data should match the
238 subclasses used, so the Png subclass should be used for 'image/png'
238 subclasses used, so the Png subclass should be used for 'image/png'
239 data. If the data is a URL, the data will first be downloaded
239 data. If the data is a URL, the data will first be downloaded
240 and then displayed. If
240 and then displayed. If
241
241
242 Parameters
242 Parameters
243 ----------
243 ----------
244 data : unicode, str or bytes
244 data : unicode, str or bytes
245 The raw data or a URL to download the data from.
245 The raw data or a URL to download the data from.
246 url : unicode
246 url : unicode
247 A URL to download the data from.
247 A URL to download the data from.
248 filename : unicode
248 filename : unicode
249 Path to a local file to load the data from.
249 Path to a local file to load the data from.
250 """
250 """
251 if data is not None and data.startswith('http'):
251 if data is not None and data.startswith('http'):
252 self.url = data
252 self.url = data
253 self.filename = None
253 self.filename = None
254 self.data = None
254 self.data = None
255 else:
255 else:
256 self.data = data
256 self.data = data
257 self.url = url
257 self.url = url
258 self.filename = None if filename is None else unicode(filename)
258 self.filename = None if filename is None else unicode(filename)
259 self.reload()
259 self.reload()
260
260
261 def reload(self):
261 def reload(self):
262 """Reload the raw data from file or URL."""
262 """Reload the raw data from file or URL."""
263 if self.filename is not None:
263 if self.filename is not None:
264 with open(self.filename, self._read_flags) as f:
264 with open(self.filename, self._read_flags) as f:
265 self.data = f.read()
265 self.data = f.read()
266 elif self.url is not None:
266 elif self.url is not None:
267 try:
267 try:
268 import urllib2
268 import urllib2
269 response = urllib2.urlopen(self.url)
269 response = urllib2.urlopen(self.url)
270 self.data = response.read()
270 self.data = response.read()
271 except:
271 except:
272 self.data = None
272 self.data = None
273
273
274 class Pretty(DisplayObject):
274 class Pretty(DisplayObject):
275
275
276 def _repr_pretty_(self):
276 def _repr_pretty_(self):
277 return self.data
277 return self.data
278
278
279
279
280 class HTML(DisplayObject):
280 class HTML(DisplayObject):
281
281
282 def _repr_html_(self):
282 def _repr_html_(self):
283 return self.data
283 return self.data
284
284
285
285
286 class Math(DisplayObject):
286 class Math(DisplayObject):
287
287
288 def _repr_latex_(self):
288 def _repr_latex_(self):
289 return self.data
289 return self.data
290
290
291
291
292 class SVG(DisplayObject):
292 class SVG(DisplayObject):
293
293
294 def _repr_svg_(self):
294 def _repr_svg_(self):
295 return self.data
295 return self.data
296
296
297
297
298 class JSON(DisplayObject):
298 class JSON(DisplayObject):
299
299
300 def _repr_json_(self):
300 def _repr_json_(self):
301 return self.data
301 return self.data
302
302
303
303
304 class Javascript(DisplayObject):
304 class Javascript(DisplayObject):
305
305
306 def _repr_javascript_(self):
306 def _repr_javascript_(self):
307 return self.data
307 return self.data
308
308
309
309
310 class Image(DisplayObject):
310 class Image(DisplayObject):
311
311
312 _read_flags = 'rb'
312 _read_flags = 'rb'
313
313
314 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=False):
314 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=False):
315 """Create a display an PNG/JPEG image given raw data.
315 """Create a display an PNG/JPEG image given raw data.
316
316
317 When this object is returned by an expression or passed to the
317 When this object is returned by an expression or passed to the
318 display function, it will result in the image being displayed
318 display function, it will result in the image being displayed
319 in the frontend.
319 in the frontend.
320
320
321 Parameters
321 Parameters
322 ----------
322 ----------
323 data : unicode, str or bytes
323 data : unicode, str or bytes
324 The raw data or a URL to download the data from.
324 The raw data or a URL to download the data from.
325 url : unicode
325 url : unicode
326 A URL to download the data from.
326 A URL to download the data from.
327 filename : unicode
327 filename : unicode
328 Path to a local file to load the data from.
328 Path to a local file to load the data from.
329 format : unicode
329 format : unicode
330 The format of the image data (png/jpeg/jpg). If a filename or URL is given
330 The format of the image data (png/jpeg/jpg). If a filename or URL is given
331 for format will be inferred from the filename extension.
331 for format will be inferred from the filename extension.
332 embed : bool
332 embed : bool
333 Should the image data be embedded in the notebook using a data URI (True)
333 Should the image data be embedded in the notebook using a data URI (True)
334 or be loaded using an <img> tag. Set this to True if you want the image
334 or be loaded using an <img> tag. Set this to True if you want the image
335 to be viewable later with no internet connection. If a filename is given
335 to be viewable later with no internet connection. If a filename is given
336 embed is always set to True.
336 embed is always set to True.
337 """
337 """
338 if filename is not None:
338 if filename is not None:
339 ext = self._find_ext(filename)
339 ext = self._find_ext(filename)
340 elif url is not None:
340 elif url is not None:
341 ext = self._find_ext(url)
341 ext = self._find_ext(url)
342 elif data.startswith('http'):
342 elif data.startswith('http'):
343 ext = self._find_ext(data)
343 ext = self._find_ext(data)
344 else:
344 else:
345 ext = None
345 ext = None
346 if ext is not None:
346 if ext is not None:
347 if ext == u'jpg' or ext == u'jpeg':
347 if ext == u'jpg' or ext == u'jpeg':
348 format = u'jpeg'
348 format = u'jpeg'
349 if ext == u'png':
349 if ext == u'png':
350 format = u'png'
350 format = u'png'
351 self.format = unicode(format).lower()
351 self.format = unicode(format).lower()
352 self.embed = True if filename is not None else embed
352 self.embed = True if filename is not None else embed
353 super(Image, self).__init__(data=data, url=url, filename=filename)
353 super(Image, self).__init__(data=data, url=url, filename=filename)
354
354
355 def reload(self):
355 def reload(self):
356 """Reload the raw data from file or URL."""
356 """Reload the raw data from file or URL."""
357 if self.embed:
357 if self.embed:
358 super(Image,self).reload()
358 super(Image,self).reload()
359
359
360 def _repr_html_(self):
360 def _repr_html_(self):
361 if not self.embed:
361 if not self.embed:
362 return u'<img src="%s" />' % self.url
362 return u'<img src="%s" />' % self.url
363
363
364 def _repr_png_(self):
364 def _repr_png_(self):
365 if self.embed and self.format == u'png':
365 if self.embed and self.format == u'png':
366 return self.data
366 return self.data
367
367
368 def _repr_jpeg_(self):
368 def _repr_jpeg_(self):
369 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
369 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
370 return self.data
370 return self.data
371
371
372 def _find_ext(self, s):
372 def _find_ext(self, s):
373 return unicode(s.split('.')[-1].lower())
373 return unicode(s.split('.')[-1].lower())
@@ -1,298 +1,298 b''
1 """An interface for publishing rich data to frontends.
1 """An interface for publishing rich data to frontends.
2
2
3 There are two components of the display system:
3 There are two components of the display system:
4
4
5 * Display formatters, which take a Python object and compute the
5 * Display formatters, which take a Python object and compute the
6 representation of the object in various formats (text, HTML, SVg, etc.).
6 representation of the object in various formats (text, HTML, SVg, etc.).
7 * The display publisher that is used to send the representation data to the
7 * The display publisher that is used to send the representation data to the
8 various frontends.
8 various frontends.
9
9
10 This module defines the logic display publishing. The display publisher uses
10 This module defines the logic display publishing. The display publisher uses
11 the ``display_data`` message type that is defined in the IPython messaging
11 the ``display_data`` message type that is defined in the IPython messaging
12 spec.
12 spec.
13
13
14 Authors:
14 Authors:
15
15
16 * Brian Granger
16 * Brian Granger
17 """
17 """
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Copyright (C) 2008-2010 The IPython Development Team
20 # Copyright (C) 2008-2010 The IPython Development Team
21 #
21 #
22 # Distributed under the terms of the BSD License. The full license is in
22 # Distributed under the terms of the BSD License. The full license is in
23 # the file COPYING, distributed as part of this software.
23 # the file COPYING, distributed as part of this software.
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Imports
27 # Imports
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 from __future__ import print_function
30 from __future__ import print_function
31
31
32 from IPython.config.configurable import Configurable
32 from IPython.config.configurable import Configurable
33
33
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35 # Main payload class
35 # Main payload class
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38 class DisplayPublisher(Configurable):
38 class DisplayPublisher(Configurable):
39 """A traited class that publishes display data to frontends.
39 """A traited class that publishes display data to frontends.
40
40
41 Instances of this class are created by the main IPython object and should
41 Instances of this class are created by the main IPython object and should
42 be accessed there.
42 be accessed there.
43 """
43 """
44
44
45 def _validate_data(self, source, data, metadata=None):
45 def _validate_data(self, source, data, metadata=None):
46 """Validate the display data.
46 """Validate the display data.
47
47
48 Parameters
48 Parameters
49 ----------
49 ----------
50 source : str
50 source : str
51 The fully dotted name of the callable that created the data, like
51 The fully dotted name of the callable that created the data, like
52 :func:`foo.bar.my_formatter`.
52 :func:`foo.bar.my_formatter`.
53 data : dict
53 data : dict
54 The formata data dictionary.
54 The formata data dictionary.
55 metadata : dict
55 metadata : dict
56 Any metadata for the data.
56 Any metadata for the data.
57 """
57 """
58
58
59 if not isinstance(source, (str,unicode)):
59 if not isinstance(source, basestring):
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)
63 if metadata is not None:
63 if metadata is not None:
64 if not isinstance(metadata, dict):
64 if not isinstance(metadata, dict):
65 raise TypeError('metadata must be a dict, got: %r' % data)
65 raise TypeError('metadata must be a dict, got: %r' % data)
66
66
67 def publish(self, source, data, metadata=None):
67 def publish(self, source, data, metadata=None):
68 """Publish data and metadata to all frontends.
68 """Publish data and metadata to all frontends.
69
69
70 See the ``display_data`` message in the messaging documentation for
70 See the ``display_data`` message in the messaging documentation for
71 more details about this message type.
71 more details about this message type.
72
72
73 The following MIME types are currently implemented:
73 The following MIME types are currently implemented:
74
74
75 * text/plain
75 * text/plain
76 * text/html
76 * text/html
77 * text/latex
77 * text/latex
78 * application/json
78 * application/json
79 * application/javascript
79 * application/javascript
80 * image/png
80 * image/png
81 * image/jpeg
81 * image/jpeg
82 * image/svg+xml
82 * image/svg+xml
83
83
84 Parameters
84 Parameters
85 ----------
85 ----------
86 source : str
86 source : str
87 A string that give the function or method that created the data,
87 A string that give the function or method that created the data,
88 such as 'IPython.core.page'.
88 such as 'IPython.core.page'.
89 data : dict
89 data : dict
90 A dictionary having keys that are valid MIME types (like
90 A dictionary having keys that are valid MIME types (like
91 'text/plain' or 'image/svg+xml') and values that are the data for
91 'text/plain' or 'image/svg+xml') and values that are the data for
92 that MIME type. The data itself must be a JSON'able data
92 that MIME type. The data itself must be a JSON'able data
93 structure. Minimally all data should have the 'text/plain' data,
93 structure. Minimally all data should have the 'text/plain' data,
94 which can be displayed by all frontends. If more than the plain
94 which can be displayed by all frontends. If more than the plain
95 text is given, it is up to the frontend to decide which
95 text is given, it is up to the frontend to decide which
96 representation to use.
96 representation to use.
97 metadata : dict
97 metadata : dict
98 A dictionary for metadata related to the data. This can contain
98 A dictionary for metadata related to the data. This can contain
99 arbitrary key, value pairs that frontends can use to interpret
99 arbitrary key, value pairs that frontends can use to interpret
100 the data.
100 the data.
101 """
101 """
102 from IPython.utils import io
102 from IPython.utils import io
103 # The default is to simply write the plain text data using io.stdout.
103 # The default is to simply write the plain text data using io.stdout.
104 if data.has_key('text/plain'):
104 if data.has_key('text/plain'):
105 print(data['text/plain'], file=io.stdout)
105 print(data['text/plain'], file=io.stdout)
106
106
107
107
108 def publish_display_data(source, data, metadata=None):
108 def publish_display_data(source, data, metadata=None):
109 """Publish data and metadata to all frontends.
109 """Publish data and metadata to all frontends.
110
110
111 See the ``display_data`` message in the messaging documentation for
111 See the ``display_data`` message in the messaging documentation for
112 more details about this message type.
112 more details about this message type.
113
113
114 The following MIME types are currently implemented:
114 The following MIME types are currently implemented:
115
115
116 * text/plain
116 * text/plain
117 * text/html
117 * text/html
118 * text/latex
118 * text/latex
119 * application/json
119 * application/json
120 * application/javascript
120 * application/javascript
121 * image/png
121 * image/png
122 * image/jpeg
122 * image/jpeg
123 * image/svg+xml
123 * image/svg+xml
124
124
125 Parameters
125 Parameters
126 ----------
126 ----------
127 source : str
127 source : str
128 A string that give the function or method that created the data,
128 A string that give the function or method that created the data,
129 such as 'IPython.core.page'.
129 such as 'IPython.core.page'.
130 data : dict
130 data : dict
131 A dictionary having keys that are valid MIME types (like
131 A dictionary having keys that are valid MIME types (like
132 'text/plain' or 'image/svg+xml') and values that are the data for
132 'text/plain' or 'image/svg+xml') and values that are the data for
133 that MIME type. The data itself must be a JSON'able data
133 that MIME type. The data itself must be a JSON'able data
134 structure. Minimally all data should have the 'text/plain' data,
134 structure. Minimally all data should have the 'text/plain' data,
135 which can be displayed by all frontends. If more than the plain
135 which can be displayed by all frontends. If more than the plain
136 text is given, it is up to the frontend to decide which
136 text is given, it is up to the frontend to decide which
137 representation to use.
137 representation to use.
138 metadata : dict
138 metadata : dict
139 A dictionary for metadata related to the data. This can contain
139 A dictionary for metadata related to the data. This can contain
140 arbitrary key, value pairs that frontends can use to interpret
140 arbitrary key, value pairs that frontends can use to interpret
141 the data.
141 the data.
142 """
142 """
143 from IPython.core.interactiveshell import InteractiveShell
143 from IPython.core.interactiveshell import InteractiveShell
144 InteractiveShell.instance().display_pub.publish(
144 InteractiveShell.instance().display_pub.publish(
145 source,
145 source,
146 data,
146 data,
147 metadata
147 metadata
148 )
148 )
149
149
150
150
151 def publish_pretty(data, metadata=None):
151 def publish_pretty(data, metadata=None):
152 """Publish raw text data to all frontends.
152 """Publish raw text data to all frontends.
153
153
154 Parameters
154 Parameters
155 ----------
155 ----------
156 data : unicode
156 data : unicode
157 The raw text data to publish.
157 The raw text data to publish.
158 metadata : dict
158 metadata : dict
159 A dictionary for metadata related to the data. This can contain
159 A dictionary for metadata related to the data. This can contain
160 arbitrary key, value pairs that frontends can use to interpret
160 arbitrary key, value pairs that frontends can use to interpret
161 the data.
161 the data.
162 """
162 """
163 publish_display_data(
163 publish_display_data(
164 u'IPython.core.displaypub.publish_pretty',
164 u'IPython.core.displaypub.publish_pretty',
165 {'text/plain':data},
165 {'text/plain':data},
166 metadata=metadata
166 metadata=metadata
167 )
167 )
168
168
169
169
170 def publish_html(data, metadata=None):
170 def publish_html(data, metadata=None):
171 """Publish raw HTML data to all frontends.
171 """Publish raw HTML data to all frontends.
172
172
173 Parameters
173 Parameters
174 ----------
174 ----------
175 data : unicode
175 data : unicode
176 The raw HTML data to publish.
176 The raw HTML data to publish.
177 metadata : dict
177 metadata : dict
178 A dictionary for metadata related to the data. This can contain
178 A dictionary for metadata related to the data. This can contain
179 arbitrary key, value pairs that frontends can use to interpret
179 arbitrary key, value pairs that frontends can use to interpret
180 the data.
180 the data.
181 """
181 """
182 publish_display_data(
182 publish_display_data(
183 u'IPython.core.displaypub.publish_html',
183 u'IPython.core.displaypub.publish_html',
184 {'text/html':data},
184 {'text/html':data},
185 metadata=metadata
185 metadata=metadata
186 )
186 )
187
187
188
188
189 def publish_latex(data, metadata=None):
189 def publish_latex(data, metadata=None):
190 """Publish raw LaTeX data to all frontends.
190 """Publish raw LaTeX data to all frontends.
191
191
192 Parameters
192 Parameters
193 ----------
193 ----------
194 data : unicode
194 data : unicode
195 The raw LaTeX data to publish.
195 The raw LaTeX data to publish.
196 metadata : dict
196 metadata : dict
197 A dictionary for metadata related to the data. This can contain
197 A dictionary for metadata related to the data. This can contain
198 arbitrary key, value pairs that frontends can use to interpret
198 arbitrary key, value pairs that frontends can use to interpret
199 the data.
199 the data.
200 """
200 """
201 publish_display_data(
201 publish_display_data(
202 u'IPython.core.displaypub.publish_latex',
202 u'IPython.core.displaypub.publish_latex',
203 {'text/latex':data},
203 {'text/latex':data},
204 metadata=metadata
204 metadata=metadata
205 )
205 )
206
206
207 def publish_png(data, metadata=None):
207 def publish_png(data, metadata=None):
208 """Publish raw binary PNG data to all frontends.
208 """Publish raw binary PNG data to all frontends.
209
209
210 Parameters
210 Parameters
211 ----------
211 ----------
212 data : str/bytes
212 data : str/bytes
213 The raw binary PNG data to publish.
213 The raw binary PNG data to publish.
214 metadata : dict
214 metadata : dict
215 A dictionary for metadata related to the data. This can contain
215 A dictionary for metadata related to the data. This can contain
216 arbitrary key, value pairs that frontends can use to interpret
216 arbitrary key, value pairs that frontends can use to interpret
217 the data.
217 the data.
218 """
218 """
219 publish_display_data(
219 publish_display_data(
220 u'IPython.core.displaypub.publish_png',
220 u'IPython.core.displaypub.publish_png',
221 {'image/png':data},
221 {'image/png':data},
222 metadata=metadata
222 metadata=metadata
223 )
223 )
224
224
225
225
226 def publish_jpeg(data, metadata=None):
226 def publish_jpeg(data, metadata=None):
227 """Publish raw binary JPEG data to all frontends.
227 """Publish raw binary JPEG data to all frontends.
228
228
229 Parameters
229 Parameters
230 ----------
230 ----------
231 data : str/bytes
231 data : str/bytes
232 The raw binary JPEG data to publish.
232 The raw binary JPEG data to publish.
233 metadata : dict
233 metadata : dict
234 A dictionary for metadata related to the data. This can contain
234 A dictionary for metadata related to the data. This can contain
235 arbitrary key, value pairs that frontends can use to interpret
235 arbitrary key, value pairs that frontends can use to interpret
236 the data.
236 the data.
237 """
237 """
238 publish_display_data(
238 publish_display_data(
239 u'IPython.core.displaypub.publish_jpeg',
239 u'IPython.core.displaypub.publish_jpeg',
240 {'image/jpeg':data},
240 {'image/jpeg':data},
241 metadata=metadata
241 metadata=metadata
242 )
242 )
243
243
244
244
245 def publish_svg(data, metadata=None):
245 def publish_svg(data, metadata=None):
246 """Publish raw SVG data to all frontends.
246 """Publish raw SVG data to all frontends.
247
247
248 Parameters
248 Parameters
249 ----------
249 ----------
250 data : unicode
250 data : unicode
251 The raw SVG data to publish.
251 The raw SVG data to publish.
252 metadata : dict
252 metadata : dict
253 A dictionary for metadata related to the data. This can contain
253 A dictionary for metadata related to the data. This can contain
254 arbitrary key, value pairs that frontends can use to interpret
254 arbitrary key, value pairs that frontends can use to interpret
255 the data.
255 the data.
256 """
256 """
257 publish_display_data(
257 publish_display_data(
258 u'IPython.core.displaypub.publish_svg',
258 u'IPython.core.displaypub.publish_svg',
259 {'image/svg+xml':data},
259 {'image/svg+xml':data},
260 metadata=metadata
260 metadata=metadata
261 )
261 )
262
262
263 def publish_json(data, metadata=None):
263 def publish_json(data, metadata=None):
264 """Publish raw JSON data to all frontends.
264 """Publish raw JSON data to all frontends.
265
265
266 Parameters
266 Parameters
267 ----------
267 ----------
268 data : unicode
268 data : unicode
269 The raw JSON data to publish.
269 The raw JSON data to publish.
270 metadata : dict
270 metadata : dict
271 A dictionary for metadata related to the data. This can contain
271 A dictionary for metadata related to the data. This can contain
272 arbitrary key, value pairs that frontends can use to interpret
272 arbitrary key, value pairs that frontends can use to interpret
273 the data.
273 the data.
274 """
274 """
275 publish_display_data(
275 publish_display_data(
276 u'IPython.core.displaypub.publish_json',
276 u'IPython.core.displaypub.publish_json',
277 {'application/json':data},
277 {'application/json':data},
278 metadata=metadata
278 metadata=metadata
279 )
279 )
280
280
281 def publish_javascript(data, metadata=None):
281 def publish_javascript(data, metadata=None):
282 """Publish raw Javascript data to all frontends.
282 """Publish raw Javascript data to all frontends.
283
283
284 Parameters
284 Parameters
285 ----------
285 ----------
286 data : unicode
286 data : unicode
287 The raw Javascript data to publish.
287 The raw Javascript data to publish.
288 metadata : dict
288 metadata : dict
289 A dictionary for metadata related to the data. This can contain
289 A dictionary for metadata related to the data. This can contain
290 arbitrary key, value pairs that frontends can use to interpret
290 arbitrary key, value pairs that frontends can use to interpret
291 the data.
291 the data.
292 """
292 """
293 publish_display_data(
293 publish_display_data(
294 u'IPython.core.displaypub.publish_javascript',
294 u'IPython.core.displaypub.publish_javascript',
295 {'application/javascript':data},
295 {'application/javascript':data},
296 metadata=metadata
296 metadata=metadata
297 )
297 )
298
298
General Comments 0
You need to be logged in to leave comments. Login now