ILoveCoding
ILoveCoding

Reputation: 916

Call decorated interactive function with widget arguments

Say I created an interactive function using ipywidgets as such:

from ipywidgets import interactive, Select

def print_fruit(fruit):
    print(fruit)

fruitSelect = Select(options=['tomato', 'banana'])

w = interactive(print_fruit, fruit=fruitSelect)
w

Now, I want to call print_fruit with the currently selected fruit from another function. What would be the recommended method ?(besides redoing the same work of getting the selected options from the widgets and calling print_fruit with the correct argument, which is tedious when there are many...).

Thanks

Upvotes: 1

Views: 351

Answers (1)

ac24
ac24

Reputation: 5565

You can access the current value of a widget by accessing the .value parameter.

So in this case, print_fruit(fruitSelect.value) should do it.

EDIT: For multiple widgets

Grab the widgets from w with the exception of the last one (the Output widgets) in a comprehension.

print_fruit(*(widg.value for widg in w.children[:-1]))

Upvotes: 1

Related Questions