ipywidgets小部件的值未更改 [英] ipywidgets widgets values not changing

查看:186
本文介绍了ipywidgets小部件的值未更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从在Python 3.6中运行Jupyter Notebooks的Microsoft Azure Notebooks中的ipywidgets小部件获取输出.但是,当我获取新值时,它不会返回新值.这也适用于从未被其他窗口小部件调用的事件处理程序/交互.

I am trying to get output from my ipywidgets widgets in Microsoft Azure Notebooks running Jupyter Notebooks in Python 3.6. However, it does not return new values when I get them. This also applies to the event handlers/interact never being called for other widgets.

我尝试使用不同类型的小部件(TextTextareaCheckboxButtonToggleButton)输入不同的初始值.我尝试在按钮上使用w.valueipywidgets.interactw.observew.on_click.

I have tried putting in different initial values, using different types of widgets (Text, Textarea, Checkbox, Button, ToggleButton). I have tried getting the w.value, ipywidgets.interact, w.observe, and w.on_click on Buttons.

我所做的测试:

import time
import ipywidgets as widgets
from IPython.display import display

w = widgets.Text(disabled=False)
display(w)

while True:
    print(w.value)
    time.sleep(1)

我希望当我在Text字段中输入内容时,它将输出该文本,但是它将继续打印出其开头的内容.没有错误.因此,对于上面的示例,无论我在结果文本字段中输入什么内容,所有打印的内容都是空行.

I expect that when I enter something into the Text field, that it will output that text, but instead it continues printing out what it started with. There are no errors. So, for the above example, regardless of what I input into the resultant Text field, all that is printed is empty lines.

推荐答案

问题是小部件和Python之间的通信 内核是异步且令人困惑的.

The problem is that communication between widgets and the Python kernel is asynchronous and confusing.

time.sleep(...)仅阻止Python解释器并执行 不允许小部件Javascript实现将更改后的值发送给Python 内核(因为Python内核被阻止并且什么也不做).

time.sleep(...) in the cell only blocks the Python interpreter and does not allow the widget Javascript implementation to send the changed value to the Python kernel (because the Python kernel is blocked and not doing anything).

如果创建窗口小部件,然后修改窗口小部件文本条目,然后评估 在下一个单元格 中以交互方式w.value,您将看到更改后的值.

If you create the widget and then modify the widget text entry and then evaluate w.value in the next cell interactively you will see the changed value.

在此处查看进一步的讨论(查找异步"):

See further discussion here (look for "async"):

https://github.com/AaronWatters/jp_proxy_widget/blob /master/notebooks/Tutorial.ipynb

通常,如果要强制Python解释器查看从Javascript小部件实现发送的某些值,则Javascript端必须以某种方式回调Python解释器,并且Python解释器不能被睡眠或任何其他此类机制阻止

In general if you want to force the Python interpreter to see some value sent from the Javascript widget implementation the Javascript side must call back to the Python interpreter in some way and the Python interpreter cannot be blocked by sleep or any other such mechanism.

这篇关于ipywidgets小部件的值未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆