Both ipython widgets and interactive objects have observe() methods. (See the results of the print statements.) With the following example, I can confirm the actions of the observe() method on a slider widget but not on the interactive (ie) object.
Q: Is there any way to use the interactive’s observe method or I have to call separate observe() method on it’s all widget components ? If so, why ?
Expected behavior: Printing ‘ie change observed’ after changing inp1,
from IPython.display import display import ipywidgets as widgets int_range0_slider = widgets.IntSlider() int_range1_slider = widgets.IntSlider() output = widgets.Output() def interactive_function(inp0,inp1): with output: print('ie changed. int_range0_slider: '+str(inp0)+' int_range1_slider: '+str(inp1)) return def report_int_range0_change(change): with output: print('int_range0 change observed'+str(change)) return def report_ie_change(change): with output: print('ie change observed'+str(change)) return ie = widgets.interactive(interactive_function, inp0=int_range0_slider,inp1=int_range1_slider) print(int_range0_slider.observe) print(ie.observe) int_range0_slider.observe(report_int_range0_change, names='value') ie.observe(report_ie_change) display(int_range0_slider,int_range1_slider,output)
I’m a newbie, any help on the correct usage would be appreciated.
Advertisement
Answer
Yeah, no you can’t do that. The interactive object’s observe will be for changes in children. So, no change there.
What you could do instead is put an observe on the children. Like this.
for child in ie.children: child.observe(report_ie_change)
so, your code will look something like this:
from IPython.display import display import ipywidgets as widgets int_range0_slider = widgets.IntSlider() int_range1_slider = widgets.IntSlider() output = widgets.Output() def interactive_function(inp0,inp1): with output: print('ie changed. int_range0_slider: '+str(inp0)+' int_range1_slider: '+str(inp1)) return def report_int_range0_change(change): with output: print('int_range0 change observed'+str(change)) return def report_ie_change(change): with output: print('ie change observed'+str(change)) return ie = widgets.interactive(interactive_function, inp0=int_range0_slider,inp1=int_range1_slider) # print(int_range0_slider.observe) # print(ie.observe) # int_range0_slider.observe(report_int_range0_change, names='value') for child in ie.children: child.observe(report_ie_change) display(int_range0_slider,int_range1_slider,output)