##// END OF EJS Templates
Add metadata to DisplayObject (#10614)...
M Pacer -
Show More
@@ -17,6 +17,7 b' import os'
17 import struct
17 import struct
18 import sys
18 import sys
19 import warnings
19 import warnings
20 from copy import deepcopy
20
21
21 from IPython.utils.py3compat import cast_unicode
22 from IPython.utils.py3compat import cast_unicode
22 from IPython.testing.skipdoctest import skip_doctest
23 from IPython.testing.skipdoctest import skip_doctest
@@ -281,6 +282,8 b' def display(*objs, include=None, exclude=None, metadata=None, transient=None, di'
281 raw = kwargs.pop('raw', False)
282 raw = kwargs.pop('raw', False)
282 if transient is None:
283 if transient is None:
283 transient = {}
284 transient = {}
285 if metadata is None:
286 metadata={}
284 if display_id:
287 if display_id:
285 if display_id is True:
288 if display_id is True:
286 display_id = _new_id()
289 display_id = _new_id()
@@ -568,8 +571,9 b' class DisplayObject(object):'
568
571
569 _read_flags = 'r'
572 _read_flags = 'r'
570 _show_mem_addr = False
573 _show_mem_addr = False
574 metadata = None
571
575
572 def __init__(self, data=None, url=None, filename=None):
576 def __init__(self, data=None, url=None, filename=None, metadata=None):
573 """Create a display object given raw data.
577 """Create a display object given raw data.
574
578
575 When this object is returned by an expression or passed to the
579 When this object is returned by an expression or passed to the
@@ -587,6 +591,8 b' class DisplayObject(object):'
587 A URL to download the data from.
591 A URL to download the data from.
588 filename : unicode
592 filename : unicode
589 Path to a local file to load the data from.
593 Path to a local file to load the data from.
594 metadata : dict
595 Dict of metadata associated to be the object when displayed
590 """
596 """
591 if data is not None and isinstance(data, str):
597 if data is not None and isinstance(data, str):
592 if data.startswith('http') and url is None:
598 if data.startswith('http') and url is None:
@@ -602,6 +608,11 b' class DisplayObject(object):'
602 self.url = url
608 self.url = url
603 self.filename = filename
609 self.filename = filename
604
610
611 if metadata is not None:
612 self.metadata = metadata
613 elif self.metadata is None:
614 self.metadata = {}
615
605 self.reload()
616 self.reload()
606 self._check_data()
617 self._check_data()
607
618
@@ -617,6 +628,13 b' class DisplayObject(object):'
617 """Override in subclasses if there's something to check."""
628 """Override in subclasses if there's something to check."""
618 pass
629 pass
619
630
631 def _data_and_metadata(self):
632 """shortcut for returning metadata with shape information, if defined"""
633 if self.metadata:
634 return self.data, deepcopy(self.metadata)
635 else:
636 return self.data
637
620 def reload(self):
638 def reload(self):
621 """Reload the raw data from file or URL."""
639 """Reload the raw data from file or URL."""
622 if self.filename is not None:
640 if self.filename is not None:
@@ -715,9 +733,9 b' class SVG(DisplayObject):'
715 pass
733 pass
716 svg = cast_unicode(svg)
734 svg = cast_unicode(svg)
717 self._data = svg
735 self._data = svg
718
736
719 def _repr_svg_(self):
737 def _repr_svg_(self):
720 return self.data
738 return self._data_and_metadata()
721
739
722
740
723 class JSON(DisplayObject):
741 class JSON(DisplayObject):
@@ -1061,8 +1079,14 b' class Image(DisplayObject):'
1061 self.height = height
1079 self.height = height
1062 self.retina = retina
1080 self.retina = retina
1063 self.unconfined = unconfined
1081 self.unconfined = unconfined
1064 self.metadata = metadata
1082 super(Image, self).__init__(data=data, url=url, filename=filename,
1065 super(Image, self).__init__(data=data, url=url, filename=filename)
1083 metadata=metadata)
1084
1085 if self.width is None and self.metadata.get('width', {}):
1086 self.width = metadata['width']
1087
1088 if self.height is None and self.metadata.get('height', {}):
1089 self.height = metadata['height']
1066
1090
1067 if retina:
1091 if retina:
1068 self._retina_shape()
1092 self._retina_shape()
@@ -1107,14 +1131,14 b' class Image(DisplayObject):'
1107 def _data_and_metadata(self):
1131 def _data_and_metadata(self):
1108 """shortcut for returning metadata with shape information, if defined"""
1132 """shortcut for returning metadata with shape information, if defined"""
1109 md = {}
1133 md = {}
1134 if self.metadata:
1135 md.update(self.metadata)
1110 if self.width:
1136 if self.width:
1111 md['width'] = self.width
1137 md['width'] = self.width
1112 if self.height:
1138 if self.height:
1113 md['height'] = self.height
1139 md['height'] = self.height
1114 if self.unconfined:
1140 if self.unconfined:
1115 md['unconfined'] = self.unconfined
1141 md['unconfined'] = self.unconfined
1116 if self.metadata:
1117 md.update(self.metadata)
1118 if md:
1142 if md:
1119 return self.data, md
1143 return self.data, md
1120 else:
1144 else:
@@ -22,6 +22,8 b' def test_image_size():'
22 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
22 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
23 img = display.Image(url=thisurl, width=200, height=200)
23 img = display.Image(url=thisurl, width=200, height=200)
24 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
24 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
25 img = display.Image(url=thisurl, metadata={'width':200, 'height':200})
26 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
25 img = display.Image(url=thisurl, width=200)
27 img = display.Image(url=thisurl, width=200)
26 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
28 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
27 img = display.Image(url=thisurl)
29 img = display.Image(url=thisurl)
General Comments 0
You need to be logged in to leave comments. Login now