gpt4 book ai didi

python - 暂停Jupyter Notebook小部件,等待用户输入

转载 作者:行者123 更新时间:2023-12-03 13:51:03 25 4
gpt4 key购买 nike

我和TheInterestedOne询问here有同样的问题。

我需要为用户创建两个按钮,并建议用户单击循环中两个按钮之一;因此,只有在用户选择之后,循环的下一次迭代才会发生。

我读了this source,但是不能使它适用于按钮。我不明白,在使用按钮的情况下,widget属性是如何变化的。

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)

最佳答案

此版本使用awaitio并针对按钮进行了修改。

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

关于python - 暂停Jupyter Notebook小部件,等待用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55244865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com