Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In IPython Widgets, how to update the DropDown widget with new value?

I created a DropDown widget:

self.foo_widget = widgets.Dropdown(description='Lorem ipsum', width=100)
self.foo_widget.options = ['Default', 'A', 'B', 'C', 'D']

And I capture the on_trait_change event:

self.foo_widget.on_trait_change(self.handler, 'value')

Now in the handler function handler, I want set the DropDown value back to 'Default'. But the following code only changes the value without updating the widget display. The DropDown still shows the original selection value (e.g., 'C') even though print self.foo_widget.value shows to be 'Default'.

self.foo_widget.value = 'Default'

Is this a bug of IPython Widget? What is the correct way to cause the update of the view?

In fact, for the list widgets, it seems I have to clear the options and assign options again to cause the widgets' display to update. Anyone has similar experience?

Update: the answer by nluigi below works great. As shown in the following example.

class test(object): 
    def __init__(self):
        self.foo_widget = widgets.Dropdown(description='Lorem ipsum', width=100)
        self.foo_widget.options = ['Default', 'A', 'B', 'C', 'D']
        self.foo_widget.on_trait_change(self.handler, 'value')
        display(self.foo_widget)

    def handler(self, name, old, new):
        print(self.foo_widget.value)
        print(self.foo_widget.selected_label)
        self.foo_widget.value = 'Default'
        self.foo_widget.selected_label = 'Default'
like image 777
Pan Yan Avatar asked Sep 04 '15 12:09

Pan Yan


People also ask

How do I add a widget to the drop down menu?

Go to “Dropdown menu settings” under “Settings” to change dropdown theme and edit your settings. Add the dropdown menu to your site using “Widgets” or template tag codes. Done.

What are IPython widgets?

Communication. ipywidgets, also known as jupyter-widgets or simply widgets, are interactive HTML widgets for Jupyter notebooks and the IPython kernel. Notebooks come alive when interactive widgets are used. Users gain control of their data and can visualize changes in the data.

How do you interact in a Jupyter notebook?

At the most basic level, interact autogenerates UI controls for function arguments, and then calls the function with those arguments when you manipulate the controls interactively. To use interact , you need to define a function that you want to explore. Here is a function that returns its only argument x .


1 Answers

Per the specification of the selection class, you must also set the selected_label attribute to 'Default' to have the widget update:

self.dropDown.value = 'Default'
self.dropDown.selected_label = 'Default'
like image 54
nluigi Avatar answered Sep 27 '22 22:09

nluigi