Htmlnotebook #705

Merged
merged 232 commits into from Aug 23, 2011
Commits
Jump to file or symbol
Failed to load files and symbols.
+35,520 −272
Diff settings

Always

Just for now

View
@@ -4,6 +4,7 @@ _build
docs/man/*.gz
docs/source/api/generated
docs/gh-pages
+IPython/frontend/html/notebook/static/mathjax
*.py[co]
build
*.egg-info
View
@@ -17,6 +17,13 @@
# Imports
#-----------------------------------------------------------------------------
+from .displaypub import (
+ publish_pretty, publish_html,
+ publish_latex, publish_svg,
+ publish_png, publish_json,
+ publish_javascript, publish_jpeg
+)
+
#-----------------------------------------------------------------------------
# Main functions
#-----------------------------------------------------------------------------
@@ -53,78 +60,314 @@ def display(*objs, **kwargs):
publish('IPython.core.display.display', format_dict)
-def display_pretty(*objs):
+def display_pretty(*objs, **kwargs):
"""Display the pretty (default) representation of an object.
Parameters
----------
objs : tuple of objects
- The Python objects to display.
+ 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]
"""
- display(*objs, include=['text/plain'])
+ raw = kwargs.pop('raw',False)
+ if raw:
+ for obj in objs:
+ publish_pretty(obj)
+ else:
+ display(*objs, include=['text/plain'])
-def display_html(*objs):
+def display_html(*objs, **kwargs):
"""Display the HTML representation of an object.
Parameters
----------
objs : tuple of objects
- The Python objects to display.
- """
- display(*objs, include=['text/plain','text/html'])
+ The Python objects to display, or if raw=True raw HTML 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:
+ publish_html(obj)
+ else:
+ display(*objs, include=['text/plain','text/html'])
-def display_svg(*objs):
+def display_svg(*objs, **kwargs):
"""Display the SVG representation of an object.
Parameters
----------
objs : tuple of objects
- The Python objects to display.
+ 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]
"""
- display(*objs, include=['text/plain','image/svg+xml'])
+ raw = kwargs.pop('raw',False)
+ if raw:
+ for obj in objs:
+ publish_svg(obj)
+ else:
+ display(*objs, include=['text/plain','image/svg+xml'])
-def display_png(*objs):
+def display_png(*objs, **kwargs):
"""Display the PNG representation of an object.
Parameters
----------
objs : tuple of objects
- The Python objects to display.
+ 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]
"""
- display(*objs, include=['text/plain','image/png'])
+ raw = kwargs.pop('raw',False)
+ if raw:
+ for obj in objs:
+ publish_png(obj)
+ else:
+ display(*objs, include=['text/plain','image/png'])
+
+def display_jpeg(*objs, **kwargs):
+ """Display the JPEG representation of an object.
-def display_latex(*objs):
+ 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:
+ publish_jpeg(obj)
+ else:
+ display(*objs, include=['text/plain','image/jpeg'])
+
+
+def display_latex(*objs, **kwargs):
"""Display the LaTeX representation of an object.
Parameters
----------
objs : tuple of objects
- The Python objects to display.
+ 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]
"""
- display(*objs, include=['text/plain','text/latex'])
+ raw = kwargs.pop('raw',False)
+ if raw:
+ for obj in objs:
+ publish_latex(obj)
+ else:
+ display(*objs, include=['text/plain','text/latex'])
-def display_json(*objs):
+def display_json(*objs, **kwargs):
"""Display the JSON representation of an object.
Parameters
----------
objs : tuple of objects
- The Python objects to display.
+ 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]
"""
- display(*objs, include=['text/plain','application/json'])
+ raw = kwargs.pop('raw',False)
+ if raw:
+ for obj in objs:
+ publish_json(obj)
+ else:
+ display(*objs, include=['text/plain','application/json'])
-def display_javascript(*objs):
+def display_javascript(*objs, **kwargs):
"""Display the Javascript representation of an object.
Parameters
----------
objs : tuple of objects
- The Python objects to display.
+ 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]
"""
- display(*objs, include=['text/plain','application/javascript'])
+ 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."""
+
+ _read_flags = 'r'
+
+ def __init__(self, data=None, url=None, filename=None):
+ """Create a 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. 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
+ and then displayed. If
+
+ 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.
+ """
+ if data is not None and data.startswith('http'):
+ self.url = data
+ self.filename = None
+ self.data = None
+ else:
+ self.data = data
+ self.url = url
+ self.filename = None if filename is None else unicode(filename)
+ 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()
+ except:
+ self.data = None
+
+class Pretty(DisplayObject):
+
+ def _repr_pretty_(self):
+ return self.data
+
+
+class HTML(DisplayObject):
+
+ def _repr_html_(self):
+ return self.data
+
+
+class Math(DisplayObject):
+
+ def _repr_latex_(self):
+ return self.data
+
+
+class SVG(DisplayObject):
+
+ def _repr_svg_(self):
+ return self.data
+
+
+class JSON(DisplayObject):
+
+ def _repr_json_(self):
+ return self.data
+
+
+class Javascript(DisplayObject):
+
+ def _repr_javascript_(self):
+ return self.data
+
+
+class Image(DisplayObject):
+
+ _read_flags = 'rb'
+
+ def __init__(self, data=None, url=None, filename=None, format=u'png', embed=False):
+ """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
+ Should the image data be embedded in the notebook 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. If a filename is given
+ embed is always set to True.
+ """
+ if filename is not None:
+ ext = self._find_ext(filename)
+ elif url is not None:
+ ext = self._find_ext(url)
+ elif data.startswith('http'):
+ ext = self._find_ext(data)
+ else:
+ ext = None
+ if ext is not None:
+ if ext == u'jpg' or ext == u'jpeg':
+ format = u'jpeg'
+ if ext == u'png':
+ format = u'png'
+ self.format = unicode(format).lower()
+ self.embed = True if filename is not None else embed
+ 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:
+ return u'<img src="%s" />' % self.url
+
+ def _repr_png_(self):
+ if self.embed and self.format == u'png':
+ return self.data
+
+ def _repr_jpeg_(self):
+ if self.embed and (self.format == u'jpeg' or self.format == u'jpg'):
+ return self.data
+
+ def _find_ext(self, s):
+ return unicode(s.split('.')[-1].lower())
Oops, something went wrong.