Permalink
Browse files

Finishing display system work.

* Added image/jpeg MIME type to notebook format, the core display
  logic and the notebook.
* Finished HTML, SVG, Image, Math, Javascript, JSON classes.
  • Loading branch information...
1 parent b84d895 commit 08ef328ca833aec8f601414c3a18ba446f4d1c01 @ellisonbg ellisonbg committed Aug 11, 2011
View
@@ -21,7 +21,7 @@
publish_pretty, publish_html,
publish_latex, publish_svg,
publish_png, publish_json,
- publish_javascript
+ publish_javascript, publish_jpeg
)
#-----------------------------------------------------------------------------
@@ -86,7 +86,7 @@ def display_html(*objs, **kwargs):
Parameters
----------
objs : tuple of objects
- The Python objects to display, or if raw=True raw html data to
+ 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
@@ -140,6 +140,26 @@ def display_png(*objs, **kwargs):
display(*objs, include=['text/plain','image/png'])
+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:
+ publish_png(obj)
+ else:
+ display(*objs, include=['text/plain','image/jpeg'])
+
+
def display_latex(*objs, **kwargs):
"""Display the LaTeX representation of an object.
@@ -207,68 +227,147 @@ def display_javascript(*objs, **kwargs):
class DisplayObject(object):
"""An object that wraps data to be displayed."""
- def __init__(self, data):
- """Create a display object given raw data of a MIME type or a URL.
+ _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.
+ 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.startswith('http'):
- import urllib2
- response = urllib2.urlopen(data)
- self.data = response.read()
+ 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):
+class HTML(DisplayObject):
def _repr_html_(self):
return self.data
-class Latex(DisplayObject):
+class Math(DisplayObject):
def _repr_latex_(self):
return self.data
-class Png(DisplayObject):
-
- def _repr_png_(self):
- return self.data
-
-
-class Svg(DisplayObject):
+class SVG(DisplayObject):
def _repr_svg_(self):
return self.data
-class Json(DisplayObject):
+class JSON(DisplayObject):
def _repr_json_(self):
return self.data
-class Javscript(DisplayObject):
+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())
View
@@ -78,6 +78,7 @@ def publish(self, source, data, metadata=None):
* application/json
* application/javascript
* image/png
+ * image/jpeg
* image/svg+xml
Parameters
@@ -118,6 +119,7 @@ def publish_display_data(source, data, metadata=None):
* application/json
* application/javascript
* image/png
+ * image/jpeg
* image/svg+xml
Parameters
@@ -166,12 +168,12 @@ def publish_pretty(data, metadata=None):
def publish_html(data, metadata=None):
- """Publish raw html data to all frontends.
+ """Publish raw HTML data to all frontends.
Parameters
----------
data : unicode
- The raw html data to publish.
+ The raw HTML data to publish.
metadata : dict
A dictionary for metadata related to the data. This can contain
arbitrary key, value pairs that frontends can use to interpret
@@ -185,12 +187,12 @@ def publish_html(data, metadata=None):
def publish_latex(data, metadata=None):
- """Publish raw latex data to all frontends.
+ """Publish raw LaTeX data to all frontends.
Parameters
----------
data : unicode
- The raw latex data to publish.
+ The raw LaTeX data to publish.
metadata : dict
A dictionary for metadata related to the data. This can contain
arbitrary key, value pairs that frontends can use to interpret
@@ -203,12 +205,12 @@ def publish_latex(data, metadata=None):
)
def publish_png(data, metadata=None):
- """Publish raw binary png data to all frontends.
+ """Publish raw binary PNG data to all frontends.
Parameters
----------
data : str/bytes
- The raw binary png data to publish.
+ The raw binary PNG data to publish.
metadata : dict
A dictionary for metadata related to the data. This can contain
arbitrary key, value pairs that frontends can use to interpret
@@ -220,13 +222,33 @@ def publish_png(data, metadata=None):
metadata=metadata
)
+
+def publish_jpeg(data, metadata=None):
+ """Publish raw binary JPEG data to all frontends.
+
+ Parameters
+ ----------
+ data : str/bytes
+ The raw binary JPEG data to publish.
+ metadata : dict
+ A dictionary for metadata related to the data. This can contain
+ arbitrary key, value pairs that frontends can use to interpret
+ the data.
+ """
+ publish_display_data(
+ u'IPython.core.displaypub.publish_jpeg',
+ {'image/jpeg':data},
+ metadata=metadata
+ )
+
+
def publish_svg(data, metadata=None):
- """Publish raw svg data to all frontends.
+ """Publish raw SVG data to all frontends.
Parameters
----------
data : unicode
- The raw svg data to publish.
+ The raw SVG data to publish.
metadata : dict
A dictionary for metadata related to the data. This can contain
arbitrary key, value pairs that frontends can use to interpret
@@ -239,12 +261,12 @@ def publish_svg(data, metadata=None):
)
def publish_json(data, metadata=None):
- """Publish raw json data to all frontends.
+ """Publish raw JSON data to all frontends.
Parameters
----------
data : unicode
- The raw json data to publish.
+ The raw JSON data to publish.
metadata : dict
A dictionary for metadata related to the data. This can contain
arbitrary key, value pairs that frontends can use to interpret
@@ -257,12 +279,12 @@ def publish_json(data, metadata=None):
)
def publish_javascript(data, metadata=None):
- """Publish raw javascript data to all frontends.
+ """Publish raw Javascript data to all frontends.
Parameters
----------
data : unicode
- The raw javascript data to publish.
+ The raw Javascript data to publish.
metadata : dict
A dictionary for metadata related to the data. This can contain
arbitrary key, value pairs that frontends can use to interpret
View
@@ -51,6 +51,7 @@ def _formatters_default(self):
HTMLFormatter,
SVGFormatter,
PNGFormatter,
+ JPEGFormatter,
LatexFormatter,
JSONFormatter,
JavascriptFormatter
@@ -74,6 +75,7 @@ def format(self, obj, include=None, exclude=None):
* application/json
* application/javascript
* image/png
+ * image/jpeg
* image/svg+xml
Parameters
@@ -496,6 +498,22 @@ class PNGFormatter(BaseFormatter):
print_method = ObjectName('_repr_png_')
+class JPEGFormatter(BaseFormatter):
+ """A JPEG formatter.
+
+ To define the callables that compute the JPEG representation of your
+ objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type`
+ or :meth:`for_type_by_name` methods to register functions that handle
+ this.
+
+ The return value of this formatter should be raw JPEG data, *not*
+ base64 encoded.
+ """
+ format_type = Unicode('image/jpeg')
+
+ print_method = ObjectName('_repr_jpeg_')
+
+
class LatexFormatter(BaseFormatter):
"""A LaTeX formatter.
@@ -547,6 +565,7 @@ class JavascriptFormatter(BaseFormatter):
FormatterABC.register(HTMLFormatter)
FormatterABC.register(SVGFormatter)
FormatterABC.register(PNGFormatter)
+FormatterABC.register(JPEGFormatter)
FormatterABC.register(LatexFormatter)
FormatterABC.register(JSONFormatter)
FormatterABC.register(JavascriptFormatter)
@@ -565,6 +584,7 @@ def format_display_data(obj, include=None, exclude=None):
* application/json
* application/javascript
* image/png
+ * image/jpeg
* image/svg+xml
Parameters
@@ -596,3 +616,4 @@ def format_display_data(obj, include=None, exclude=None):
include,
exclude
)
+
Oops, something went wrong.

0 comments on commit 08ef328

Please sign in to comment.