##// END OF EJS Templates
extract svg tag from svg files...
MinRK -
Show More
@@ -1,407 +1,434 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Top-level display functions for displaying object in different formats.
3 3
4 4 Authors:
5 5
6 6 * Brian Granger
7 7 """
8 8
9 9 #-----------------------------------------------------------------------------
10 10 # Copyright (C) 2008-2011 The IPython Development Team
11 11 #
12 12 # Distributed under the terms of the BSD License. The full license is in
13 13 # the file COPYING, distributed as part of this software.
14 14 #-----------------------------------------------------------------------------
15 15
16 16 #-----------------------------------------------------------------------------
17 17 # Imports
18 18 #-----------------------------------------------------------------------------
19 19
20 from xml.dom import minidom
21
20 22 from .displaypub import (
21 23 publish_pretty, publish_html,
22 24 publish_latex, publish_svg,
23 25 publish_png, publish_json,
24 26 publish_javascript, publish_jpeg
25 27 )
26 28
27 29 #-----------------------------------------------------------------------------
28 30 # Main functions
29 31 #-----------------------------------------------------------------------------
30 32
31 33 def display(*objs, **kwargs):
32 34 """Display a Python object in all frontends.
33 35
34 36 By default all representations will be computed and sent to the frontends.
35 37 Frontends can decide which representation is used and how.
36 38
37 39 Parameters
38 40 ----------
39 41 objs : tuple of objects
40 42 The Python objects to display.
41 43 include : list or tuple, optional
42 44 A list of format type strings (MIME types) to include in the
43 45 format data dict. If this is set *only* the format types included
44 46 in this list will be computed.
45 47 exclude : list or tuple, optional
46 48 A list of format type string (MIME types) to exclue in the format
47 49 data dict. If this is set all format types will be computed,
48 50 except for those included in this argument.
49 51 """
50 52 include = kwargs.get('include')
51 53 exclude = kwargs.get('exclude')
52 54
53 55 from IPython.core.interactiveshell import InteractiveShell
54 56 inst = InteractiveShell.instance()
55 57 format = inst.display_formatter.format
56 58 publish = inst.display_pub.publish
57 59
58 60 for obj in objs:
59 61 format_dict = format(obj, include=include, exclude=exclude)
60 62 publish('IPython.core.display.display', format_dict)
61 63
62 64
63 65 def display_pretty(*objs, **kwargs):
64 66 """Display the pretty (default) representation of an object.
65 67
66 68 Parameters
67 69 ----------
68 70 objs : tuple of objects
69 71 The Python objects to display, or if raw=True raw text data to
70 72 display.
71 73 raw : bool
72 74 Are the data objects raw data or Python objects that need to be
73 75 formatted before display? [default: False]
74 76 """
75 77 raw = kwargs.pop('raw',False)
76 78 if raw:
77 79 for obj in objs:
78 80 publish_pretty(obj)
79 81 else:
80 82 display(*objs, include=['text/plain'])
81 83
82 84
83 85 def display_html(*objs, **kwargs):
84 86 """Display the HTML representation of an object.
85 87
86 88 Parameters
87 89 ----------
88 90 objs : tuple of objects
89 91 The Python objects to display, or if raw=True raw HTML data to
90 92 display.
91 93 raw : bool
92 94 Are the data objects raw data or Python objects that need to be
93 95 formatted before display? [default: False]
94 96 """
95 97 raw = kwargs.pop('raw',False)
96 98 if raw:
97 99 for obj in objs:
98 100 publish_html(obj)
99 101 else:
100 102 display(*objs, include=['text/plain','text/html'])
101 103
102 104
103 105 def display_svg(*objs, **kwargs):
104 106 """Display the SVG representation of an object.
105 107
106 108 Parameters
107 109 ----------
108 110 objs : tuple of objects
109 111 The Python objects to display, or if raw=True raw svg data to
110 112 display.
111 113 raw : bool
112 114 Are the data objects raw data or Python objects that need to be
113 115 formatted before display? [default: False]
114 116 """
115 117 raw = kwargs.pop('raw',False)
116 118 if raw:
117 119 for obj in objs:
118 120 publish_svg(obj)
119 121 else:
120 122 display(*objs, include=['text/plain','image/svg+xml'])
121 123
122 124
123 125 def display_png(*objs, **kwargs):
124 126 """Display the PNG representation of an object.
125 127
126 128 Parameters
127 129 ----------
128 130 objs : tuple of objects
129 131 The Python objects to display, or if raw=True raw png data to
130 132 display.
131 133 raw : bool
132 134 Are the data objects raw data or Python objects that need to be
133 135 formatted before display? [default: False]
134 136 """
135 137 raw = kwargs.pop('raw',False)
136 138 if raw:
137 139 for obj in objs:
138 140 publish_png(obj)
139 141 else:
140 142 display(*objs, include=['text/plain','image/png'])
141 143
142 144
143 145 def display_jpeg(*objs, **kwargs):
144 146 """Display the JPEG representation of an object.
145 147
146 148 Parameters
147 149 ----------
148 150 objs : tuple of objects
149 151 The Python objects to display, or if raw=True raw JPEG data to
150 152 display.
151 153 raw : bool
152 154 Are the data objects raw data or Python objects that need to be
153 155 formatted before display? [default: False]
154 156 """
155 157 raw = kwargs.pop('raw',False)
156 158 if raw:
157 159 for obj in objs:
158 160 publish_jpeg(obj)
159 161 else:
160 162 display(*objs, include=['text/plain','image/jpeg'])
161 163
162 164
163 165 def display_latex(*objs, **kwargs):
164 166 """Display the LaTeX representation of an object.
165 167
166 168 Parameters
167 169 ----------
168 170 objs : tuple of objects
169 171 The Python objects to display, or if raw=True raw latex data to
170 172 display.
171 173 raw : bool
172 174 Are the data objects raw data or Python objects that need to be
173 175 formatted before display? [default: False]
174 176 """
175 177 raw = kwargs.pop('raw',False)
176 178 if raw:
177 179 for obj in objs:
178 180 publish_latex(obj)
179 181 else:
180 182 display(*objs, include=['text/plain','text/latex'])
181 183
182 184
183 185 def display_json(*objs, **kwargs):
184 186 """Display the JSON representation of an object.
185 187
186 188 Parameters
187 189 ----------
188 190 objs : tuple of objects
189 191 The Python objects to display, or if raw=True raw json data to
190 192 display.
191 193 raw : bool
192 194 Are the data objects raw data or Python objects that need to be
193 195 formatted before display? [default: False]
194 196 """
195 197 raw = kwargs.pop('raw',False)
196 198 if raw:
197 199 for obj in objs:
198 200 publish_json(obj)
199 201 else:
200 202 display(*objs, include=['text/plain','application/json'])
201 203
202 204
203 205 def display_javascript(*objs, **kwargs):
204 206 """Display the Javascript representation of an object.
205 207
206 208 Parameters
207 209 ----------
208 210 objs : tuple of objects
209 211 The Python objects to display, or if raw=True raw javascript data to
210 212 display.
211 213 raw : bool
212 214 Are the data objects raw data or Python objects that need to be
213 215 formatted before display? [default: False]
214 216 """
215 217 raw = kwargs.pop('raw',False)
216 218 if raw:
217 219 for obj in objs:
218 220 publish_javascript(obj)
219 221 else:
220 222 display(*objs, include=['text/plain','application/javascript'])
221 223
222 224 #-----------------------------------------------------------------------------
223 225 # Smart classes
224 226 #-----------------------------------------------------------------------------
225 227
226 228
227 229 class DisplayObject(object):
228 230 """An object that wraps data to be displayed."""
229 231
230 232 _read_flags = 'r'
231 233
232 234 def __init__(self, data=None, url=None, filename=None):
233 235 """Create a display object given raw data.
234 236
235 237 When this object is returned by an expression or passed to the
236 238 display function, it will result in the data being displayed
237 239 in the frontend. The MIME type of the data should match the
238 240 subclasses used, so the Png subclass should be used for 'image/png'
239 241 data. If the data is a URL, the data will first be downloaded
240 242 and then displayed. If
241 243
242 244 Parameters
243 245 ----------
244 246 data : unicode, str or bytes
245 247 The raw data or a URL to download the data from.
246 248 url : unicode
247 249 A URL to download the data from.
248 250 filename : unicode
249 251 Path to a local file to load the data from.
250 252 """
251 253 if data is not None and data.startswith('http'):
252 254 self.url = data
253 255 self.filename = None
254 256 self.data = None
255 257 else:
256 258 self.data = data
257 259 self.url = url
258 260 self.filename = None if filename is None else unicode(filename)
259 261 self.reload()
260 262
261 263 def reload(self):
262 264 """Reload the raw data from file or URL."""
263 265 if self.filename is not None:
264 266 with open(self.filename, self._read_flags) as f:
265 267 self.data = f.read()
266 268 elif self.url is not None:
267 269 try:
268 270 import urllib2
269 271 response = urllib2.urlopen(self.url)
270 272 self.data = response.read()
271 273 # extract encoding from header, if there is one:
272 274 encoding = None
273 275 for sub in response.headers['content-type'].split(';'):
274 276 sub = sub.strip()
275 277 if sub.startswith('charset'):
276 278 encoding = sub.split('=')[-1].strip()
277 279 break
278 280 # decode data, if an encoding was specified
279 281 if encoding:
280 282 self.data = self.data.decode(encoding, 'replace')
281 283 except:
282 284 self.data = None
283 285
284 286 class Pretty(DisplayObject):
285 287
286 288 def _repr_pretty_(self):
287 289 return self.data
288 290
289 291
290 292 class HTML(DisplayObject):
291 293
292 294 def _repr_html_(self):
293 295 return self.data
294 296
295 297
296 298 class Math(DisplayObject):
297 299
298 300 def _repr_latex_(self):
299 301 return self.data
300 302
301 303
302 304 class SVG(DisplayObject):
303 305
306 # wrap data in a property, which extracts the <svg> tag, discarding
307 # document headers
308 _data = None
309
310 @property
311 def data(self):
312 return self._data
313
314 @data.setter
315 def data(self, svg):
316 if svg is None:
317 self._data = None
318 return
319 # parse into dom object
320 x = minidom.parseString(svg)
321 # get svg tag (should be 1)
322 found_svg = x.getElementsByTagName('svg')
323 if found_svg:
324 svg = found_svg[0].toxml()
325 else:
326 # fallback on the input, trust the user
327 # but this is probably an error.
328 pass
329 self._data = svg
330
304 331 def _repr_svg_(self):
305 332 return self.data
306 333
307 334
308 335 class JSON(DisplayObject):
309 336
310 337 def _repr_json_(self):
311 338 return self.data
312 339
313 340
314 341 class Javascript(DisplayObject):
315 342
316 343 def _repr_javascript_(self):
317 344 return self.data
318 345
319 346
320 347 class Image(DisplayObject):
321 348
322 349 _read_flags = 'rb'
323 350
324 351 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=False):
325 352 """Create a display an PNG/JPEG image given raw data.
326 353
327 354 When this object is returned by an expression or passed to the
328 355 display function, it will result in the image being displayed
329 356 in the frontend.
330 357
331 358 Parameters
332 359 ----------
333 360 data : unicode, str or bytes
334 361 The raw data or a URL to download the data from.
335 362 url : unicode
336 363 A URL to download the data from.
337 364 filename : unicode
338 365 Path to a local file to load the data from.
339 366 format : unicode
340 367 The format of the image data (png/jpeg/jpg). If a filename or URL is given
341 368 for format will be inferred from the filename extension.
342 369 embed : bool
343 370 Should the image data be embedded in the notebook using a data URI (True)
344 371 or be loaded using an <img> tag. Set this to True if you want the image
345 372 to be viewable later with no internet connection. If a filename is given
346 373 embed is always set to True.
347 374 """
348 375 if filename is not None:
349 376 ext = self._find_ext(filename)
350 377 elif url is not None:
351 378 ext = self._find_ext(url)
352 379 elif data.startswith('http'):
353 380 ext = self._find_ext(data)
354 381 else:
355 382 ext = None
356 383 if ext is not None:
357 384 if ext == u'jpg' or ext == u'jpeg':
358 385 format = u'jpeg'
359 386 if ext == u'png':
360 387 format = u'png'
361 388 self.format = unicode(format).lower()
362 389 self.embed = True if filename is not None else embed
363 390 super(Image, self).__init__(data=data, url=url, filename=filename)
364 391
365 392 def reload(self):
366 393 """Reload the raw data from file or URL."""
367 394 if self.embed:
368 395 super(Image,self).reload()
369 396
370 397 def _repr_html_(self):
371 398 if not self.embed:
372 399 return u'<img src="%s" />' % self.url
373 400
374 401 def _repr_png_(self):
375 402 if self.embed and self.format == u'png':
376 403 return self.data
377 404
378 405 def _repr_jpeg_(self):
379 406 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
380 407 return self.data
381 408
382 409 def _find_ext(self, s):
383 410 return unicode(s.split('.')[-1].lower())
384 411
385 412
386 413 def clear_output(stdout=True, stderr=True, other=True):
387 414 """Clear the output of the current cell receiving output.
388 415
389 416 Optionally, each of stdout/stderr or other non-stream data (e.g. anything
390 417 produced by display()) can be excluded from the clear event.
391 418
392 419 By default, everything is cleared.
393 420
394 421 Parameters
395 422 ----------
396 423 stdout : bool [default: True]
397 424 Whether to clear stdout.
398 425 stderr : bool [default: True]
399 426 Whether to clear stderr.
400 427 other : bool [default: True]
401 428 Whether to clear everything else that is not stdout/stderr
402 429 (e.g. figures,images,HTML, any result of display()).
403 430 """
404 431 from IPython.core.interactiveshell import InteractiveShell
405 432 InteractiveShell.instance().display_pub.clear_output(
406 433 stdout=stdout, stderr=stderr, other=other,
407 434 )
General Comments 0
You need to be logged in to leave comments. Login now