Search code examples
pythonipywidgets

Is there a way where you can reassign options to dropdown in ipywidgets


I want to update the dropdown option when i update the referred list. Is there a way of doing it?

options_list = []

def update_options_list():
c.exexute('SQL CODES')
for ids in c:
  options_list.append(c[0])

dropdown_lable = widgets.Dropdown(options=options_list, value = None)

def button_function():
  update_options_list
  dropdown_lable.options = options_list

Is there a way to apply the updated options list to dropdown_lable.options?

I come across this error: 'NoneType' object is not callable


Solution

  • Simply

    dropdown_lable.options = options_list
    

    BTW:

    Using Google ipywidgets dropdown update options I found it in 5 minutes.
    And you had to wait 1 hour for answer on Stackoverflow. Next time first check in Google.


    EDIT:

    Minimal working example

    import ipywidgets as widgets
    
    # --- functions ---
    
    def update_options(widget):
        global options
        
        #print('widget:', widget)
        #widget.description = 'Updated !!!'
        
        options = ['A', 'B', 'C']        
        dropdown.options = options
        
    # --- main ---
    
    options = [1, 2, 3]
    
    dropdown = widgets.Dropdown(options=options)
    
    button = widgets.Button(description='Update')
    button.on_click(update_options)
    
    box = widgets.HBox([dropdown, button])
    
    box
    

    enter image description here