ipywidgets icon indicating copy to clipboard operation
ipywidgets copied to clipboard

New example to show that it is possible to get the current value of a widget slider from a function without using multithreading

Open richardstardust opened this issue 2 years ago • 0 comments

I wanted to get the current value of a widget slider from a function without using multitheading. There are some related examples regarding asynchronous widgets but I could not figure out how to do it from them.

Asynchronous Widgets (https://ipywidgets.readthedocs.io/en/8.0.0/examples/Widget%20Asynchronous.html#Asynchronous-Widgets)

I think that the code below would be a nice example to add. The code is probably not perfect but I think it shows a common use case.

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));

richardstardust avatar Aug 29 '22 07:08 richardstardust