Skip to content

Async ipywidgets.Output updates from background task only render after next cell in JupyterLab #4012

@decitre

Description

@decitre

Description

Widget updates from the async consumer task appear to be buffered until another cell finishes.

This happens in JupyterLab, but not in JupyterLite (Pyodide/Xeus), where the same pattern works as expected.

Minimal notebook repro

import asyncio
import ipywidgets as W
from IPython.display import display

q = asyncio.Queue()
out = W.Output()
button = W.Button(description="Click me")

def on_click(_):
    q.put_nowait("click")

button.on_click(on_click)

async def consumer():
    counter = 0
    while True:
        item = await q.get()
        counter += 1
        with out:
            out.clear_output(wait=True)
            print(f"GOT click {counter}")

loop = asyncio.get_event_loop()
task = loop.create_task(consumer())

display(button, out)

Observed behavior

  1. Run the cell above.
  2. Click the "Click me" button several times.
  3. Notice that no "GOT click N" output appears in the Output widget.
  4. Execute a next cell with any valid Python
  5. Notice that only then does the previous cell's output update and show:
    GOT click 1
    

So widget updates from the async consumer task appear to be buffered until another cell finishes.

Expected behavior

Each button click should immediately cause the consumer task to update the Output widget, and the new content should be visible right away in JupyterLab, just like it is in JupyterLite (same pattern works there).

Context

  • Python: 3.11.9
  • ipywidgets: 8.1.8
  • jupyterlab_widgets: 3.0.16
  • JupyterLab: 4.5.1
  • ipykernel: 7.1.0
  • Labextensions:
    • jupyterlab_pygments v0.3.0 enabled OK (python, jupyterlab_pygments)
    • jupyter-leaflet v0.20.0 enabled OK
    • jupyterlab-filesystem-access v0.7.0 enabled OK (python, jupyterlab-filesystem-access)
    • @jupyter-widgets/jupyterlab-manager v5.0.15 enabled OK (python, jupyterlab_widgets)
    • @jupyterlite/pyodide-kernel-extension v0.7.0 enabled OK (python, jupyterlite_pyodide_kernel)
    • @jupyterlite/xeus-extension v4.3.7 enabled OK (python, jupyterlite_xeus)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions