散景:如果触发了另一个回调,则触发回调

时间:2019-05-30 09:26:54

标签: python callback bokeh bokehjs

我正在尝试为我的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

1 个答案:

答案 0 :(得分: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