[英]How can I use Asynchronous Widgets on jupyter lab?
I'm trying to reproduce the official Asynchronous Widgets-Example on jupyter lab , but the await
never continues.我试图在 jupyter lab上重现官方的异步小部件示例,但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
The cell outputs单元格输出
did work 0
async function continued with value 1
did work 1
async function continued with value 2
[...]
nothing after the first did work 0
第一个did work 0
I'm specifically talking about jupyter lab and not about regular jupyter notebooks我专门谈论的是 jupyter实验室,而不是常规的 jupyter 笔记本
There is no error-message or anything.没有错误消息或任何东西。 The expected output just doesn't happen预期的 output 只是没有发生
The minimal asyncio-example does work in jupyter lab:最小的 asyncio-example 确实在 jupyter 实验室中工作:
import asyncio
async def main():
print('hello')
await asyncio.sleep(1)
print('world')
await main()
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.当您省略-e JUPYTER_ENABLE_LAB=yes
时,您会得到一个没有 jupyter lab 的常规 jupyter notebook,并且会发生预期的结果。
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这不是ipywidgets 小部件值未更改或Jupyter Interactive Widget 未正确执行的重复,因为这些问题既包括 jupyter lab 也不 asyncio
Actually it works, but jupyter lose print output.实际上它有效,但 jupyter 丢失打印 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)
You can find more details here: https://github.com/jupyter-widgets/ipywidgets/issues/2567#issuecomment-535971252您可以在此处找到更多详细信息: https://github.com/jupyter-widgets/ipywidgets/issues/2567#issuecomment-535971252
I've had luck with jupyter-ui-poll to synchronize widget activity with the Jupyter Python kernal:我很幸运使用 jupyter-ui-poll 将小部件活动与 Jupyter Python 内核同步:
https://github.com/Kirill888/jupyter-ui-poll https://github.com/Kirill888/jupyter-ui-poll
In particular I used it here:特别是我在这里使用它:
https://github.com/AaronWatters/jp_doodle/blob/master/jp_doodle/auto_capture.py https://github.com/AaronWatters/jp_doodle/blob/master/jp_doodle/auto_capture.py
Works for me.为我工作。 Hope that helps!希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.