Contents

Polling Jupyter widget UI events in runtime

Contents

Let’s look at a code snippet in the Jupyter Notebook:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import time
import ipywidgets as widgets
from IPython.display import display

slider = widgets.IntSlider()
display(slider)

while True:
    print(slider.value)
    time.sleep(1)

IntSlider is an interactive Jupyter Notebook widget. When the user interacts with the slider, the slider value should change.

However, if you run the above codes, you will find that the printed value of the slider won’t change at all – it will be stuck at its initial value.


In fact, the UI elements won’t be updated until all code blocks are finished executing. In other words, the update of the UI elements is blocked by the execution of any code block.

In order to poll the states of UI elements inside a running loop, we can use the jupyter-ui-poll library. The modified codes are as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import time
import ipywidgets as widgets
from IPython.display import display
from jupyter_ui_poll import ui_events

slider = widgets.IntSlider()
display(slider)

while True:
    with ui_events() as poll:
        poll(1) # poll one event
    print(slider.value)
    time.sleep(1)