What I need to do is a function that would pause the script execution and display a “continue” button widget which would resume the execution after being pushed. The function would be used like so:
print("Hello, please push the continue button")
displayContinueAndWaitForPress()
print("You have pushed the button, congratulations I guess.")
As trivial as this looks like, I am having a very hard time to make it to work.
The Jupyter doc provides two ways to do it: Asynchronous and Generator based.
I can’t used the asynchronous method as my function will be implemented in a bigger script that is not running in an async loop.
The generator based function does print some stuff after pressing the button, but it doesn’t seem to stop the execution the way I would need it to, and shows “You have pushed the button, congratulations I guess.” before I press the button.
Here’s the code I’m using:
from functools import wraps
def yield_for_change(widget):
def f(iterator):
@wraps(iterator)
def inner():
i = iterator()
def next_i(change):
try:
i.send(change)
except StopIteration as e:
widget.unobserve(next_i, attribute)
widget.on_click(next_i)
# start the generator
next(i)
return inner
return f
from ipywidgets import Button
button=Button()
@yield_for_change(button)
def f():
for i in range(10):
print('did work %s'%i)
x = yield
def displayContinueAndWaitForPress():
f()
display(button)
/
print("Hello, please push the continue button")
displayContinueAndWaitForPress()
print("You have pushed the button, congratulations I guess.")
I am not very well experienced with Python and Jupiter but do you see what’s my problem? I’m surpised a library such a ipwidgets makes it so easy to make layouts but doesn’t provide much help with a use like mine.
Thank you for your help,
Kromahtique