Skip to content

Pulling updated widget values from front-end #1287

@hannorein

Description

@hannorein

Summary:

Add the examples in #1287 (comment) and #1287 (comment) to the docs as an advanced example for having a request/reply pattern with widgets using generator-like syntax

Original issue

I can check for updates to a widget value using observe:

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)

But how can I do that during another operation, for example:

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

I tried doing something like

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

to pull for updates. But is seems unreliable. Is there a better way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussiongood first issueresolved-lockedClosed issues are locked after 30 days inactivity. Please open a new issue for related discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions