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'
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.
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.
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 .
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With