jupyter-notebook - 如何在 jupyter lab 上使用异步小部件?

标签 jupyter-notebook python-asyncio jupyter-lab ipywidgets

如何在 jupyter 上使用异步小部件 实验室 ?

我正在尝试重现 the official Asynchronous Widgets-Example在 jupyter 实验室 ,但 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笔记本
  • 创建一个单元格并在下面输入代码
  • 运行单元
  • 移动 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
    

    预期结果

    单元格输出
    did work 0
    async function continued with value 1
    did work 1
    async function continued with value 2
    [...]
    

    实际输出

    第一个 did work 0 之后什么都没有

    笔记
  • 我说的是 jupyter 实验室 而不是普通的 jupyter 笔记本
  • 没有错误消息或任何东西。预期的输出不会发生
  • 最小的 asyncio 示例在 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 会丢失打印输出。
    试试这个代码:

    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-notebook - 如何在 jupyter lab 上使用异步小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58996358/

    相关文章:

    jupyter-notebook - 修复损坏的 Jupyter 笔记本/加载以前的版本?

    python - asyncio 中的循环时间与 time.monotonic 相同吗?

    python-3.x - 如何使用 asyncio 优雅地超时

    python - 无法从 Python 正确解码 JSON URL

    python - 无法将JupyterLab升级到最新版本

    python - 如何在 Python 中重新加载环境变量?

    python - set_axis() 获得参数 'axis' 的多个值

    pdf - 如何使用 nbconvert 和 PDFExporter() 将 jupyter notebook 导出为 pdf 的最小示例

    Python 程序在一台机器上非常快速地运行异步抓取任务,但在其他机器上运行速度非常快

    jupyter-notebook - jupyter 实验室中的自动单元格执行计时