问题描述
我正在尝试复制官方的异步小部件-示例在jupyter 实验室上,但await
永远不会继续.
I'm trying to reproduce the official Asynchronous Widgets-Example on jupyter lab, but the await
never continues.
-
docker run --rm -p 8888:8888 -e JUPYTER_ENABLE_LAB=yes jupyter/datascience-notebook start-notebook.sh --NotebookApp.token=''
-
firefox 0.0.0.0:8888
- 创建一个新的python3笔记本
- 创建一个单元格并在下面输入代码
- 运行单元格
- 移动滑块
docker run --rm -p 8888:8888 -e JUPYTER_ENABLE_LAB=yes jupyter/datascience-notebook start-notebook.sh --NotebookApp.token=''
firefox 0.0.0.0:8888
- create a new python3 notebook
- create a cell and enter the code below
- run cell
- move slider
单元格的代码
%gui asyncio
import asyncio
def wait_for_change(widget, value):
future = asyncio.Future()
def getvalue(change):
# make the new value available
future.set_result(change.new)
widget.unobserve(getvalue, value)
widget.observe(getvalue, value)
return future
from ipywidgets import IntSlider
slider = IntSlider()
async def f():
for i in range(10):
print('did work %s'%i)
#x = await asyncio.sleep(1)
x = await wait_for_change(slider, 'value')
print('async function continued with value %s'%x)
asyncio.ensure_future(f())
#task = asyncio.create_task(f())
slider
预期结果
单元格输出
Expected result
The cell outputs
did work 0
async function continued with value 1
did work 1
async function continued with value 2
[...]
实际输出
第一个did work 0
-
我专门讲的是jupyter 实验室,而不是普通的jupyter笔记本
I'm specifically talking about jupyter lab and not about regular jupyter notebooks
没有错误消息或任何东西.预期的输出不会发生
There is no error-message or anything. The expected output just doesn't happen
最小的asyncio示例在jupyter实验室中有效:
The minimal asyncio-example does work in jupyter lab:
import asyncio
async def main():
print('hello')
await asyncio.sleep(1)
print('world')
await main()
-
,当您省去
-e JUPYTER_ENABLE_LAB=yes
时,会得到没有jupyter实验室的常规jupyter笔记本,并且会发生预期的结果.when you leave out the
-e JUPYTER_ENABLE_LAB=yes
, then you get a regular jupyter notebook without jupyter lab and the expected result happens.这不是不是重复的 ipywidgets小部件值保持不变或 Jupyter Interactive Widget无法正确执行,因为这些问题包括jupyter lab或asyncio
this is not a duplicate of ipywidgets widgets values not changing or Jupyter Interactive Widget not executing properly, because these questions nether include jupyter lab nor asyncio
推荐答案
实际上可以,但是jupyter会丢失打印输出.尝试以下代码:
Actually it works, but jupyter lose print output.Try this code:
from IPython.display import display import ipywidgets as widgets out = widgets.Output() import asyncio def wait_for_change(widget, value): future = asyncio.Future() def getvalue(change): # make the new value available future.set_result(change.new) widget.unobserve(getvalue, value) widget.observe(getvalue, value) return future from ipywidgets import IntSlider slider = IntSlider() # Now the key: the container is displayed (while empty) in the main thread async def f(): for i in range(10): out.append_stdout('did work %s'%i) x = await wait_for_change(slider, 'value') out.append_stdout('async function continued with value %s'%x) asyncio.ensure_future(f()) display(slider) display(out)
您可以在此处找到更多详细信息: https://github. com/jupyter-widgets/ipywidgets/issues/2567#issuecomment-535971252
You can find more details here: https://github.com/jupyter-widgets/ipywidgets/issues/2567#issuecomment-535971252
这篇关于如何在Jupyter Lab上使用“异步小部件"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!