Davide_sd
Davide_sd

Reputation: 13185

Jupyter Notebook - Linking two slider widgets adding an offset to the value

I'm reading the ipywidgets docs trying to understand if it's possible to achieve the following.

I have 2 sliders. It is simple to link the minimum value of the second slider to the actual value of the first slider:

a = widgets.IntSlider(value=3, min=0, max=5)
b = widgets.IntSlider(value=7, min=6, max=10)
l = widgets.jslink((a, 'value'), (b, 'min'))
display(a, b)

Is it possible to add an offset to the minimum value of the second slider? For instance, if slider a=4 I would like the minimum value of slider b to be 5 (so, an offset of 1). Also, would it be possible to link at the same time the value of the first slider to the maximum value of the second slider with a different offset? For instance, if slider a=4 I would like the maximum value of slider b to be 10 (so, an offset of 6).

Upvotes: 2

Views: 1646

Answers (1)

foglerit
foglerit

Reputation: 8279

One possible solution is to add an event handler that listens to value changes in a and updates the widget b accordingly:

from IPython.display import display
import ipywidgets as widgets

a = widgets.IntSlider(value=3, min=0, max=5)
b = widgets.IntSlider(value=7, min=6, max=10)
def on_value_change(change):
    new_a = change['new']
    b.min = new_a + 1
    b.max = new_a + 6
a.observe(on_value_change, names='value')
display(a, b)

Upvotes: 2

Related Questions