Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Block kernel until ipywidget's initial rendering has completed #3144

Open
anant-k-singh opened this issue Mar 2, 2021 · 3 comments

Comments

@anant-k-singh
Copy link

Question:
I am building a custom ipywidget and the first rendering of widget (in output cell) takes some time. The following cells in the notebook interact with the widget and it requires the widget to be fully rendered before interacting with it in other cells.
There is no python function which I can await, as the widget start rendering as soon as we execute the widget class's object

In other words, I want the kernel to wait until DOMWidgetView.render() has finished rendering in output cell.

@anant-k-singh anant-k-singh changed the title Question: Block kernel until ipywidget's rendering has completed Question: Block kernel until ipywidget's initial rendering has completed Mar 2, 2021
@vidartf
Copy link
Member

vidartf commented Mar 2, 2021

I would expose a Promise on the "slow to start" widget, and have any widgets that need to interact with that one await the widget before rendering themselves fully. A (somewhat complicated) example is in pythreejs, where we use an initPromise: https://github.com/jupyter-widgets/pythreejs/search?q=initPromise

@vidartf
Copy link
Member

vidartf commented Mar 2, 2021

Relevant other points of note, that might or might not be useful:

@link89
Copy link

link89 commented Nov 1, 2023

Hi @vidartf I am also looking for a method to block execution until specific event happens, but it turns out that the event won't be sent if the main thread of Jupyter is blocking. For example,

import asyncio
from IPython.display import display
from ipywidgets import Text
def wait_for_change(widget, value):
    future = asyncio.Future()
    def getvalue(change):
        # make the new value available
        future.set_result(change.new)
        widget.unobserve(getvalue, value)
    widget.observe(getvalue, value)
    return future

text_input = Text()
print('before', text_input.value)

display(text_input)
future = wait_for_change(text_input, 'value') 

await asyncio.ensure_future(future)  # block forever, the future didn't get resolved after user finishing input

# never reach this line
print(text_input.value)

There are more examples in the SO issue: https://stackoverflow.com/questions/77363909/how-to-block-execution-until-jupyter-widgets-value-has-been-changed Is there any solution to make it work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants