I'm trying to "widgetize" my IPython notebooks and am running into trouble with events and returning values from a function. This is the workflow I'm thinking is the best way to go:
- using widgets to get the input values for an arbitrary function
- call the function on event trigger
- return value from function
I first tried using the "interact" method to call the function, but that seemed difficult to associate events and return values. From reading other interactive examples, making a class seemed like the way to go. I don't write classes very often; so hopefully my error is something simple there.
The following makes two widgets, and when the user presses "Enter" should call a function and store its return value in the class for future use.
In reality, it fires off the function two times before I enter any text and throws 'unicode object is not callable' when I change value.
import ipywidgets as widgets
from IPython.display import display
def any_function_returning_value(word1,word2):
new_word = 'Combining words is easy: %s %s'%(word1,word2)
print new_word
return new_word
class learn_classes_and_widgets():
def __init__(self, param1 = 'a word', param2 = 'another word'):
self.p1_text = widgets.Text(description = 'Word #1',value = param1)
self.p2_text = widgets.Text(description = 'Word #2',value = param2)
self.p1_text.on_submit(self.handle_submit())
self.p2_text.on_submit(self.handle_submit())
display(self.p1_text, self.p2_text)
def handle_submit(self):
print "Submitting"
self.w = any_function_returning_value(self.p1_text.value,self.p2_text.value)
return self.w
f = learn_classes_and_widgets(param1 = 'try this word')
#f.w should contain the combined words when this is all working