0

我正在尝试为我的 Bokeh 应用程序制作一个基本的搜索工具。基本流程是:

  1. 用户在TextInput小部件中输入搜索词。

  2. TextInput值被传递给一个搜索函数,该函数返回一个MultiSelect小部件的(元组)选项列表。

我使用本地服务器运行我的应用程序:

bokeh serve --show my_app

我无法通过回调更新MultiSelect选项。

def createtab_search():

    # callback text_input
    def callback_text_input(attr, old, new):
        print("Previous label: " + old)
        print("Updated label: " + new)

    text_input = TextInput(value="search here", title="Search:")
    text_input.on_change("value", callback_text_input)


    # callback search results    
    def callback_search_result(attr,old,new):
        search_results = search_function(text_input.value)
        options = [(k,k) for k in search_results.items()]
        multi_select.options = options

    # results dropdown
    multi_select = MultiSelect(title="Results:",
                           value=[],
                           options=[])
    multi_select.on_change("options",callback_search_result)         

    # Layout setup
    layout = column(text_input,multi_select)

    #output tab
    tab = Panel(child = layout, title = 'Search')
    return tab
4

1 回答 1

0

我通过从第一个回调中调用第二个回调来解决这个问题。因此,每当我更新TextInput中的搜索词时,就会调用搜索函数并重新填充MultiSelect小部件选项。

def createtab_search():

    # callback text_input
    def callback_text_input(attr, old, new):
        callback_search_result(attr, old, new)

    text_input = TextInput(value="search here", title="Search:")
    text_input.on_change("value", callback_text_input)


    # callback search results    
    def callback_search_result(attr,old,new):
        search_results = search_function(text_input.value)
        options = [(k,k) for k in search_results.items()]
        multi_select.options = options

    # results dropdown
    multi_select = MultiSelect(title="Results:",
                           value=[],
                           options=[])
    multi_select.on_change("options",callback_search_result)         

    # Layout setup
    layout = column(text_input,multi_select)

    #output tab
    tab = Panel(child = layout, title = 'Search')
    return tab
于 2019-05-30T15:54:57.160 回答