我正在玩MultiSelect
小部件,我发现它产生了一个我无法解释的错误。
以下是没有错误的工作代码:
from bokeh.io import curdoc
from bokeh.layouts import column, widgetbox
from bokeh.models import Slider, Select, TextInput, MultiSelect
df = ["apples", "oranges", "grapes"]
def callback(attr, old, new):
print(df)
print("{} changed: Old [ {} ] -> New [ {} ]".format(attr, old, new))
# multiselect1.update(options = [x for x in df if new in x])
multiselect1 = MultiSelect(title = "multiselect1",
name = "multiselect1",
value = [],
options = df)
multiselect1.on_change('value', callback)
curdoc().add_root(widgetbox(multiselect1))
打印出来的是预期的:
['apples', 'oranges', 'grapes']
value changed: Old [ [] ] -> New [ ['oranges'] ]
关于工作部分......很多
对于 not working part ...只要注释了multiselect1.update(options = [x for x in df if new in x])
并且新的callback
函数是:
def callback(attr, old, new):
print(df)
print("{} changed: Old [ {} ] -> New [ {} ]".format(attr, old, new))
multiselect1.update(options = [x for x in df if new in x])
...它会生成错误:
['apples', 'oranges', 'grapes']
value changed: Old [ [] ] -> New [ ['oranges'] ]
2018-05-17 13:34:48,755 error handling message Message 'PATCH-DOC' (revision 1): TypeError("'in <string>' requires string as left operand, not PropertyValueList",)
知道为什么会产生这个错误以及如何避免它?
答案 0 :(得分:2)
看起来变量new
是一个列表,而下面的列表理解需要new
是一个字符串:
[x for x in df if new in x]
您可以尝试索引new
列表以获取字符串值,如下所示:
[x for x in df if new[0] in x]