forked from jupyter-widgets/ipywidgets
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jupyter-widgets#1676 from pganssle/img_url
Add filename and URL support for `Image`
- Loading branch information
Showing
7 changed files
with
264 additions
and
8 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
"""Test Image widget""" | ||
|
||
import io | ||
import os | ||
|
||
from ipywidgets import Image | ||
|
||
import hashlib | ||
|
||
import nose.tools as nt | ||
|
||
import pkgutil | ||
|
||
import tempfile | ||
from contextlib import contextmanager | ||
|
||
# Data | ||
@contextmanager | ||
def get_logo_png(): | ||
# Once the tests are not in the package, this context manager can be | ||
# replaced with the location of the actual file | ||
LOGO_DATA = pkgutil.get_data('ipywidgets.widgets.tests', | ||
'data/jupyter-logo-transparent.png') | ||
handle, fname = tempfile.mkstemp() | ||
os.close(handle) | ||
with open(fname, 'wb') as f: | ||
f.write(LOGO_DATA) | ||
|
||
yield fname | ||
|
||
os.remove(fname) | ||
|
||
LOGO_PNG_DIGEST = '3ff9eafd7197083153e83339a72e7a335539bae189c33554c680e4382c98af02' | ||
|
||
|
||
def test_empty_image(): | ||
# Empty images shouldn't raise any errors | ||
Image() | ||
|
||
|
||
def test_image_value(): | ||
random_bytes = b'\x0ee\xca\x80\xcd\x9ak#\x7f\x07\x03\xa7' | ||
|
||
Image(value=random_bytes) | ||
|
||
|
||
def test_image_format(): | ||
# Test that these format names don't throw an error | ||
Image(format='png') | ||
|
||
Image(format='jpeg') | ||
|
||
Image(format='url') | ||
|
||
|
||
def test_from_filename(): | ||
with get_logo_png() as LOGO_PNG: | ||
img = Image.from_file(LOGO_PNG) | ||
|
||
assert_equal_hash(img.value, LOGO_PNG_DIGEST) | ||
|
||
|
||
def test_set_from_filename(): | ||
img = Image() | ||
with get_logo_png() as LOGO_PNG: | ||
img.set_value_from_file(LOGO_PNG) | ||
|
||
assert_equal_hash(img.value, LOGO_PNG_DIGEST) | ||
|
||
|
||
def test_from_file(): | ||
with get_logo_png() as LOGO_PNG: | ||
with open(LOGO_PNG, 'rb') as f: | ||
img = Image.from_file(f) | ||
assert_equal_hash(img.value, LOGO_PNG_DIGEST) | ||
|
||
|
||
def test_set_value_from_file(): | ||
img = Image() | ||
with get_logo_png() as LOGO_PNG: | ||
with open(LOGO_PNG, 'rb') as f: | ||
img.set_value_from_file(f) | ||
assert_equal_hash(img.value, LOGO_PNG_DIGEST) | ||
|
||
|
||
def test_from_url_unicode(): | ||
img = Image.from_url(u'https://jupyter.org/assets/main-logo.svg') | ||
assert img.value == b'https://jupyter.org/assets/main-logo.svg' | ||
|
||
|
||
def test_from_url_bytes(): | ||
img = Image.from_url(b'https://jupyter.org/assets/main-logo.svg') | ||
|
||
assert img.value == b'https://jupyter.org/assets/main-logo.svg' | ||
|
||
|
||
def test_format_inference_filename(): | ||
with tempfile.NamedTemporaryFile(suffix='.svg', delete=False) as f: | ||
name = f.name | ||
f.close() # Allow tests to run on Windows | ||
img = Image.from_file(name) | ||
|
||
assert img.format == 'svg+xml' | ||
|
||
|
||
def test_format_inference_file(): | ||
with tempfile.NamedTemporaryFile(suffix='.gif', delete=False) as f: | ||
img = Image.from_file(f) | ||
|
||
assert img.format == 'gif' | ||
|
||
|
||
def test_format_inference_stream(): | ||
# There's no way to infer the format, so it should default to png | ||
fstream = io.BytesIO(b'') | ||
img = Image.from_file(fstream) | ||
|
||
assert img.format == 'png' | ||
|
||
|
||
def test_format_inference_overridable(): | ||
with tempfile.NamedTemporaryFile(suffix='.svg', delete=False) as f: | ||
name = f.name | ||
f.close() # Allow tests to run on Windows | ||
img = Image.from_file(name, format='gif') | ||
|
||
assert img.format == 'gif' | ||
|
||
|
||
# Helper functions | ||
def get_hash_hex(byte_str): | ||
m = hashlib.new('sha256') | ||
|
||
m.update(byte_str) | ||
|
||
return m.hexdigest() | ||
|
||
|
||
def assert_equal_hash(byte_str, digest, msg=None): | ||
kwargs = {} | ||
if msg is not None: | ||
kwargs['msg'] = msg | ||
|
||
nt.eq_(get_hash_hex(byte_str), digest, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters