Skip to content

Commit

Permalink
Support bytes on Image (#2963)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Nov 30, 2021
1 parent 1badb73 commit 1d03ef4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 8 additions & 0 deletions panel/pane/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def applies(cls, obj):
return True
elif isurl(obj, None):
return 0
elif isinstance(obj, bytes):
try:
cls._imgshape(obj)
return True
except Exception:
return False
if hasattr(obj, 'read'): # Check for file like object
return True
return False
Expand All @@ -57,6 +63,8 @@ def _data(self):
if isfile(self.object):
with open(self.object, 'rb') as f:
return f.read()
elif isinstance(self.object, bytes):
return self.object
if hasattr(self.object, 'read'):
if hasattr(self.object, 'seek'):
self.object.seek(0)
Expand Down
9 changes: 8 additions & 1 deletion panel/tests/pane/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ def test_loading_a_image_from_url():
image_data = image_pane._data()
assert b'PNG' in image_data

def test_image_from_bytes():
path = os.path.dirname(__file__)
with open(os.path.join(path, '../test_data/logo.png'), 'rb') as f:
img = f.read()

image_pane = PNG(img)
image_data = image_pane._data()
assert b'PNG' in image_data

def test_loading_a_image_from_pathlib():
"""Tests the loading of a image from a pathlib"""
Expand All @@ -107,7 +115,6 @@ def test_loading_a_image_from_pathlib():
image_data = image_pane._data()
assert b'PNG' in image_data


def test_image_alt_text(document, comm):
"""Tests the loading of a image from a url"""
url = 'https://file-examples-com.github.io/uploads/2017/10/file_example_PNG_500kB.png'
Expand Down

0 comments on commit 1d03ef4

Please sign in to comment.