##// END OF EJS Templates
Fix docs to reflect that container.show() no longer needed #5988
Andrew Payne -
Show More
@@ -1,812 +1,811 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) 2013 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 20 from __future__ import print_function
21 21
22 22 import os
23 23 import struct
24 24
25 25 from IPython.core.formatters import _safe_get_formatter_method
26 26 from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode,
27 27 unicode_type)
28 28 from IPython.testing.skipdoctest import skip_doctest
29 29 from .displaypub import publish_display_data
30 30
31 31 #-----------------------------------------------------------------------------
32 32 # utility functions
33 33 #-----------------------------------------------------------------------------
34 34
35 35 def _safe_exists(path):
36 36 """Check path, but don't let exceptions raise"""
37 37 try:
38 38 return os.path.exists(path)
39 39 except Exception:
40 40 return False
41 41
42 42 def _merge(d1, d2):
43 43 """Like update, but merges sub-dicts instead of clobbering at the top level.
44 44
45 45 Updates d1 in-place
46 46 """
47 47
48 48 if not isinstance(d2, dict) or not isinstance(d1, dict):
49 49 return d2
50 50 for key, value in d2.items():
51 51 d1[key] = _merge(d1.get(key), value)
52 52 return d1
53 53
54 54 def _display_mimetype(mimetype, objs, raw=False, metadata=None):
55 55 """internal implementation of all display_foo methods
56 56
57 57 Parameters
58 58 ----------
59 59 mimetype : str
60 60 The mimetype to be published (e.g. 'image/png')
61 61 objs : tuple of objects
62 62 The Python objects to display, or if raw=True raw text data to
63 63 display.
64 64 raw : bool
65 65 Are the data objects raw data or Python objects that need to be
66 66 formatted before display? [default: False]
67 67 metadata : dict (optional)
68 68 Metadata to be associated with the specific mimetype output.
69 69 """
70 70 if metadata:
71 71 metadata = {mimetype: metadata}
72 72 if raw:
73 73 # turn list of pngdata into list of { 'image/png': pngdata }
74 74 objs = [ {mimetype: obj} for obj in objs ]
75 75 display(*objs, raw=raw, metadata=metadata, include=[mimetype])
76 76
77 77 #-----------------------------------------------------------------------------
78 78 # Main functions
79 79 #-----------------------------------------------------------------------------
80 80
81 81 def display(*objs, **kwargs):
82 82 """Display a Python object in all frontends.
83 83
84 84 By default all representations will be computed and sent to the frontends.
85 85 Frontends can decide which representation is used and how.
86 86
87 87 Parameters
88 88 ----------
89 89 objs : tuple of objects
90 90 The Python objects to display.
91 91 raw : bool, optional
92 92 Are the objects to be displayed already mimetype-keyed dicts of raw display data,
93 93 or Python objects that need to be formatted before display? [default: False]
94 94 include : list or tuple, optional
95 95 A list of format type strings (MIME types) to include in the
96 96 format data dict. If this is set *only* the format types included
97 97 in this list will be computed.
98 98 exclude : list or tuple, optional
99 99 A list of format type strings (MIME types) to exclude in the format
100 100 data dict. If this is set all format types will be computed,
101 101 except for those included in this argument.
102 102 metadata : dict, optional
103 103 A dictionary of metadata to associate with the output.
104 104 mime-type keys in this dictionary will be associated with the individual
105 105 representation formats, if they exist.
106 106 """
107 107 raw = kwargs.get('raw', False)
108 108 include = kwargs.get('include')
109 109 exclude = kwargs.get('exclude')
110 110 metadata = kwargs.get('metadata')
111 111
112 112 from IPython.core.interactiveshell import InteractiveShell
113 113
114 114 if not raw:
115 115 format = InteractiveShell.instance().display_formatter.format
116 116
117 117 for obj in objs:
118 118
119 119 # If _ipython_display_ is defined, use that to display this object.
120 120 display_method = _safe_get_formatter_method(obj, '_ipython_display_')
121 121 if display_method is not None:
122 122 try:
123 123 display_method(**kwargs)
124 124 except NotImplementedError:
125 125 pass
126 126 else:
127 127 continue
128 128 if raw:
129 129 publish_display_data(data=obj, metadata=metadata)
130 130 else:
131 131 format_dict, md_dict = format(obj, include=include, exclude=exclude)
132 132 if metadata:
133 133 # kwarg-specified metadata gets precedence
134 134 _merge(md_dict, metadata)
135 135 publish_display_data(data=format_dict, metadata=md_dict)
136 136
137 137
138 138 def display_pretty(*objs, **kwargs):
139 139 """Display the pretty (default) representation of an object.
140 140
141 141 Parameters
142 142 ----------
143 143 objs : tuple of objects
144 144 The Python objects to display, or if raw=True raw text data to
145 145 display.
146 146 raw : bool
147 147 Are the data objects raw data or Python objects that need to be
148 148 formatted before display? [default: False]
149 149 metadata : dict (optional)
150 150 Metadata to be associated with the specific mimetype output.
151 151 """
152 152 _display_mimetype('text/plain', objs, **kwargs)
153 153
154 154
155 155 def display_html(*objs, **kwargs):
156 156 """Display the HTML representation of an object.
157 157
158 158 Parameters
159 159 ----------
160 160 objs : tuple of objects
161 161 The Python objects to display, or if raw=True raw HTML data to
162 162 display.
163 163 raw : bool
164 164 Are the data objects raw data or Python objects that need to be
165 165 formatted before display? [default: False]
166 166 metadata : dict (optional)
167 167 Metadata to be associated with the specific mimetype output.
168 168 """
169 169 _display_mimetype('text/html', objs, **kwargs)
170 170
171 171
172 172 def display_markdown(*objs, **kwargs):
173 173 """Displays the Markdown representation of an object.
174 174
175 175 Parameters
176 176 ----------
177 177 objs : tuple of objects
178 178 The Python objects to display, or if raw=True raw markdown data to
179 179 display.
180 180 raw : bool
181 181 Are the data objects raw data or Python objects that need to be
182 182 formatted before display? [default: False]
183 183 metadata : dict (optional)
184 184 Metadata to be associated with the specific mimetype output.
185 185 """
186 186
187 187 _display_mimetype('text/markdown', objs, **kwargs)
188 188
189 189
190 190 def display_svg(*objs, **kwargs):
191 191 """Display the SVG representation of an object.
192 192
193 193 Parameters
194 194 ----------
195 195 objs : tuple of objects
196 196 The Python objects to display, or if raw=True raw svg data to
197 197 display.
198 198 raw : bool
199 199 Are the data objects raw data or Python objects that need to be
200 200 formatted before display? [default: False]
201 201 metadata : dict (optional)
202 202 Metadata to be associated with the specific mimetype output.
203 203 """
204 204 _display_mimetype('image/svg+xml', objs, **kwargs)
205 205
206 206
207 207 def display_png(*objs, **kwargs):
208 208 """Display the PNG representation of an object.
209 209
210 210 Parameters
211 211 ----------
212 212 objs : tuple of objects
213 213 The Python objects to display, or if raw=True raw png data to
214 214 display.
215 215 raw : bool
216 216 Are the data objects raw data or Python objects that need to be
217 217 formatted before display? [default: False]
218 218 metadata : dict (optional)
219 219 Metadata to be associated with the specific mimetype output.
220 220 """
221 221 _display_mimetype('image/png', objs, **kwargs)
222 222
223 223
224 224 def display_jpeg(*objs, **kwargs):
225 225 """Display the JPEG representation of an object.
226 226
227 227 Parameters
228 228 ----------
229 229 objs : tuple of objects
230 230 The Python objects to display, or if raw=True raw JPEG data to
231 231 display.
232 232 raw : bool
233 233 Are the data objects raw data or Python objects that need to be
234 234 formatted before display? [default: False]
235 235 metadata : dict (optional)
236 236 Metadata to be associated with the specific mimetype output.
237 237 """
238 238 _display_mimetype('image/jpeg', objs, **kwargs)
239 239
240 240
241 241 def display_latex(*objs, **kwargs):
242 242 """Display the LaTeX representation of an object.
243 243
244 244 Parameters
245 245 ----------
246 246 objs : tuple of objects
247 247 The Python objects to display, or if raw=True raw latex data to
248 248 display.
249 249 raw : bool
250 250 Are the data objects raw data or Python objects that need to be
251 251 formatted before display? [default: False]
252 252 metadata : dict (optional)
253 253 Metadata to be associated with the specific mimetype output.
254 254 """
255 255 _display_mimetype('text/latex', objs, **kwargs)
256 256
257 257
258 258 def display_json(*objs, **kwargs):
259 259 """Display the JSON representation of an object.
260 260
261 261 Note that not many frontends support displaying JSON.
262 262
263 263 Parameters
264 264 ----------
265 265 objs : tuple of objects
266 266 The Python objects to display, or if raw=True raw json data to
267 267 display.
268 268 raw : bool
269 269 Are the data objects raw data or Python objects that need to be
270 270 formatted before display? [default: False]
271 271 metadata : dict (optional)
272 272 Metadata to be associated with the specific mimetype output.
273 273 """
274 274 _display_mimetype('application/json', objs, **kwargs)
275 275
276 276
277 277 def display_javascript(*objs, **kwargs):
278 278 """Display the Javascript representation of an object.
279 279
280 280 Parameters
281 281 ----------
282 282 objs : tuple of objects
283 283 The Python objects to display, or if raw=True raw javascript data to
284 284 display.
285 285 raw : bool
286 286 Are the data objects raw data or Python objects that need to be
287 287 formatted before display? [default: False]
288 288 metadata : dict (optional)
289 289 Metadata to be associated with the specific mimetype output.
290 290 """
291 291 _display_mimetype('application/javascript', objs, **kwargs)
292 292
293 293
294 294 def display_pdf(*objs, **kwargs):
295 295 """Display the PDF representation of an object.
296 296
297 297 Parameters
298 298 ----------
299 299 objs : tuple of objects
300 300 The Python objects to display, or if raw=True raw javascript data to
301 301 display.
302 302 raw : bool
303 303 Are the data objects raw data or Python objects that need to be
304 304 formatted before display? [default: False]
305 305 metadata : dict (optional)
306 306 Metadata to be associated with the specific mimetype output.
307 307 """
308 308 _display_mimetype('application/pdf', objs, **kwargs)
309 309
310 310
311 311 #-----------------------------------------------------------------------------
312 312 # Smart classes
313 313 #-----------------------------------------------------------------------------
314 314
315 315
316 316 class DisplayObject(object):
317 317 """An object that wraps data to be displayed."""
318 318
319 319 _read_flags = 'r'
320 320 _show_mem_addr = False
321 321
322 322 def __init__(self, data=None, url=None, filename=None):
323 323 """Create a display object given raw data.
324 324
325 325 When this object is returned by an expression or passed to the
326 326 display function, it will result in the data being displayed
327 327 in the frontend. The MIME type of the data should match the
328 328 subclasses used, so the Png subclass should be used for 'image/png'
329 329 data. If the data is a URL, the data will first be downloaded
330 330 and then displayed. If
331 331
332 332 Parameters
333 333 ----------
334 334 data : unicode, str or bytes
335 335 The raw data or a URL or file to load the data from
336 336 url : unicode
337 337 A URL to download the data from.
338 338 filename : unicode
339 339 Path to a local file to load the data from.
340 340 """
341 341 if data is not None and isinstance(data, string_types):
342 342 if data.startswith('http') and url is None:
343 343 url = data
344 344 filename = None
345 345 data = None
346 346 elif _safe_exists(data) and filename is None:
347 347 url = None
348 348 filename = data
349 349 data = None
350 350
351 351 self.data = data
352 352 self.url = url
353 353 self.filename = None if filename is None else unicode_type(filename)
354 354
355 355 self.reload()
356 356 self._check_data()
357 357
358 358 def __repr__(self):
359 359 if not self._show_mem_addr:
360 360 cls = self.__class__
361 361 r = "<%s.%s object>" % (cls.__module__, cls.__name__)
362 362 else:
363 363 r = super(DisplayObject, self).__repr__()
364 364 return r
365 365
366 366 def _check_data(self):
367 367 """Override in subclasses if there's something to check."""
368 368 pass
369 369
370 370 def reload(self):
371 371 """Reload the raw data from file or URL."""
372 372 if self.filename is not None:
373 373 with open(self.filename, self._read_flags) as f:
374 374 self.data = f.read()
375 375 elif self.url is not None:
376 376 try:
377 377 try:
378 378 from urllib.request import urlopen # Py3
379 379 except ImportError:
380 380 from urllib2 import urlopen
381 381 response = urlopen(self.url)
382 382 self.data = response.read()
383 383 # extract encoding from header, if there is one:
384 384 encoding = None
385 385 for sub in response.headers['content-type'].split(';'):
386 386 sub = sub.strip()
387 387 if sub.startswith('charset'):
388 388 encoding = sub.split('=')[-1].strip()
389 389 break
390 390 # decode data, if an encoding was specified
391 391 if encoding:
392 392 self.data = self.data.decode(encoding, 'replace')
393 393 except:
394 394 self.data = None
395 395
396 396 class TextDisplayObject(DisplayObject):
397 397 """Validate that display data is text"""
398 398 def _check_data(self):
399 399 if self.data is not None and not isinstance(self.data, string_types):
400 400 raise TypeError("%s expects text, not %r" % (self.__class__.__name__, self.data))
401 401
402 402 class Pretty(TextDisplayObject):
403 403
404 404 def _repr_pretty_(self):
405 405 return self.data
406 406
407 407
408 408 class HTML(TextDisplayObject):
409 409
410 410 def _repr_html_(self):
411 411 return self.data
412 412
413 413 def __html__(self):
414 414 """
415 415 This method exists to inform other HTML-using modules (e.g. Markupsafe,
416 416 htmltag, etc) that this object is HTML and does not need things like
417 417 special characters (<>&) escaped.
418 418 """
419 419 return self._repr_html_()
420 420
421 421
422 422 class Markdown(TextDisplayObject):
423 423
424 424 def _repr_markdown_(self):
425 425 return self.data
426 426
427 427
428 428 class Math(TextDisplayObject):
429 429
430 430 def _repr_latex_(self):
431 431 s = self.data.strip('$')
432 432 return "$$%s$$" % s
433 433
434 434
435 435 class Latex(TextDisplayObject):
436 436
437 437 def _repr_latex_(self):
438 438 return self.data
439 439
440 440
441 441 class SVG(DisplayObject):
442 442
443 443 # wrap data in a property, which extracts the <svg> tag, discarding
444 444 # document headers
445 445 _data = None
446 446
447 447 @property
448 448 def data(self):
449 449 return self._data
450 450
451 451 @data.setter
452 452 def data(self, svg):
453 453 if svg is None:
454 454 self._data = None
455 455 return
456 456 # parse into dom object
457 457 from xml.dom import minidom
458 458 svg = cast_bytes_py2(svg)
459 459 x = minidom.parseString(svg)
460 460 # get svg tag (should be 1)
461 461 found_svg = x.getElementsByTagName('svg')
462 462 if found_svg:
463 463 svg = found_svg[0].toxml()
464 464 else:
465 465 # fallback on the input, trust the user
466 466 # but this is probably an error.
467 467 pass
468 468 svg = cast_unicode(svg)
469 469 self._data = svg
470 470
471 471 def _repr_svg_(self):
472 472 return self.data
473 473
474 474
475 475 class JSON(TextDisplayObject):
476 476
477 477 def _repr_json_(self):
478 478 return self.data
479 479
480 480 css_t = """$("head").append($("<link/>").attr({
481 481 rel: "stylesheet",
482 482 type: "text/css",
483 483 href: "%s"
484 484 }));
485 485 """
486 486
487 487 lib_t1 = """$.getScript("%s", function () {
488 488 """
489 489 lib_t2 = """});
490 490 """
491 491
492 492 class Javascript(TextDisplayObject):
493 493
494 494 def __init__(self, data=None, url=None, filename=None, lib=None, css=None):
495 495 """Create a Javascript display object given raw data.
496 496
497 497 When this object is returned by an expression or passed to the
498 498 display function, it will result in the data being displayed
499 499 in the frontend. If the data is a URL, the data will first be
500 500 downloaded and then displayed.
501 501
502 502 In the Notebook, the containing element will be available as `element`,
503 and jQuery will be available. The output area starts hidden, so if
504 the js appends content to `element` that should be visible, then
505 it must call `container.show()` to unhide the area.
503 and jQuery will be available. Content appended to `element` will be
504 visible in the output area.
506 505
507 506 Parameters
508 507 ----------
509 508 data : unicode, str or bytes
510 509 The Javascript source code or a URL to download it from.
511 510 url : unicode
512 511 A URL to download the data from.
513 512 filename : unicode
514 513 Path to a local file to load the data from.
515 514 lib : list or str
516 515 A sequence of Javascript library URLs to load asynchronously before
517 516 running the source code. The full URLs of the libraries should
518 517 be given. A single Javascript library URL can also be given as a
519 518 string.
520 519 css: : list or str
521 520 A sequence of css files to load before running the source code.
522 521 The full URLs of the css files should be given. A single css URL
523 522 can also be given as a string.
524 523 """
525 524 if isinstance(lib, string_types):
526 525 lib = [lib]
527 526 elif lib is None:
528 527 lib = []
529 528 if isinstance(css, string_types):
530 529 css = [css]
531 530 elif css is None:
532 531 css = []
533 532 if not isinstance(lib, (list,tuple)):
534 533 raise TypeError('expected sequence, got: %r' % lib)
535 534 if not isinstance(css, (list,tuple)):
536 535 raise TypeError('expected sequence, got: %r' % css)
537 536 self.lib = lib
538 537 self.css = css
539 538 super(Javascript, self).__init__(data=data, url=url, filename=filename)
540 539
541 540 def _repr_javascript_(self):
542 541 r = ''
543 542 for c in self.css:
544 543 r += css_t % c
545 544 for l in self.lib:
546 545 r += lib_t1 % l
547 546 r += self.data
548 547 r += lib_t2*len(self.lib)
549 548 return r
550 549
551 550 # constants for identifying png/jpeg data
552 551 _PNG = b'\x89PNG\r\n\x1a\n'
553 552 _JPEG = b'\xff\xd8'
554 553
555 554 def _pngxy(data):
556 555 """read the (width, height) from a PNG header"""
557 556 ihdr = data.index(b'IHDR')
558 557 # next 8 bytes are width/height
559 558 w4h4 = data[ihdr+4:ihdr+12]
560 559 return struct.unpack('>ii', w4h4)
561 560
562 561 def _jpegxy(data):
563 562 """read the (width, height) from a JPEG header"""
564 563 # adapted from http://www.64lines.com/jpeg-width-height
565 564
566 565 idx = 4
567 566 while True:
568 567 block_size = struct.unpack('>H', data[idx:idx+2])[0]
569 568 idx = idx + block_size
570 569 if data[idx:idx+2] == b'\xFF\xC0':
571 570 # found Start of Frame
572 571 iSOF = idx
573 572 break
574 573 else:
575 574 # read another block
576 575 idx += 2
577 576
578 577 h, w = struct.unpack('>HH', data[iSOF+5:iSOF+9])
579 578 return w, h
580 579
581 580 class Image(DisplayObject):
582 581
583 582 _read_flags = 'rb'
584 583 _FMT_JPEG = u'jpeg'
585 584 _FMT_PNG = u'png'
586 585 _ACCEPTABLE_EMBEDDINGS = [_FMT_JPEG, _FMT_PNG]
587 586
588 587 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None, width=None, height=None, retina=False):
589 588 """Create a PNG/JPEG image object given raw data.
590 589
591 590 When this object is returned by an input cell or passed to the
592 591 display function, it will result in the image being displayed
593 592 in the frontend.
594 593
595 594 Parameters
596 595 ----------
597 596 data : unicode, str or bytes
598 597 The raw image data or a URL or filename to load the data from.
599 598 This always results in embedded image data.
600 599 url : unicode
601 600 A URL to download the data from. If you specify `url=`,
602 601 the image data will not be embedded unless you also specify `embed=True`.
603 602 filename : unicode
604 603 Path to a local file to load the data from.
605 604 Images from a file are always embedded.
606 605 format : unicode
607 606 The format of the image data (png/jpeg/jpg). If a filename or URL is given
608 607 for format will be inferred from the filename extension.
609 608 embed : bool
610 609 Should the image data be embedded using a data URI (True) or be
611 610 loaded using an <img> tag. Set this to True if you want the image
612 611 to be viewable later with no internet connection in the notebook.
613 612
614 613 Default is `True`, unless the keyword argument `url` is set, then
615 614 default value is `False`.
616 615
617 616 Note that QtConsole is not able to display images if `embed` is set to `False`
618 617 width : int
619 618 Width to which to constrain the image in html
620 619 height : int
621 620 Height to which to constrain the image in html
622 621 retina : bool
623 622 Automatically set the width and height to half of the measured
624 623 width and height.
625 624 This only works for embedded images because it reads the width/height
626 625 from image data.
627 626 For non-embedded images, you can just set the desired display width
628 627 and height directly.
629 628
630 629 Examples
631 630 --------
632 631 # embedded image data, works in qtconsole and notebook
633 632 # when passed positionally, the first arg can be any of raw image data,
634 633 # a URL, or a filename from which to load image data.
635 634 # The result is always embedding image data for inline images.
636 635 Image('http://www.google.fr/images/srpr/logo3w.png')
637 636 Image('/path/to/image.jpg')
638 637 Image(b'RAW_PNG_DATA...')
639 638
640 639 # Specifying Image(url=...) does not embed the image data,
641 640 # it only generates `<img>` tag with a link to the source.
642 641 # This will not work in the qtconsole or offline.
643 642 Image(url='http://www.google.fr/images/srpr/logo3w.png')
644 643
645 644 """
646 645 if filename is not None:
647 646 ext = self._find_ext(filename)
648 647 elif url is not None:
649 648 ext = self._find_ext(url)
650 649 elif data is None:
651 650 raise ValueError("No image data found. Expecting filename, url, or data.")
652 651 elif isinstance(data, string_types) and (
653 652 data.startswith('http') or _safe_exists(data)
654 653 ):
655 654 ext = self._find_ext(data)
656 655 else:
657 656 ext = None
658 657
659 658 if ext is not None:
660 659 format = ext.lower()
661 660 if ext == u'jpg' or ext == u'jpeg':
662 661 format = self._FMT_JPEG
663 662 if ext == u'png':
664 663 format = self._FMT_PNG
665 664 elif isinstance(data, bytes) and format == 'png':
666 665 # infer image type from image data header,
667 666 # only if format might not have been specified.
668 667 if data[:2] == _JPEG:
669 668 format = 'jpeg'
670 669
671 670 self.format = unicode_type(format).lower()
672 671 self.embed = embed if embed is not None else (url is None)
673 672
674 673 if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
675 674 raise ValueError("Cannot embed the '%s' image format" % (self.format))
676 675 self.width = width
677 676 self.height = height
678 677 self.retina = retina
679 678 super(Image, self).__init__(data=data, url=url, filename=filename)
680 679
681 680 if retina:
682 681 self._retina_shape()
683 682
684 683 def _retina_shape(self):
685 684 """load pixel-doubled width and height from image data"""
686 685 if not self.embed:
687 686 return
688 687 if self.format == 'png':
689 688 w, h = _pngxy(self.data)
690 689 elif self.format == 'jpeg':
691 690 w, h = _jpegxy(self.data)
692 691 else:
693 692 # retina only supports png
694 693 return
695 694 self.width = w // 2
696 695 self.height = h // 2
697 696
698 697 def reload(self):
699 698 """Reload the raw data from file or URL."""
700 699 if self.embed:
701 700 super(Image,self).reload()
702 701 if self.retina:
703 702 self._retina_shape()
704 703
705 704 def _repr_html_(self):
706 705 if not self.embed:
707 706 width = height = ''
708 707 if self.width:
709 708 width = ' width="%d"' % self.width
710 709 if self.height:
711 710 height = ' height="%d"' % self.height
712 711 return u'<img src="%s"%s%s/>' % (self.url, width, height)
713 712
714 713 def _data_and_metadata(self):
715 714 """shortcut for returning metadata with shape information, if defined"""
716 715 md = {}
717 716 if self.width:
718 717 md['width'] = self.width
719 718 if self.height:
720 719 md['height'] = self.height
721 720 if md:
722 721 return self.data, md
723 722 else:
724 723 return self.data
725 724
726 725 def _repr_png_(self):
727 726 if self.embed and self.format == u'png':
728 727 return self._data_and_metadata()
729 728
730 729 def _repr_jpeg_(self):
731 730 if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
732 731 return self._data_and_metadata()
733 732
734 733 def _find_ext(self, s):
735 734 return unicode_type(s.split('.')[-1].lower())
736 735
737 736
738 737 def clear_output(wait=False):
739 738 """Clear the output of the current cell receiving output.
740 739
741 740 Parameters
742 741 ----------
743 742 wait : bool [default: false]
744 743 Wait to clear the output until new output is available to replace it."""
745 744 from IPython.core.interactiveshell import InteractiveShell
746 745 if InteractiveShell.initialized():
747 746 InteractiveShell.instance().display_pub.clear_output(wait)
748 747 else:
749 748 from IPython.utils import io
750 749 print('\033[2K\r', file=io.stdout, end='')
751 750 io.stdout.flush()
752 751 print('\033[2K\r', file=io.stderr, end='')
753 752 io.stderr.flush()
754 753
755 754
756 755 @skip_doctest
757 756 def set_matplotlib_formats(*formats, **kwargs):
758 757 """Select figure formats for the inline backend. Optionally pass quality for JPEG.
759 758
760 759 For example, this enables PNG and JPEG output with a JPEG quality of 90%::
761 760
762 761 In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)
763 762
764 763 To set this in your config files use the following::
765 764
766 765 c.InlineBackend.figure_formats = {'png', 'jpeg'}
767 766 c.InlineBackend.print_figure_kwargs.update({'quality' : 90})
768 767
769 768 Parameters
770 769 ----------
771 770 *formats : strs
772 771 One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
773 772 **kwargs :
774 773 Keyword args will be relayed to ``figure.canvas.print_figure``.
775 774 """
776 775 from IPython.core.interactiveshell import InteractiveShell
777 776 from IPython.core.pylabtools import select_figure_formats
778 777 from IPython.kernel.zmq.pylab.config import InlineBackend
779 778 # build kwargs, starting with InlineBackend config
780 779 kw = {}
781 780 cfg = InlineBackend.instance()
782 781 kw.update(cfg.print_figure_kwargs)
783 782 kw.update(**kwargs)
784 783 shell = InteractiveShell.instance()
785 784 select_figure_formats(shell, formats, **kw)
786 785
787 786 @skip_doctest
788 787 def set_matplotlib_close(close=True):
789 788 """Set whether the inline backend closes all figures automatically or not.
790 789
791 790 By default, the inline backend used in the IPython Notebook will close all
792 791 matplotlib figures automatically after each cell is run. This means that
793 792 plots in different cells won't interfere. Sometimes, you may want to make
794 793 a plot in one cell and then refine it in later cells. This can be accomplished
795 794 by::
796 795
797 796 In [1]: set_matplotlib_close(False)
798 797
799 798 To set this in your config files use the following::
800 799
801 800 c.InlineBackend.close_figures = False
802 801
803 802 Parameters
804 803 ----------
805 804 close : bool
806 805 Should all matplotlib figures be automatically closed after each cell is
807 806 run?
808 807 """
809 808 from IPython.kernel.zmq.pylab.config import InlineBackend
810 809 cfg = InlineBackend.instance()
811 810 cfg.close_figures = close
812 811
General Comments 0
You need to be logged in to leave comments. Login now