Show More
@@ -6,6 +6,7 b'' | |||
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | from binascii import b2a_base64, hexlify |
|
9 | import html | |
|
9 | 10 | import json |
|
10 | 11 | import mimetypes |
|
11 | 12 | import os |
@@ -802,7 +803,7 b' class Image(DisplayObject):' | |||
|
802 | 803 | |
|
803 | 804 | def __init__(self, data=None, url=None, filename=None, format=None, |
|
804 | 805 | embed=None, width=None, height=None, retina=False, |
|
805 | unconfined=False, metadata=None): | |
|
806 | unconfined=False, metadata=None, alt=None): | |
|
806 | 807 | """Create a PNG/JPEG/GIF image object given raw data. |
|
807 | 808 | |
|
808 | 809 | When this object is returned by an input cell or passed to the |
@@ -847,6 +848,8 b' class Image(DisplayObject):' | |||
|
847 | 848 | Set unconfined=True to disable max-width confinement of the image. |
|
848 | 849 | metadata : dict |
|
849 | 850 | Specify extra metadata to attach to the image. |
|
851 | alt : unicode | |
|
852 | Alternative text for the image, for use by screen readers. | |
|
850 | 853 | |
|
851 | 854 | Examples |
|
852 | 855 | -------- |
@@ -924,6 +927,7 b' class Image(DisplayObject):' | |||
|
924 | 927 | self.height = height |
|
925 | 928 | self.retina = retina |
|
926 | 929 | self.unconfined = unconfined |
|
930 | self.alt = alt | |
|
927 | 931 | super(Image, self).__init__(data=data, url=url, filename=filename, |
|
928 | 932 | metadata=metadata) |
|
929 | 933 | |
@@ -933,6 +937,9 b' class Image(DisplayObject):' | |||
|
933 | 937 | if self.height is None and self.metadata.get('height', {}): |
|
934 | 938 | self.height = metadata['height'] |
|
935 | 939 | |
|
940 | if self.alt is None and self.metadata.get('alt', {}): | |
|
941 | self.alt = metadata['alt'] | |
|
942 | ||
|
936 | 943 | if retina: |
|
937 | 944 | self._retina_shape() |
|
938 | 945 | |
@@ -962,18 +969,21 b' class Image(DisplayObject):' | |||
|
962 | 969 | |
|
963 | 970 | def _repr_html_(self): |
|
964 | 971 | if not self.embed: |
|
965 | width = height = klass = '' | |
|
972 | width = height = klass = alt = '' | |
|
966 | 973 | if self.width: |
|
967 | 974 | width = ' width="%d"' % self.width |
|
968 | 975 | if self.height: |
|
969 | 976 | height = ' height="%d"' % self.height |
|
970 | 977 | if self.unconfined: |
|
971 | 978 | klass = ' class="unconfined"' |
|
972 | return u'<img src="{url}"{width}{height}{klass}/>'.format( | |
|
979 | if self.alt: | |
|
980 | alt = ' alt="%s"' % html.escape(self.alt) | |
|
981 | return u'<img src="{url}"{width}{height}{klass}{alt}/>'.format( | |
|
973 | 982 | url=self.url, |
|
974 | 983 | width=width, |
|
975 | 984 | height=height, |
|
976 | 985 | klass=klass, |
|
986 | alt=alt, | |
|
977 | 987 | ) |
|
978 | 988 | |
|
979 | 989 | def _repr_mimebundle_(self, include=None, exclude=None): |
@@ -1006,6 +1016,8 b' class Image(DisplayObject):' | |||
|
1006 | 1016 | md['height'] = self.height |
|
1007 | 1017 | if self.unconfined: |
|
1008 | 1018 | md['unconfined'] = self.unconfined |
|
1019 | if self.alt: | |
|
1020 | md['alt'] = self.alt | |
|
1009 | 1021 | if md or always_both: |
|
1010 | 1022 | return b64_data, md |
|
1011 | 1023 | else: |
@@ -457,3 +457,21 b' def test_display_handle():' | |||
|
457 | 457 | 'update': True, |
|
458 | 458 | }) |
|
459 | 459 | |
|
460 | def test_image_alt_tag(): | |
|
461 | """Simple test for display.Image(args, alt=x,)""" | |
|
462 | thisurl = 'http://example.com/image.png' | |
|
463 | img = display.Image(url=thisurl, alt='an image') | |
|
464 | nt.assert_equal(u'<img src="%s" alt="an image"/>' % (thisurl), img._repr_html_()) | |
|
465 | img = display.Image(url=thisurl, unconfined=True, alt='an image') | |
|
466 | nt.assert_equal(u'<img src="%s" class="unconfined" alt="an image"/>' % (thisurl), img._repr_html_()) | |
|
467 | img = display.Image(url=thisurl, alt='>"& <') | |
|
468 | nt.assert_equal(u'<img src="%s" alt=">"& <"/>' % (thisurl), img._repr_html_()) | |
|
469 | ||
|
470 | img = display.Image(url=thisurl, metadata={'alt':'an image'}) | |
|
471 | nt.assert_equal(img.alt, 'an image') | |
|
472 | ||
|
473 | here = os.path.dirname(__file__) | |
|
474 | img = display.Image(os.path.join(here, "2x2.png"), alt='an image') | |
|
475 | nt.assert_equal(img.alt, 'an image') | |
|
476 | _, md = img._repr_png_() | |
|
477 | nt.assert_equal(md['alt'], 'an image') |
General Comments 0
You need to be logged in to leave comments.
Login now