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