##// END OF EJS Templates
remove publish_foo functions
remove publish_foo functions

File last commit:

r10447:853e909f
r10447:853e909f
Show More
display.py
591 lines | 18.5 KiB | text/x-python | PythonLexer
Brian Granger
Display system is fully working now....
r3278 # -*- coding: utf-8 -*-
"""Top-level display functions for displaying object in different formats.
Authors:
* Brian Granger
"""
#-----------------------------------------------------------------------------
MinRK
allow Image("filename")
r10050 # Copyright (C) 2013 The IPython Development Team
Brian Granger
Display system is fully working now....
r3278 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
MinRK
clear_output implies '\r' for terminal frontends
r6422 from __future__ import print_function
MinRK
allow Image("filename")
r10050 import os
Thomas Kluyver
Fix 'Custom Display Logic' example notebook for Python 3
r9197 from IPython.utils.py3compat import string_types
Brian Granger
Display system is fully working now....
r3278 #-----------------------------------------------------------------------------
MinRK
allow Image("filename")
r10050 # utility functions
#-----------------------------------------------------------------------------
def _safe_exists(path):
Kyle Kelley
Minor typo, casing, and plurality fixes
r10213 """Check path, but don't let exceptions raise"""
MinRK
allow Image("filename")
r10050 try:
return os.path.exists(path)
except Exception:
return False
#-----------------------------------------------------------------------------
Brian Granger
Display system is fully working now....
r3278 # Main functions
#-----------------------------------------------------------------------------
Brian Granger
Final work on display system....
r3288 def display(*objs, **kwargs):
Brian Granger
Display system is fully working now....
r3278 """Display a Python object in all frontends.
By default all representations will be computed and sent to the frontends.
Frontends can decide which representation is used and how.
Parameters
----------
Brian Granger
Final work on display system....
r3288 objs : tuple of objects
The Python objects to display.
Brian Granger
Display system is fully working now....
r3278 include : list or tuple, optional
A list of format type strings (MIME types) to include in the
format data dict. If this is set *only* the format types included
in this list will be computed.
exclude : list or tuple, optional
Kyle Kelley
Minor typo, casing, and plurality fixes
r10213 A list of format type strings (MIME types) to exclude in the format
Brian Granger
Display system is fully working now....
r3278 data dict. If this is set all format types will be computed,
except for those included in this argument.
"""
Brian Granger
Final work on display system....
r3288 include = kwargs.get('include')
exclude = kwargs.get('exclude')
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
Display system is fully working now....
r3278 from IPython.core.interactiveshell import InteractiveShell
Brian Granger
Addressing review comments....
r3286 inst = InteractiveShell.instance()
format = inst.display_formatter.format
publish = inst.display_pub.publish
Brian Granger
Display system is fully working now....
r3278
Brian Granger
Final work on display system....
r3288 for obj in objs:
MinRK
allow formatters to publish metadata...
r10443 format_dict, md_dict = format(obj, include=include, exclude=exclude)
publish('IPython.core.display.display', format_dict, md_dict)
Brian Granger
Display system is fully working now....
r3278
Brian E. Granger
Updates to the display system....
r4526 def display_pretty(*objs, **kwargs):
Brian Granger
More improvements to the display system....
r3279 """Display the pretty (default) representation of an object.
Parameters
----------
Brian Granger
Final work on display system....
r3288 objs : tuple of objects
Brian E. Granger
Updates to the display system....
r4526 The Python objects to display, or if raw=True raw text data to
display.
raw : bool
Are the data objects raw data or Python objects that need to be
formatted before display? [default: False]
Brian Granger
More improvements to the display system....
r3279 """
Brian E. Granger
Updates to the display system....
r4526 raw = kwargs.pop('raw',False)
if raw:
for obj in objs:
publish_pretty(obj)
else:
display(*objs, include=['text/plain'])
Brian Granger
More improvements to the display system....
r3279
Brian E. Granger
Updates to the display system....
r4526 def display_html(*objs, **kwargs):
Brian Granger
Display system is fully working now....
r3278 """Display the HTML representation of an object.
Parameters
----------
Brian Granger
Final work on display system....
r3288 objs : tuple of objects
Brian E. Granger
Finishing display system work....
r4528 The Python objects to display, or if raw=True raw HTML data to
Brian E. Granger
Updates to the display system....
r4526 display.
raw : bool
Are the data objects raw data or Python objects that need to be
formatted before display? [default: False]
"""
raw = kwargs.pop('raw',False)
if raw:
for obj in objs:
publish_html(obj)
else:
display(*objs, include=['text/plain','text/html'])
Brian Granger
Display system is fully working now....
r3278
Brian E. Granger
Updates to the display system....
r4526 def display_svg(*objs, **kwargs):
Brian Granger
Display system is fully working now....
r3278 """Display the SVG representation of an object.
Parameters
----------
Brian Granger
Final work on display system....
r3288 objs : tuple of objects
Brian E. Granger
Updates to the display system....
r4526 The Python objects to display, or if raw=True raw svg data to
display.
raw : bool
Are the data objects raw data or Python objects that need to be
formatted before display? [default: False]
Brian Granger
Display system is fully working now....
r3278 """
Brian E. Granger
Updates to the display system....
r4526 raw = kwargs.pop('raw',False)
if raw:
for obj in objs:
publish_svg(obj)
else:
display(*objs, include=['text/plain','image/svg+xml'])
Brian Granger
Display system is fully working now....
r3278
Brian E. Granger
Updates to the display system....
r4526 def display_png(*objs, **kwargs):
Brian Granger
Display system is fully working now....
r3278 """Display the PNG representation of an object.
Parameters
----------
Brian Granger
Final work on display system....
r3288 objs : tuple of objects
Brian E. Granger
Updates to the display system....
r4526 The Python objects to display, or if raw=True raw png data to
display.
raw : bool
Are the data objects raw data or Python objects that need to be
formatted before display? [default: False]
Brian Granger
Display system is fully working now....
r3278 """
Brian E. Granger
Updates to the display system....
r4526 raw = kwargs.pop('raw',False)
if raw:
for obj in objs:
publish_png(obj)
else:
display(*objs, include=['text/plain','image/png'])
Brian Granger
Display system is fully working now....
r3278
Brian E. Granger
Finishing display system work....
r4528 def display_jpeg(*objs, **kwargs):
"""Display the JPEG representation of an object.
Parameters
----------
objs : tuple of objects
The Python objects to display, or if raw=True raw JPEG data to
display.
raw : bool
Are the data objects raw data or Python objects that need to be
formatted before display? [default: False]
"""
raw = kwargs.pop('raw',False)
if raw:
for obj in objs:
Brian E. Granger
Fixing two minor things for review....
r4577 publish_jpeg(obj)
Brian E. Granger
Finishing display system work....
r4528 else:
display(*objs, include=['text/plain','image/jpeg'])
Brian E. Granger
Updates to the display system....
r4526 def display_latex(*objs, **kwargs):
Brian Granger
Display system is fully working now....
r3278 """Display the LaTeX representation of an object.
Parameters
----------
Brian Granger
Final work on display system....
r3288 objs : tuple of objects
Brian E. Granger
Updates to the display system....
r4526 The Python objects to display, or if raw=True raw latex data to
display.
raw : bool
Are the data objects raw data or Python objects that need to be
formatted before display? [default: False]
Brian Granger
Display system is fully working now....
r3278 """
Brian E. Granger
Updates to the display system....
r4526 raw = kwargs.pop('raw',False)
if raw:
for obj in objs:
publish_latex(obj)
else:
display(*objs, include=['text/plain','text/latex'])
Brian Granger
Display system is fully working now....
r3278
Brian E. Granger
Updates to the display system....
r4526 def display_json(*objs, **kwargs):
Brian Granger
Display system is fully working now....
r3278 """Display the JSON representation of an object.
Kyle Kelley
Fixed minor typo
r10212
Thomas Kluyver
Notebook does not support displaying JSON
r8086 Note that not many frontends support displaying JSON.
Brian Granger
Display system is fully working now....
r3278
Parameters
----------
Brian Granger
Final work on display system....
r3288 objs : tuple of objects
Brian E. Granger
Updates to the display system....
r4526 The Python objects to display, or if raw=True raw json data to
display.
raw : bool
Are the data objects raw data or Python objects that need to be
formatted before display? [default: False]
Brian Granger
Display system is fully working now....
r3278 """
Brian E. Granger
Updates to the display system....
r4526 raw = kwargs.pop('raw',False)
if raw:
for obj in objs:
publish_json(obj)
else:
display(*objs, include=['text/plain','application/json'])
Brian Granger
Display system is fully working now....
r3278
Brian E. Granger
Updates to the display system....
r4526 def display_javascript(*objs, **kwargs):
Brian Granger
Renaming the special methods of the formatters....
r3878 """Display the Javascript representation of an object.
Brian Granger
Display system is fully working now....
r3278
Brian Granger
Renaming the special methods of the formatters....
r3878 Parameters
----------
objs : tuple of objects
Brian E. Granger
Updates to the display system....
r4526 The Python objects to display, or if raw=True raw javascript data to
display.
raw : bool
Are the data objects raw data or Python objects that need to be
formatted before display? [default: False]
Brian Granger
Renaming the special methods of the formatters....
r3878 """
Brian E. Granger
Updates to the display system....
r4526 raw = kwargs.pop('raw',False)
if raw:
for obj in objs:
publish_javascript(obj)
else:
display(*objs, include=['text/plain','application/javascript'])
#-----------------------------------------------------------------------------
# Smart classes
#-----------------------------------------------------------------------------
class DisplayObject(object):
"""An object that wraps data to be displayed."""
Brian E. Granger
Finishing display system work....
r4528 _read_flags = 'r'
def __init__(self, data=None, url=None, filename=None):
"""Create a display object given raw data.
Brian E. Granger
Updates to the display system....
r4526
When this object is returned by an expression or passed to the
display function, it will result in the data being displayed
in the frontend. The MIME type of the data should match the
subclasses used, so the Png subclass should be used for 'image/png'
data. If the data is a URL, the data will first be downloaded
Bernardo B. Marques
remove all trailling spaces
r4872 and then displayed. If
Brian E. Granger
Updates to the display system....
r4526
Parameters
----------
data : unicode, str or bytes
MinRK
allow Image("filename")
r10050 The raw data or a URL or file to load the data from
Brian E. Granger
Finishing display system work....
r4528 url : unicode
A URL to download the data from.
filename : unicode
Path to a local file to load the data from.
Brian E. Granger
Updates to the display system....
r4526 """
MinRK
allow Image("filename")
r10050 if data is not None and isinstance(data, string_types):
if data.startswith('http') and url is None:
url = data
filename = None
data = None
elif _safe_exists(data) and filename is None:
url = None
filename = data
data = None
Kyle Kelley
Fixed minor typo
r10212
MinRK
allow Image("filename")
r10050 self.data = data
self.url = url
self.filename = None if filename is None else unicode(filename)
Kyle Kelley
Fixed minor typo
r10212
Brian E. Granger
Finishing display system work....
r4528 self.reload()
def reload(self):
"""Reload the raw data from file or URL."""
if self.filename is not None:
with open(self.filename, self._read_flags) as f:
self.data = f.read()
elif self.url is not None:
try:
import urllib2
response = urllib2.urlopen(self.url)
self.data = response.read()
MinRK
respect encoding of display data from urls
r4684 # extract encoding from header, if there is one:
encoding = None
for sub in response.headers['content-type'].split(';'):
sub = sub.strip()
if sub.startswith('charset'):
encoding = sub.split('=')[-1].strip()
break
# decode data, if an encoding was specified
if encoding:
self.data = self.data.decode(encoding, 'replace')
Brian E. Granger
Finishing display system work....
r4528 except:
self.data = None
Brian E. Granger
Updates to the display system....
r4526
class Pretty(DisplayObject):
def _repr_pretty_(self):
return self.data
Brian E. Granger
Finishing display system work....
r4528 class HTML(DisplayObject):
Brian E. Granger
Updates to the display system....
r4526
def _repr_html_(self):
return self.data
Brian E. Granger
Finishing display system work....
r4528 class Math(DisplayObject):
Brian E. Granger
Updates to the display system....
r4526
def _repr_latex_(self):
Brian Granger
Changes to Math class and added Latex class....
r6065 s = self.data.strip('$')
return "$$%s$$" % s
class Latex(DisplayObject):
def _repr_latex_(self):
Brian E. Granger
Updates to the display system....
r4526 return self.data
Brian E. Granger
Finishing display system work....
r4528 class SVG(DisplayObject):
Kyle Kelley
Fixed minor typo
r10212
MinRK
extract svg tag from svg files...
r5671 # wrap data in a property, which extracts the <svg> tag, discarding
# document headers
_data = None
Kyle Kelley
Fixed minor typo
r10212
MinRK
extract svg tag from svg files...
r5671 @property
def data(self):
return self._data
Kyle Kelley
Fixed minor typo
r10212
MinRK
extract svg tag from svg files...
r5671 @data.setter
def data(self, svg):
if svg is None:
self._data = None
return
# parse into dom object
Thomas Kluyver
Defer import of xml.dom.minidom
r9390 from xml.dom import minidom
MinRK
extract svg tag from svg files...
r5671 x = minidom.parseString(svg)
# get svg tag (should be 1)
found_svg = x.getElementsByTagName('svg')
if found_svg:
svg = found_svg[0].toxml()
else:
# fallback on the input, trust the user
# but this is probably an error.
pass
self._data = svg
Kyle Kelley
Fixed minor typo
r10212
Brian E. Granger
Updates to the display system....
r4526 def _repr_svg_(self):
return self.data
Brian E. Granger
Finishing display system work....
r4528 class JSON(DisplayObject):
Brian E. Granger
Updates to the display system....
r4526
def _repr_json_(self):
return self.data
Brian Granger
Add javascript library and css stylesheet loading to JS class.
r6120 css_t = """$("head").append($("<link/>").attr({
rel: "stylesheet",
type: "text/css",
href: "%s"
}));
"""
lib_t1 = """$.getScript("%s", function () {
"""
lib_t2 = """});
"""
Brian E. Granger
Updates to the display system....
r4526
Brian E. Granger
Finishing display system work....
r4528 class Javascript(DisplayObject):
Brian E. Granger
Updates to the display system....
r4526
Brian Granger
Add javascript library and css stylesheet loading to JS class.
r6120 def __init__(self, data=None, url=None, filename=None, lib=None, css=None):
"""Create a Javascript display object given raw data.
When this object is returned by an expression or passed to the
display function, it will result in the data being displayed
in the frontend. If the data is a URL, the data will first be
Kyle Kelley
Fixed minor typo
r10212 downloaded and then displayed.
MinRK
document initially hidden javascript container
r6425 In the Notebook, the containing element will be available as `element`,
and jQuery will be available. The output area starts hidden, so if
the js appends content to `element` that should be visible, then
it must call `container.show()` to unhide the area.
Brian Granger
Add javascript library and css stylesheet loading to JS class.
r6120
Parameters
----------
data : unicode, str or bytes
The Javascript source code or a URL to download it from.
url : unicode
A URL to download the data from.
filename : unicode
Path to a local file to load the data from.
lib : list or str
A sequence of Javascript library URLs to load asynchronously before
running the source code. The full URLs of the libraries should
be given. A single Javascript library URL can also be given as a
string.
css: : list or str
A sequence of css files to load before running the source code.
Kyle Kelley
Fixed minor typo
r10212 The full URLs of the css files should be given. A single css URL
Brian Granger
Add javascript library and css stylesheet loading to JS class.
r6120 can also be given as a string.
"""
if isinstance(lib, basestring):
lib = [lib]
elif lib is None:
lib = []
if isinstance(css, basestring):
css = [css]
elif css is None:
css = []
if not isinstance(lib, (list,tuple)):
raise TypeError('expected sequence, got: %r' % lib)
if not isinstance(css, (list,tuple)):
raise TypeError('expected sequence, got: %r' % css)
self.lib = lib
self.css = css
super(Javascript, self).__init__(data=data, url=url, filename=filename)
Brian E. Granger
Updates to the display system....
r4526 def _repr_javascript_(self):
Brian Granger
Add javascript library and css stylesheet loading to JS class.
r6120 r = ''
for c in self.css:
r += css_t % c
for l in self.lib:
r += lib_t1 % l
r += self.data
Brian Granger
Minor cleanup of Javascript display code.y
r6121 r += lib_t2*len(self.lib)
Brian Granger
Add javascript library and css stylesheet loading to JS class.
r6120 return r
Brian E. Granger
Updates to the display system....
r4526
MinRK
check image header to identify image type,...
r10049 # constants for identifying png/jpeg data
_PNG = b'\x89PNG\r\n\x1a\n'
_JPEG = b'\xff\xd8'
Brian E. Granger
Updates to the display system....
r4526
Brian E. Granger
Finishing display system work....
r4528 class Image(DisplayObject):
_read_flags = 'rb'
Jerry Fowler
Issue 2053:...
r8077 _FMT_JPEG = u'jpeg'
_FMT_PNG = u'png'
_ACCEPTABLE_EMBEDDINGS = [_FMT_JPEG, _FMT_PNG]
Brian E. Granger
Finishing display system work....
r4528
Jerry Fowler
Issue 2053:...
r8077 def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None, width=None, height=None):
Brian E. Granger
Finishing display system work....
r4528 """Create a display an PNG/JPEG image given raw data.
When this object is returned by an expression or passed to the
display function, it will result in the image being displayed
in the frontend.
Parameters
----------
data : unicode, str or bytes
The raw data or a URL to download the data from.
url : unicode
A URL to download the data from.
filename : unicode
Path to a local file to load the data from.
format : unicode
The format of the image data (png/jpeg/jpg). If a filename or URL is given
for format will be inferred from the filename extension.
embed : bool
Matthias BUSSONNIER
Embed image as default...
r6529 Should the image data be embedded using a data URI (True) or be
loaded using an <img> tag. Set this to True if you want the image
to be viewable later with no internet connection in the notebook.
Default is `True`, unless the keyword argument `url` is set, then
default value is `False`.
Note that QtConsole is not able to display images if `embed` is set to `False`
Jerry Fowler
Issue 2053:...
r8077 width : int
Width to which to constrain the image in html
height : int
Height to which to constrain the image in html
Matthias BUSSONNIER
Embed image as default...
r6529
Examples
--------
Matthias BUSSONNIER
fix english
r6580 # embed implicitly True, works in qtconsole and notebook
Matthias BUSSONNIER
Embed image as default...
r6529 Image('http://www.google.fr/images/srpr/logo3w.png')
Matthias BUSSONNIER
fix english
r6580 # embed implicitly False, does not works in qtconsole but works in notebook if
# internet connection available
Matthias BUSSONNIER
Embed image as default...
r6529 Image(url='http://www.google.fr/images/srpr/logo3w.png')
Brian E. Granger
Finishing display system work....
r4528 """
if filename is not None:
ext = self._find_ext(filename)
elif url is not None:
ext = self._find_ext(url)
Jerry Fowler
Issue 2053:...
r8077 elif data is None:
raise ValueError("No image data found. Expecting filename, url, or data.")
MinRK
allow Image("filename")
r10050 elif isinstance(data, string_types) and (
data.startswith('http') or _safe_exists(data)
):
Brian E. Granger
Finishing display system work....
r4528 ext = self._find_ext(data)
else:
ext = None
Jerry Fowler
Issue 2053:...
r8077
Brian E. Granger
Finishing display system work....
r4528 if ext is not None:
Jerry Fowler
Issue 2053:...
r8077 format = ext.lower()
Brian E. Granger
Finishing display system work....
r4528 if ext == u'jpg' or ext == u'jpeg':
Jerry Fowler
Issue 2053:...
r8077 format = self._FMT_JPEG
Brian E. Granger
Finishing display system work....
r4528 if ext == u'png':
Jerry Fowler
Issue 2053:...
r8077 format = self._FMT_PNG
MinRK
check image header to identify image type,...
r10049 elif isinstance(data, bytes) and format == 'png':
# infer image type from image data header,
# only if format might not have been specified.
if data[:2] == _JPEG:
format = 'jpeg'
Jerry Fowler
Issue 2053:...
r8077
Brian E. Granger
Finishing display system work....
r4528 self.format = unicode(format).lower()
Matthias BUSSONNIER
Embed image as default...
r6529 self.embed = embed if embed is not None else (url is None)
Jerry Fowler
Issue 2053:...
r8077
if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS:
raise ValueError("Cannot embed the '%s' image format" % (self.format))
self.width = width
self.height = height
Brian E. Granger
Finishing display system work....
r4528 super(Image, self).__init__(data=data, url=url, filename=filename)
def reload(self):
"""Reload the raw data from file or URL."""
if self.embed:
super(Image,self).reload()
def _repr_html_(self):
if not self.embed:
Jerry Fowler
Issue 2053:...
r8077 width = height = ''
if self.width:
width = ' width="%d"' % self.width
if self.height:
height = ' height="%d"' % self.height
return u'<img src="%s"%s%s/>' % (self.url, width, height)
MinRK
publish image shape as metadata
r10444
def _data_and_metadata(self):
"""shortcut for returning metadata with shape information, if defined"""
md = {}
if self.width:
md['width'] = self.width
if self.height:
md['height'] = self.height
if md:
return self.data, md
else:
return self.data
Brian E. Granger
Finishing display system work....
r4528
def _repr_png_(self):
if self.embed and self.format == u'png':
MinRK
publish image shape as metadata
r10444 return self._data_and_metadata()
Brian E. Granger
Finishing display system work....
r4528
def _repr_jpeg_(self):
if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
MinRK
publish image shape as metadata
r10444 return self._data_and_metadata()
Brian E. Granger
Finishing display system work....
r4528
def _find_ext(self, s):
return unicode(s.split('.')[-1].lower())
Brian Granger
Adding clear_output to kernel and HTML notebook.
r5080
MinRK
add channel-selection to clear_output...
r5085 def clear_output(stdout=True, stderr=True, other=True):
"""Clear the output of the current cell receiving output.
Kyle Kelley
Fixed minor typo
r10212
MinRK
add channel-selection to clear_output...
r5085 Optionally, each of stdout/stderr or other non-stream data (e.g. anything
produced by display()) can be excluded from the clear event.
Kyle Kelley
Fixed minor typo
r10212
MinRK
add channel-selection to clear_output...
r5085 By default, everything is cleared.
Kyle Kelley
Fixed minor typo
r10212
MinRK
add channel-selection to clear_output...
r5085 Parameters
----------
stdout : bool [default: True]
Whether to clear stdout.
stderr : bool [default: True]
Whether to clear stderr.
other : bool [default: True]
Whether to clear everything else that is not stdout/stderr
(e.g. figures,images,HTML, any result of display()).
"""
Brian Granger
Adding clear_output to kernel and HTML notebook.
r5080 from IPython.core.interactiveshell import InteractiveShell
MinRK
clear_output implies '\r' for terminal frontends
r6422 if InteractiveShell.initialized():
InteractiveShell.instance().display_pub.clear_output(
stdout=stdout, stderr=stderr, other=other,
)
else:
from IPython.utils import io
if stdout:
print('\033[2K\r', file=io.stdout, end='')
io.stdout.flush()
if stderr:
print('\033[2K\r', file=io.stderr, end='')
io.stderr.flush()
Kyle Kelley
Fixed minor typo
r10212