我有一组灰度图像(2D numpy 数组),并希望根据 holoviews.MultiSelect 小部件的状态同时显示其中的几个,但在 MultiSelect 小部件中进行选择时无法更新图像。这是一个最小的工作示例:
pn.extension()
import numpy as np
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
# Make grayscale test images
images = []
xmax, ymax = 2560, 1600
for i in range(5):
# Gray background
image = np.ones(shape=(ymax, xmax), dtype=np.uint8) * 200
# Make each image unique with different position for a black square
# with interior white square
image[200*i:200*i + 400,200*i:200*i + 400] = 0
image[200*i+175:200*i+175 + 50,200*i+175:200*i+175 + 50] = 255
images.append(image)
# Make hv.Images objects
scale_factor = 8
bounds=(0, 0, xmax, ymax) # Coordinate system: (left, bottom, right, top)
img_0 = hv.Image(images[0], bounds=bounds)
img_1 = hv.Image(images[1], bounds=bounds)
options = [
opts.Image(cmap='gray',
aspect='equal',
frame_width=int(xmax/scale_factor),
frame_height=int(ymax/scale_factor),
)
]
img_0.opts(*options)
img_1.opts(*options)
# Set up selector object
image_selector = pn.widgets.MultiSelect(name="Choose image index",
options=[i for i in range(len(images))],
size=6,
width=120
)
# Define interactivity
@pn.depends(image_selector, watch=True)
def change_image_shown(image_selector):
index = image_selector[0]
img_0.data = images[index]
if index != len(images) - 1:
img_1.data = images[index + 1]
else:
img_1.data = images[index]
# Create panel layout
layout = pn.Row(image_selector, pn.Column(img_0, img_1))
layout
初始面板布局符合预期,但是当我在 MultiSelect 中选择索引时,图像不会更新。检查 MultiSelect 对象的值,
image_selector.value[0]
我得到了选定的索引,所以问题必须出在装饰change_image_shown
函数中,但我没有尝试过任何工作。我错过了什么?