0

I am trying to make a basic search tool for my Bokeh application. The basic process is:

  1. A user enters a search term into a TextInput widget.

  2. The TextInput value is passed to a search function and that returns a list of (tuples) options for a MultiSelect widget.

I run my app using a local server:

bokeh serve --show my_app

I cant get my MultiSelect options to update via a callback.

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
BenP
  • 825
  • 1
  • 10
  • 30
  • Have you determined if the text callback is getting called at all (i.e. by putting in some print statements)? Can you provide a *complete* minimal example that reproduces what you see, so that people who want to help an investigate directly? – bigreddot May 30 '19 at 14:40

1 Answers1

1

I solved this by calling the 2nd callback from within the first. So whenever I update the search term in TextInput, the search function is called and the MultiSelect widget options are re-populated.

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
BenP
  • 825
  • 1
  • 10
  • 30