Hi, I’m trying to create an image carousel.
import ipywidgets as widgets
from ipywidgets import register
@register
class ImageCarousel(widgets.DOMWidget):
def __init__(self, images, image_width=500):
self._images = images
self._index = 0
self._btn_left = widgets.Button(
icon="caret-left",
layout=widgets.Layout(
width='30px',
height='100%',
)
)
self._btn_right = widgets.Button(
icon="caret-right",
layout=widgets.Layout(
width='30px',
height='100%',
)
)
self._btn_left.on_click(self._on_click)
self._btn_right.on_click(self._on_click)
self._image = widgets.Image(
width=image_width,
)
self.panel = widgets.HBox(
children=[self._btn_left, self._image, self._btn_right],
layout=widgets.Layout(
height='100%',
)
)
self._change_image(0)
def _on_click(self, button):
if button == self._btn_left:
self._change_image(-1)
elif button == self._btn_right:
self._change_image(1)
def _change_image(self, increment):
if not self._images:
self._btn_left.disabled = True
self._btn_right.disabled = True
return
n = len(self._images)
self._index = (self._index + increment) % n
self._btn_left.disabled = self._index <= 0
self._btn_right.disabled = self._index >= n-1
self._image.set_value_from_file(images[self._index])
images = [
'./images/max-heap-1.webp',
'./images/max-heap-2.webp',
'./images/min-heap-1.webp',
'./images/min-heap-2.webp',
]
ImageCarousel(images).panel
It works fine, but I’m unable to display it directly. I need to display the panel member.
Is there a way to make ImageCarousel directly “displayable”?
