Troubleshooting `ipywidgets.SelectionSlider`: Preserving Value Across Option Changes

In the following example, I’m trying to reset the value of the ipywidgets SelectionSlider to the minimum value according to an sample dataset:

import ipywidgets as widgets

data = {
    "A": [1,4,5,7],
    "B": [123, 452, 546],
    "C": [-23, 23,34, 342, ]
}

slider = widgets.SelectionSlider(options=data["A"], description='Slider')
dropdown = widgets.Dropdown(options=["A", "B", "C"], description="Value")
out = widgets.Output()

def update(change):
    slider.options = data[dropdown.value]
    slider.value = min(data[dropdown.value])
    with out:
        print(slider.options,slider.value)
    
dropdown.observe(update, "value")

display(slider, dropdown, out)

However, when I carry out the following steps, the slider fails to reset the value to the minimum for the current choice:

  • Move slider all the way to the right while A is selected in the drop down
  • Select B from the dropdown
  • Observe that the slider jumps to the right instead of jumping to the minimum value for B which is 123

Why does SelectionSlider exhibit this behavior?

“sometimes the slider fails to reset the value to the minimum for the current choice.”

That ‘sometimes’ is frustratingly rather vague.
I can tell you that where I see it consistently fail is if I start your code running so that ‘A’ is the present default value for the dropdown, and then I slide the slider all the way to left. If I do that set-up and then slide the slider all the way over to the right so that the value of the slider is 7, and then switch the drop-down to ‘B’, I see the slider stay over to the right and erroneously indicate ‘546’ as the value even though the slider.value for the print(slider.options,slider.value) is shown as ‘123’.
Similarly, if I next set the dropdown to ‘C’ and then slide the slider all the way to the right, then select ‘B’ as the dropdown, then the slider stays on the right indicating erroneously again ‘546’ as the value even though the slider.value for the print(slider.options,slider.value) is shown as ‘123’.
Do you see this type of behavior consistently?

I was wondering if you needed to link them explicitly in some way to get around that.

I usually use interactive() and so I tried that in the hopes it didn’t exhibit this weirdness. However, it does. Darn.
Here was my attempt at the conversion of your code to use interactive() for the record:

import ipywidgets as widgets
from ipywidgets import interactive

data = {
    "A": [1,4,5,7],
    "B": [123, 452, 546],
    "C": [-23, 23,34, 342, ]
}


my_slider = widgets.SelectionSlider(options=data["A"], description='Slider')
my_dropdown = widgets.Dropdown(options=["A", "B", "C"], description="Value")


def update_both(the_dropdown):
    info = f"I would love to try a {my_slider} {the_dropdown} {data[the_dropdown]}!"
    my_slider.options = data[the_dropdown]
    my_slider.value = min(data[the_dropdown])
    updated_info = f"I would love to try a {my_slider} {the_dropdown} {data[the_dropdown]}!"
    display(info)
    display(updated_info)
        
w = interactive(update_both, the_dropdown=my_dropdown) 
display(my_slider, w)

Thanks for the attempt. I’ve logged an issue at the main repo as I’m pretty sure this has to be a bug. Would be nice if someone could chime in with a viable workaround.