Is it possible to get the current value of a widget slider from a function without using multithreading?

I am trying to access the current slider value from a function, but it seems that it can only get the value that the slider had when the function was called.
Is there a way to do this without using multi threading?

Code example:

import ipywidgets as widgets
import time

slider = widgets.IntSlider(
    value=5,
    min=0,
    max=10,
    step=1,
    description='Test:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,
    readout_format='d'
)

def test(slider, output):
    i = 0
    while slider.value != 10:
        i = i+1
        time.sleep(0.1)
        with output:
            output.clear_output()
            print('test ' + str(slider.value) + ' - ' + str(i))

output = widgets.Output()
display(slider, output)

test(slider, output)

Inside a modern ipython/ipykernel, you’ll pretty much always be inside an asyncio-compatible event loop. If you block, though, then things stop flowing.

Output is kind of an implementation nightmare, with each frontend being slightly different. However, display(..., display_id=True).update works great.

So:

import ipywidgets as widgets
import asyncio

slider = widgets.IntSlider(
    value=5,
    min=0,
    max=10,
    step=1,
    description='Test:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,
    readout_format='d'
)

async def test(slider, output):
    i = 0
    while slider.value != 10:
        i = i+1
        await asyncio.sleep(0.1)
        output.update('test ' + str(slider.value) + ' - ' + str(i))

output = display("tbd", display_id=True)
display(slider)

asyncio.create_task(test(slider, output));

Related: Threading with Matplotlib and ipywidgets - #2 by bollwyvl

1 Like

Thank you! Great solution!
It was exactly the type of solution I was looking for.
I will suggest that they add this as an example to the manual.