Why does the Output context manager not show the desired output?

Greetings, as per the title I’m not seeing expected output in the Output widget when I use the context manager. If I use the append_stdout method instead, it does work (however that doesn’t seem to work for my use case, where I want to append a display() object, and append_display_data method seems not to be implemented yet).

My jupyter version:

>jupyter --version
Selected Jupyter core packages...
IPython          : 7.18.1
ipykernel        : 5.3.4
ipywidgets       : 7.7.0
jupyter_client   : 7.2.2
jupyter_core     : 4.10.0
jupyter_server   : not installed
jupyterlab       : 2.2.6
nbclient         : not installed
nbconvert        : 5.6.1
nbformat         : 5.0.7
notebook         : 6.1.1
qtconsole        : 5.3.0
traitlets        : 4.3.3

Attaching screenshot below rather than pasting code, as it includes execution times:

Thank you!

I would guess append_stdout() is method of out so that it isn’t compatible with appending to print-sourced things? However, then why does it work if you follow the append with print()?:

out = widgets.Output(layout={'border':'1px solid black'})
out.append_stdout("bar")
with out:
    for _ in range(10):
        print("foo")
display(out)

I’ve learned not to rely on print() in conjunction with ipywidgets, unless it is in a function wrapped by the decorator @out.capture(clear_output=True,wait=True), but it is clearly discussed as if viable in the documentation.


This works:

import ipywidgets as widgets
out = widgets.Output(layout={'border':'1px solid black'})
with out:
    for _ in range(10):
        out.append_stdout("foo\n")
out.append_stdout("bar")
display(out)

Somewhat based on here


For those looking to replicate, here’s some other code versions:

import ipywidgets as widgets
out = widgets.Output(layout={'border':'1px solid black'})
with out:
    for _ in range(10):
        print("foo")
out

And

import ipywidgets as widgets
out = widgets.Output(layout={'border':'1px solid black'})
with out:
    for _ in range(10):
        print("foo")
out.append_stdout("bar")
out

Right, basically this example from the documentation doesn’t work for me in the environment I pasted above:

with out:
    for i in range(10):
        print(i, 'Hello world!')

That same snippet does work fine on another machine with this environment:

>jupyter --version
jupyter core     : 4.6.3
jupyter-notebook : 6.0.3
qtconsole        : 4.3.1
ipython          : 6.4.0
ipykernel        : 4.8.2
jupyter client   : 6.1.5
jupyter lab      : 0.32.1
nbconvert        : 6.0.7
ipywidgets       : 7.2.1
nbformat         : 5.1.3
traitlets        : 4.3.2

For simplicity I’ve put the two setups side-by-side.

Works fine Doesn’t work
jupyter core 4.6.3 4.10.0
jupyter-notebook 6.0.3 6.1.1
qtconsole 4.3.1 5.3.0
ipython 6.4.0 7.18.1
ipykernel 4.8.2 5.3.4
jupyter client 6.1.5 7.2.2
jupyter lab 0.32.1 2.2.6
nbconvert 6.0.7 5.6.1
ipywidgets 7.2.1 7.7.0
nbformat 5.1.3 5.0.7
traitlets 4.3.2 4.3.3

Your environment where it doesn’t work seems to have a mix of super up-to-date, such as jupyter core & ipywidgets, and somewhat out of date things, such as IPython, ipykernel, & traitlets. For comparison a recent build and launch via MyBinder using a requirements.txt:

And a build that originated today:

I tried upgrading all the packages via $ pip install -U _packagename_ and it didn’t resolve the issue. New package list:

>jupyter --version
Selected Jupyter core packages...
IPython          : 7.32.0
ipykernel        : 6.13.0
ipywidgets       : 7.7.0
jupyter_client   : 7.2.2
jupyter_core     : 4.10.0
jupyter_server   : not installed
jupyterlab       : 2.2.6
nbclient         : 0.6.0
nbconvert        : 6.5.0
nbformat         : 5.3.0
notebook         : 6.4.11
qtconsole        : 5.3.0
traitlets        : 5.1.1

I was hoping one of those might fix it. It is odd. There are a number of open issues involving the output context manager at the ipywidgets issues page. However, I’m not seeing one that exactly matches yours at this time. If you file an issue there I would focus on that one in the documentation and show what you see vs the expected.

I’m running into something similar:

This works fine:
Screenshot from 2022-05-02 11-14-45

This doesn’t:

import ipywidgets as widgets
with widgets.Output():
    widgets.IntSlider()

Doesn’t display the widget. Assigning the output widget to a variable and doing a display(out) as well doesn’t work.

jupyter --version

Selected Jupyter core packages...
IPython          : 8.0.1
ipykernel        : 6.8.0
ipywidgets       : 7.6.5
jupyter_client   : 7.1.2
jupyter_core     : 4.9.1
jupyter_server   : 1.13.5
jupyterlab       : not installed
nbclient         : 0.5.10
nbconvert        : 6.4.1
nbformat         : 5.1.3
notebook         : 6.4.8
qtconsole        : not installed
traitlets        : 5.1.1

I’m trying to create a Voila Dashboard and was planing to use ipywidgets, but with this issue, I’m afraid that wouldn’t be possible.