Output widget context manager prints to wrong cell (if used in a Thread)
See original GitHub issueIf 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:
- Created 4 years ago
- Comments:14 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.