6

I am trying to link two ipython widgets together so that when one widget updates (slider moves, text inputted, etc) another widget also updates. I have found that you can link the values together using traitlets.link, but cannot find a way to link together just the update behavior. Basically I want one widget to call its on_trait_change (now called observe) handler whenever the other one does.

I can't use the method suggested for this question because the handler for the widget that needs to update relies on variables that are not accessible to the "trigger" widget, so the handler for the "trigger" widget cannot simply include the actions required of the widget that needs to update.

Example:

from ipywidgets import widgets
from IPython.display import display

data={'foo':3, 'bar':5}
private_data=10 #input1 widget can use this data, input2 widget can't

output=widgets.Text(value='outputs', disabled=True)

def input_update(x):
    data['foo']=x
    output.value=str(data['foo']+data['bar']+private_data)

#elsewhere in another class, not privy to private_data
def input_update2(y):
    data['bar']=y

input1=widgets.interactive(input_update,x=10)
input2=widgets.interactive(input_update2,y=10)
display(output,input1,input2)
KEL
  • 61
  • 4

0 Answers0