wot*_*nii 8 python-asyncio jupyter-notebook ipywidgets jupyter-lab
我正在尝试在 jupyter lab上重现官方的 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%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
Run Code Online (Sandbox Code Playgroud)
细胞输出
did work 0
async function continued with value 1
did work 1
async function continued with value 2
[...]
Run Code Online (Sandbox Code Playgroud)
第一次之后什么都没有 did work 0
我专门谈论 jupyter实验室,而不是普通的 jupyter 笔记本
没有错误消息或任何东西。预期的输出不会发生
最小的 asyncio-example 在 jupyter 实验室中确实有效:
did work 0
async function continued with value 1
did work 1
async function continued with value 2
[...]
Run Code Online (Sandbox Code Playgroud)
当您省略 时,您将-e JUPYTER_ENABLE_LAB=yes获得一个没有 jupyter lab 的普通 jupyter notebook,并且会出现预期的结果。
这是不是一个重复ipywidgets控件值不改变或Jupyter交互式控件没有正确地执行,因为这些问题幽冥包括jupyter实验室也不ASYNCIO
实际上它可以工作,但是 jupyter 丢失了打印输出。试试这个代码:
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)
Run Code Online (Sandbox Code Playgroud)
您可以在这里找到更多详细信息:https://github.com/jupyter-widgets/ipywidgets/issues/2567#issuecomment-535971252