I have been trying to automatically update a variable and run a code snippet after altering a ipywidget.
So far the only partial solution I have found is to declare a global variable kind of following the example on the github (here):
import ipywidgets as widgets
from IPython.display import display
x = 5
slider = widgets.IntSlider()
slider.value = x
def on_change(v):
global x
x = v['new']
slider.observe(on_change, names='value')
display(slider)
Ideally what I am trying to achieve is to automatically change the x value after altering the widget without the use of global variables and also change some previously defined variables. It would be something like this:
x = 5
y = []
slider = widgets.IntSlider()
slider.value = x
def on_change(v):
x = v['new']
y.append(x)
slider.observe(on_change, names='value')
display(slider)