Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for webp format #6035

Merged
merged 9 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/reference/panes/Image.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"source": [
"jpg_pane = pn.pane.Image('https://assets.holoviz.org/panel/samples/jpeg_sample.jpeg')\n",
"png_pane = pn.pane.Image('https://assets.holoviz.org/panel/samples/png_sample.png')\n",
"webp_pane = pn.pane.Image('https://mistral.ai/images/logo_hubc88c4ece131b91c7cb753f40e9e1cc5_2589_256x0_resize_q97_h2_lanczos_3.webp')"
"webp_pane = pn.pane.Image('https://assets.holoviz.org/panel/samples/webp_sample.webp')"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion panel/pane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from .equation import LaTeX # noqa
from .holoviews import HoloViews, Interactive # noqa
from .image import ( # noqa
GIF, ICO, JPG, PDF, PNG, SVG, Image,
GIF, ICO, JPG, PDF, PNG, SVG, Image, WebP,
)
from .ipywidget import IPyLeaflet, IPyWidget, Reacton # noqa
from .markup import ( # noqa
Expand Down
8 changes: 4 additions & 4 deletions panel/pane/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,18 +497,18 @@ def _transform_object(self, obj: Any) -> Dict[str, Any]:
html = f'<embed src="{obj}{page}" width={w!r} height={h!r} type="application/pdf">'
return dict(text=escape(html))

class WEBP(ImageBase):
class WebP(ImageBase):
"""
The `WEBP` pane embeds a .webp image file in a panel if
The `WebP` pane embeds a .webp image file in a panel if
provided a local path, or will link to a remote image if provided
a URL.

Reference: https://developers.google.com/speed/webp/docs/riff_container

:Example:

>>> WEBP(
... 'https://www.gstatic.com/webp/gallery/4.sm.webp',
>>> WebP(
... 'https://assets.holoviz.org/panel/samples/webp_sample.webp',
... alt_text='A nice tree',
... link_url='https://en.wikipedia.org/wiki/WebP',
... width=500,
Expand Down
12 changes: 7 additions & 5 deletions panel/tests/pane/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
from requests.exceptions import MissingSchema

from panel.pane import (
GIF, ICO, JPG, PDF, PNG, SVG,
GIF, ICO, JPG, PDF, PNG, SVG, WebP,
)
from panel.pane.markup import escape

JPG_FILE = 'https://assets.holoviz.org/panel/samples/jpg_sample.jpg'
JPEG_FILE = 'https://assets.holoviz.org/panel/samples/jpeg_sample.jpeg'
PNG_FILE = 'https://assets.holoviz.org/panel/samples/png_sample.png'
SVG_FILE = 'https://assets.holoviz.org/panel/samples/svg_sample.svg'

WEBP_FILE = 'https://assets.holoviz.org/panel/samples/webp_sample.webp'

def test_jpeg_applies():
assert JPG.applies(JPEG_FILE)
Expand Down Expand Up @@ -69,11 +69,13 @@ def test_svg_pane(document, comm):
b'AAAAAAAAAAAAAAAAFBv/EABkRAAEFAAAAAAAAAAAAAAAAAAEAAjFxsf/aAAwDAQ' + \
b'ACEQMRAD8AA0qs5HvTHQcJdsChioXSbOr/2Q==',
ico = b'AAABAAEAAgEAAAEAIAA0AAAAFgAAACgAAAACAAAAAgAAAAEAIAAAAAAACAAAAHQ' + \
b'SAAB0EgAAAAAAAAAAAAD//////////wAAAAA=')

b'SAAB0EgAAAAAAAAAAAAD//////////wAAAAA=',
webp= b'iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAYAAAD0In+KAAAAFElEQVQIHQEJAPbA' + \
b'WNYYP/h4uMAFL0EwlEn99gAAAAASUVORK5CYIA=='
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved
)

def test_imgshape():
for t in [PNG, JPG, GIF, ICO]:
for t in [PNG, JPG, GIF, ICO, WebP, ]:
w,h = t._imgshape(b64decode(twopixel[t.name.lower()]))
assert w == 2
assert h == 1
Expand Down
Loading