如何在 jupyter lab 上使用异步小部件?
How can I use Asynchronous Widgets on jupyter lab?
如何在 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
没有错误消息或任何内容。预期的输出并没有发生
最小的异步示例在 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 or Jupyter Interactive Widget not executing properly 的副本,因为这些问题既不包括 jupyter lab 也不包括 asyncio
我很幸运使用 jupyter-ui-poll 将小部件 activity 与 Jupyter Python 内核同步:
https://github.com/Kirill888/jupyter-ui-poll
特别是我在这里使用它:
https://github.com/AaronWatters/jp_doodle/blob/master/jp_doodle/auto_capture.py
适合我。
希望对您有所帮助!
实际上它可以工作,但是 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)
您可以在此处找到更多详细信息:https://github.com/jupyter-widgets/ipywidgets/issues/2567#issuecomment-535971252
如何在 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
没有错误消息或任何内容。预期的输出并没有发生
最小的异步示例在 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 or Jupyter Interactive Widget not executing properly 的副本,因为这些问题既不包括 jupyter lab 也不包括 asyncio
我很幸运使用 jupyter-ui-poll 将小部件 activity 与 Jupyter Python 内核同步:
https://github.com/Kirill888/jupyter-ui-poll
特别是我在这里使用它:
https://github.com/AaronWatters/jp_doodle/blob/master/jp_doodle/auto_capture.py
适合我。 希望对您有所帮助!
实际上它可以工作,但是 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)
您可以在此处找到更多详细信息:https://github.com/jupyter-widgets/ipywidgets/issues/2567#issuecomment-535971252