SHARE
TWEET

Untitled

a guest Mar 19th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from functools import wraps
  2. def yield_for_change(widget, attribute):
  3.     def f(iterator):
  4.         @wraps(iterator)
  5.         def inner():
  6.             i = iterator()
  7.             def next_i(change):
  8.                 try:
  9.                     i.send(change.new)
  10.                 except StopIteration as e:
  11.                     widget.unobserve(next_i, attribute)
  12.             widget.observe(next_i, attribute)
  13.             # start the generator
  14.             next(i)
  15.         return inner
  16.     return f
  17.  
  18.  
  19. from ipywidgets import Button
  20. button=Button()
  21.  
  22.  
  23. def on_button_clicked():
  24.     print("Button clicked.")
  25.  
  26.  
  27. @yield_for_change(button, 'value')
  28. def f():
  29.     for i in range(10):
  30.         print('did work %s'%i)
  31.         x = yield
  32.         button.on_click(on_button_clicked)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top