Issue
I am using widgets in IPython that allows the user to repeatedly search for a phrase and see the results (different titles) in another widget (a selection widget) and then select one of the results.
In short:
search_text = widgets.Text(description = 'Search')
search_result = widgets.Select(description = 'Select table')
def search_action(sender):
phrase = search_text.value
df = search(phrase) # A function that returns the results in a pandas df
titles = df['title'].tolist()
search_result.options = titles
search_text.on_submit(search_action)
This used to work fine, but after updating to the latest version of ipywidgets (5.1.3 from 4.0.1) it seems like
search_selection.options = titles
Produce the following errors (one or both, it varies):
TraitError: Invalid selection
TypeError: 'list' object is not callable
It still works in the sense that the widget gets updated with the results based on the search from the other widget, but it gives an error.
What is the correct way of setting the options in one widget based on the results from another widget?
(edit: added more detailed error message)
Solution
You can hold notifications for during the assignment to options
:
with search_result.hold_trait_notifications():
search_result.options = titles
Thus:
search_text = widgets.Text(description = 'Search')
search_result = widgets.Select(description = 'Select table')
def search_action(sender):
phrase = search_text.value
df = search(phrase) # A function that returns the results in a pandas df
titles = df['title'].tolist()
with search_result.hold_trait_notifications():
search_result.options = titles
See hmelberg's explanation below
"The root of the error is that the widget also has a value property and the value may not be in the new list of options. Because of this, widget value may be "orphaned" for a short time and an error is produced."
Answered By - Dan Schien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.