我正在尝试使用Bokeh创建一个下拉菜单。菜单选项应从列表中添加。我已经使用Bokeh创建了列表和菜单,但是菜单中没有显示任何选项。请给我一些建议,以解决这个问题。谢谢!
首先,我从数据框列“ Continent”创建了选项列表。
options = data['Continent']
def unique(list):
# intilize a null list
unique_list = []
# traverse for all elements
for x in list:
# check if exists in unique_list or not
if x not in unique_list:
unique_list.append(x)
# print list
for x in unique_list:
print(x)
options = unique(data['Continent'])
然后我用bokeh创建菜单:
from bokeh.models.widgets import Dropdown
# Import output_file and show from bokeh.io
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
menu_widget = [options]
menu_dropdown = Dropdown(label = "Menu", menu=menu_widget)
output_file("dropdown.html")
show(widgetbox(menu_dropdown))
首先,我从数据框列“ Continent”创建了选项列表。
[如果您使用的是Pandas,则整个代码可以仅用data['Continent'].unique()
代替。如果您不使用熊猫,则可以使用list(set(data['Continent']))
或sorted(set(data['Continent']))
。
据您所知,unique
函数返回None
而不是唯一项列表。
问题是菜单中没有显示选项
除了上面返回None
的问题外,menu
参数必须是字符串元组列表中的字符串列表。但是,您传递了包装在列表中的列表。相反,请尝试仅使用menu=options
。