Pause Jupyter Notebook widgets, waiting for user input(暂停 Jupyter Notebook 小部件,等待用户输入)
                            本文介绍了暂停 Jupyter Notebook 小部件,等待用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
I have the same issue as TheInterestedOne asked here.
I need to create two buttons for the user, and suggest that the user clicks one of the two buttons in the loop; so that the next iteration of the loop occurs only after the user's choice.
I read this source, but I can't make it work for buttons. I don't understand, how the widget attribute changes in the case of using buttons.
from functools import wraps
def yield_for_change(widget, attribute):
    def f(iterator):
        @wraps(iterator)
        def inner():
            i = iterator()
            def next_i(change):
                try:
                    i.send(change.new)
                except StopIteration as e:
                    widget.unobserve(next_i, attribute)
            widget.observe(next_i, attribute) //**button.on_click(on_button_clicked) 
                                                                may be?**
            # start the generator
            next(i)
        return inner
    return f
from ipywidgets import Button
button=Button()
def on_button_clicked():
    print("Button clicked.")
@yield_for_change(button, 'value')
def f():
    for i in range(10):
        print('did work %s'%i)
        x = yield
        button.on_click(on_button_clicked)
 解决方案 
This version uses awaitio and is modified for buttons. 
from ipywidgets import Button
import asyncio
def wait_for_change(widget):
    future = asyncio.Future()
    def getvalue(change):
        future.set_result(change.description)
        widget.on_click(getvalue, remove=True) 
        # we need to free up the binding to getvalue to avoid an InvalidState error
        # buttons don't support unobserve
        # so use `remove=True` 
    widget.on_click(getvalue)
    return future
button = Button(description="wow")
list_to_tag = ["one", "two", "three", "four"]
async def f():
    for i in list_to_tag:
        print("going to tag {}".format(i))
        x = await wait_for_change(button)
        print("tagged {} with {}".format(i, x))
        print()
asyncio.create_task(f())
button
这篇关于暂停 Jupyter Notebook 小部件,等待用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:暂停 Jupyter Notebook 小部件,等待用户输入
				
        
 
            
        基础教程推荐
             猜你喜欢
        
	     - PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 - 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - 修改列表中的数据帧不起作用 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 - PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 - 包装空间模型 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				