【发布时间】:2020-03-18 15:37:14
【问题描述】:
如何在 jupyter lab 上使用异步小部件?
我试图在 jupyter lab 上重现 the official Asynchronous Widgets-Example,但 await 永远不会继续。
设置/复制
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 笔记本
- 创建一个单元格并在下面输入代码
- 运行单元格
- 移动滑块
单元格代码
%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
预期结果
单元格输出
did work 0
async function continued with value 1
did work 1
async function continued with value 2
[...]
实际输出
在第一个 did work 0 之后什么都没有
注意事项
我说的是 jupyter lab,而不是常规的 jupyter notebooks
没有错误消息或任何东西。预期的输出没有发生
最小的 asyncio-example 确实在 jupyter 实验室中工作:
import asyncio
async def main():
print('hello')
await asyncio.sleep(1)
print('world')
await main()
当您省略
-e JUPYTER_ENABLE_LAB=yes时,您会得到一个没有 jupyter lab 的常规 jupyter notebook,并且会发生预期的结果。这不是与 ipywidgets widgets values not changing 或 Jupyter Interactive Widget not executing properly 的重复,因为这些问题既不包括 jupyter lab 也不包括 asyncio
【问题讨论】:
标签: jupyter-notebook python-asyncio jupyter-lab ipywidgets