Show More
@@ -0,0 +1,38 b'' | |||
|
1 | #----------------------------------------------------------------------------- | |
|
2 | # Copyright (C) 2010-2011 The IPython Development Team. | |
|
3 | # | |
|
4 | # Distributed under the terms of the BSD License. | |
|
5 | # | |
|
6 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
7 | #----------------------------------------------------------------------------- | |
|
8 | import os | |
|
9 | ||
|
10 | import nose.tools as nt | |
|
11 | ||
|
12 | from IPython.core import display | |
|
13 | from IPython.utils import path as ipath | |
|
14 | ||
|
15 | def test_image_size(): | |
|
16 | """Simple test for display.Image(args, width=x,height=y)""" | |
|
17 | thisurl = 'http://www.google.fr/images/srpr/logo3w.png' | |
|
18 | img = display.Image(url=thisurl, width=200, height=200) | |
|
19 | nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_()) | |
|
20 | img = display.Image(url=thisurl, width=200) | |
|
21 | nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_()) | |
|
22 | img = display.Image(url=thisurl) | |
|
23 | nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_()) | |
|
24 | ||
|
25 | def test_image_filename_defaults(): | |
|
26 | '''test format constraint, and validity of jpeg and png''' | |
|
27 | tpath = ipath.get_ipython_package_dir() | |
|
28 | nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'), | |
|
29 | embed=True) | |
|
30 | nt.assert_raises(ValueError, display.Image) | |
|
31 | nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True) | |
|
32 | imgfile = os.path.join(tpath, 'frontend/html/notebook/static/ipynblogo.png') | |
|
33 | img = display.Image(filename=imgfile) | |
|
34 | nt.assert_equal('png', img.format) | |
|
35 | nt.assert_is_not_none(img._repr_png_()) | |
|
36 | img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False) | |
|
37 | nt.assert_equal('jpeg', img.format) | |
|
38 | nt.assert_is_none(img._repr_jpeg_()) |
@@ -423,8 +423,11 b' class Javascript(DisplayObject):' | |||
|
423 | 423 | class Image(DisplayObject): |
|
424 | 424 | |
|
425 | 425 | _read_flags = 'rb' |
|
426 | _FMT_JPEG = u'jpeg' | |
|
427 | _FMT_PNG = u'png' | |
|
428 | _ACCEPTABLE_EMBEDDINGS = [_FMT_JPEG, _FMT_PNG] | |
|
426 | 429 | |
|
427 | def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None): | |
|
430 | def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None, width=None, height=None): | |
|
428 | 431 | """Create a display an PNG/JPEG image given raw data. |
|
429 | 432 | |
|
430 | 433 | When this object is returned by an expression or passed to the |
@@ -451,6 +454,10 b' class Image(DisplayObject):' | |||
|
451 | 454 | default value is `False`. |
|
452 | 455 | |
|
453 | 456 | Note that QtConsole is not able to display images if `embed` is set to `False` |
|
457 | width : int | |
|
458 | Width to which to constrain the image in html | |
|
459 | height : int | |
|
460 | Height to which to constrain the image in html | |
|
454 | 461 | |
|
455 | 462 | Examples |
|
456 | 463 | -------- |
@@ -466,17 +473,27 b' class Image(DisplayObject):' | |||
|
466 | 473 | ext = self._find_ext(filename) |
|
467 | 474 | elif url is not None: |
|
468 | 475 | ext = self._find_ext(url) |
|
476 | elif data is None: | |
|
477 | raise ValueError("No image data found. Expecting filename, url, or data.") | |
|
469 | 478 | elif data.startswith('http'): |
|
470 | 479 | ext = self._find_ext(data) |
|
471 | 480 | else: |
|
472 | 481 | ext = None |
|
482 | ||
|
473 | 483 | if ext is not None: |
|
484 | format = ext.lower() | |
|
474 | 485 | if ext == u'jpg' or ext == u'jpeg': |
|
475 |
format = |
|
|
486 | format = self._FMT_JPEG | |
|
476 | 487 | if ext == u'png': |
|
477 |
format = |
|
|
488 | format = self._FMT_PNG | |
|
489 | ||
|
478 | 490 | self.format = unicode(format).lower() |
|
479 | 491 | self.embed = embed if embed is not None else (url is None) |
|
492 | ||
|
493 | if self.embed and self.format not in self._ACCEPTABLE_EMBEDDINGS: | |
|
494 | raise ValueError("Cannot embed the '%s' image format" % (self.format)) | |
|
495 | self.width = width | |
|
496 | self.height = height | |
|
480 | 497 | super(Image, self).__init__(data=data, url=url, filename=filename) |
|
481 | 498 | |
|
482 | 499 | def reload(self): |
@@ -486,7 +503,12 b' class Image(DisplayObject):' | |||
|
486 | 503 | |
|
487 | 504 | def _repr_html_(self): |
|
488 | 505 | if not self.embed: |
|
489 | return u'<img src="%s" />' % self.url | |
|
506 | width = height = '' | |
|
507 | if self.width: | |
|
508 | width = ' width="%d"' % self.width | |
|
509 | if self.height: | |
|
510 | height = ' height="%d"' % self.height | |
|
511 | return u'<img src="%s"%s%s/>' % (self.url, width, height) | |
|
490 | 512 | |
|
491 | 513 | def _repr_png_(self): |
|
492 | 514 | if self.embed and self.format == u'png': |
@@ -15,3 +15,15 b' def assert_not_in(item, collection):' | |||
|
15 | 15 | |
|
16 | 16 | if not hasattr(nt, 'assert_not_in'): |
|
17 | 17 | nt.assert_not_in = assert_not_in |
|
18 | ||
|
19 | def assert_is_none(obj): | |
|
20 | assert obj is None, '%r is not None' % obj | |
|
21 | ||
|
22 | if not hasattr(nt, 'assert_is_none'): | |
|
23 | nt.assert_is_none = assert_is_none | |
|
24 | ||
|
25 | def assert_is_not_none(obj): | |
|
26 | assert obj is not None, '%r is None' % obj | |
|
27 | ||
|
28 | if not hasattr(nt, 'assert_is_not_none'): | |
|
29 | nt.assert_is_not_none = assert_is_not_none |
General Comments 0
You need to be logged in to leave comments.
Login now