Bokeh下拉菜单中的一列唯一值

问题描述 投票:0回答:1

我正在尝试使用Bokeh创建一个下拉菜单。菜单选项应从列表中添加。我已经使用Bokeh创建了列表和菜单,但是菜单中没有显示任何选项。请给我一些建议,以解决这个问题。谢谢!

数据框:enter image description here

首先,我从数据框列“ 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))

问题是菜单中没有显示任何选项(请参见图波纹)。enter image description here

python menu bokeh
1个回答
0
投票

首先,我从数据框列“ Continent”创建了选项列表。

[如果您使用的是Pandas,则整个代码可以仅用data['Continent'].unique()代替。如果您不使用熊猫,则可以使用list(set(data['Continent']))sorted(set(data['Continent']))

据您所知,unique函数返回None而不是唯一项列表。

问题是菜单中没有显示选项

除了上面返回None的问题外,menu参数必须是字符串元组列表中的字符串列表。但是,您传递了包装在列表中的列表。相反,请尝试仅使用menu=options

© www.soinside.com 2019 - 2024. All rights reserved.