question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Output widget context manager prints to wrong cell (if used in a Thread)

See original GitHub issue

If I wrap this code from source/docs/examples/Output Widget.ipynb,

import ipywidgets as widgets
out = widgets.Output(layout={'border': '1px solid black'})
with out:
    for i in range(10):
        print(i, 'Hello world!')

in a Thread,

import ipywidgets as widgets
out = widgets.Output(layout={'border': '1px solid black'})
def foo():
    with out:
        for i in range(10):
            print(i,'Hello world!')
IPython.display.display(out)
threading.Thread(target=foo).start()

then the output ends up in a wrong cell as soon as I execute another cell.

I would consider this a bug, as I cannot imagine anyone wanting the Output widget to change cells. To convince you of the awesomeness of updating the Output widget in the cell where it was started, imagine how easy it would be to make a simple

%%run_cell_in_background
for i in range(10):
        print(i, 'Hello world!')

out of this (indeed, that’s what I did, and it works beautifully except for the problem above. In my application, I need a simple job monitor, but I am sure there would be dozens of different uses for this)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
maartenbreddelscommented, May 21, 2019

It wouldn’t be such a bad idea to refactor the output widget. We could have the same logic in the frontend and backend (so that the Output widget behaves the same without a front-end connected). And if we send the outputs using a custom msg, we could send the msg_id with it. It is tricky work, and requires ipywidgets, the labextension, the notebook extension, nbconvert and voila to all be in sync.

1reaction
jasongroutcommented, May 21, 2019

Are there plans to improve that situation? Or is it something that’s too difficult to address without completely uprooting things

I don’t know of anyone working on this in IPython itself (though that repo would be the place to ask). @SylvainCorlay and some others from quantstack are experimenting with a new python kernel based on the C++ backend xeus that experiments with other concurrency models.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Output widgets: leveraging Jupyter's display system - IPyWidgets
This always prints in the currently active cell, not the cell that started the background thread. This can lead to surprising behaviour in...
Read more >
python - Jupyter Output widget not clearing - Stack Overflow
I do use clear_output but it does not seem to be helping. A full minimal snippet demonstrating the issue can be found below:...
Read more >
Python Context Manager - Cambridge Spark
In this tutorial, you'll learn what the Python context manager is, how to use it to properly manage resources, and how to create...
Read more >
PySimpleGUI
It is 2 lines of Python code to create and start a thread. ... It's nice because you can use a GUI to...
Read more >
API — Dask.distributed 2022.12.1 documentation
Thread -local, Task-local context manager that causes the Client.current ... If msgpack is used (default), the output will be stored in a gzipped...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found