Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling ipywidget value manually

I have this code ipython code:

import ipywidgets as widgets
from IPython.display import display
import time

w = widgets.Dropdown(
    options=['Addition', 'Multiplication', 'Subtraction'],
    value='Addition',
    description='Task:',
)

def on_change(change):
    print("changed to %s" % change['new'])

w.observe(on_change)

display(w)

It works as expected. When the value of the widget changes, the on_change function gets triggered. However, I want to run a long computation and periodically check for updates to the widget. For example:

for i in range(100):
    time.sleep(1)
    # pull for changes to w here.
    # if w.has_changed:
    #     print(w.value)

How can I achieve this?

like image 361
hanno Avatar asked Sep 09 '26 05:09

hanno


1 Answers

For reference, I seem to be able to do the desired polling with

import IPython
ipython = IPython.get_ipython()
ipython.kernel.do_one_iteration()

(I'd still love to have some feedback on whether this works by accident or design.)

like image 159
hanno Avatar answered Sep 11 '26 05:09

hanno