DynamicMap 可调用对象是否总是必须 return 相同的类型?

Does a DynamicMap callable always have to return the same type?

下面的代码使传递给 DynamicMap 的可调用对象看起来无法更改元素类型。将类别小部件更改为 B 时,将返回 Curve 元素而不是 Points,但绘图只是空的。是否不支持此行为,或者是否需要一些 opts 魔法才能使其工作?

import holoviews as hv
import numpy as np
import panel as pn
hv.extension('bokeh')
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

main_category_widget = pn.widgets.Select(name='Main category', options=['A','B'], value='A')
widgets = main_category_widget

@pn.depends(selected_main_category=main_category_widget.param.value)
def get_points(selected_main_category):
    if selected_main_category == 'A':
        return hv.Points(np.random.rand(5,5)).opts(size=10, title='Points')
    else:
        return hv.Curve(np.random.rand(5,5)).opts(title='Curve')

points = hv.DynamicMap(get_points).opts(height=400, width=800)

pn.Row(widgets, points)

使用 hv.Overlay 作为 return 类型的 get_points() 您的代码按预期工作:

import holoviews as hv
import numpy as np
import panel as pn
hv.extension('bokeh')

main_category_widget = pn.widgets.Select(name='Main category', options=['Points','Curve'], value='Points')

@pn.depends(selected_main_category=main_category_widget.param.value)
def get_points(selected_main_category):
    images = []
    if selected_main_category == 'Points':
        images.append(hv.Points(np.random.rand(5,5)).opts(size=10, title='Points'))
    else:
        images.append(hv.Curve(np.random.rand(5,5)).opts(title='Curve'))
    return hv.Overlay(images)

points = hv.DynamicMap(get_points).opts(height=400, width=800)

pn.Row(main_category_widget, points)