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.

ipywidgets.Output() context suppresses exceptions

See original GitHub issue

ipywidgets.Output() context manager supresses exceptions and prevents correct exit code

Description

For example take this code snippet and save it to a cell of a *.ipynb notebook.

import ipywidgets
o = ipywidgets.Output()
with o:
    raise Exception()
display(o)
print("execution continues")

Then execute the notebook with ipython:

$ ipython path/to/notebook.ipynb

The output will be

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
/tmp/ipywidgets_bug/example.ipynb in <module>
      2 o = ipywidgets.Output()
      3 with o:
----> 4     raise Exception()
      5 display(o)
      6 print("execution continues")

Exception: 
Output()
execution continues

Expected behavior

The call to ipython should fail. If it is not aborted at the time when the exception is raised, at least it should exit with exit code other than 0.

Actual Behavior

The exception raised in the output context is suppressed. Execution continues and the ipython call exits with code 0.

Also the stderr output from the exception is not displayed unless display(o) is called explicitly. But this I reckon is expected behavior.

Context

  • ipywidgets version
    8.0.0a4
  • Operating System and version:
    archlinux kernel 5.12.7-arch1-1
  • Browser and version:
    not used
Troubleshoot Output
$PATH:
	/tmp/ipywidgets_bug/.venv/bin
	/usr/local/bin
	/usr/bin
	/usr/local/sbin
	/opt/cuda/bin
	/opt/cuda/nsight_compute
	/opt/cuda/nsight_systems/bin
	/usr/lib/jvm/default/bin
	/usr/bin/site_perl
	/usr/bin/vendor_perl
	/usr/bin/core_perl

sys.path: /tmp/ipywidgets_bug/.venv/bin /usr/lib/python39.zip /usr/lib/python3.9 /usr/lib/python3.9/lib-dynload /tmp/ipywidgets_bug/.venv/lib/python3.9/site-packages

sys.executable: /tmp/ipywidgets_bug/.venv/bin/python

sys.version: 3.9.5 (default, May 24 2021, 12:50:35) [GCC 11.1.0]

platform.platform(): Linux-5.12.7-arch1-1-x86_64-with-glibc2.33

which -a jupyter: /tmp/ipywidgets_bug/.venv/bin/jupyter /usr/bin/jupyter

pip list: Package Version ------------------ ------- attrs 21.2.0 backcall 0.2.0 decorator 5.0.9 ipykernel 5.5.5 ipython 7.24.1 ipython-genutils 0.2.0 ipywidgets 8.0.0a4 jedi 0.18.0 jsonschema 3.2.0 jupyter-client 6.1.12 jupyter-core 4.7.1 jupyterlab-widgets 2.0.0a1 matplotlib-inline 0.1.2 nbformat 5.1.3 parso 0.8.2 pexpect 4.8.0 pickleshare 0.7.5 pip 21.1.1 prompt-toolkit 3.0.18 ptyprocess 0.7.0 Pygments 2.9.0 pyrsistent 0.17.3 python-dateutil 2.8.1 pyzmq 22.1.0 setuptools 56.0.0 six 1.16.0 tornado 6.1 traitlets 5.0.5 wcwidth 0.2.5 widgetsnbextension 4.0.0a2

Command Line Output
[TerminalIPythonApp] IPYTHONDIR set to: /home/XXXX/.ipython
[TerminalIPythonApp] Using existing profile dir: '/home/XXXX/.ipython/profile_default'
[TerminalIPythonApp] Searching path ['/tmp/ipywidgets_bug', '/home/XXXX/.ipython/profile_default', '/tmp/ipywidgets_bug/.venv/etc/ipython', '/usr/local/etc/ipython', '/etc/ipython'] for config files
[TerminalIPythonApp] Attempting to load config file: ipython_config.py
[TerminalIPythonApp] Looking for ipython_config in /etc/ipython
[TerminalIPythonApp] Looking for ipython_config in /usr/local/etc/ipython
[TerminalIPythonApp] Looking for ipython_config in /tmp/ipywidgets_bug/.venv/etc/ipython
[TerminalIPythonApp] Looking for ipython_config in /home/XXXX/.ipython/profile_default
[TerminalIPythonApp] Looking for ipython_config in /tmp/ipywidgets_bug

[TerminalIPythonApp] Loading IPython extensions… [TerminalIPythonApp] Loading IPython extension: storemagic [TerminalIPythonApp] Running file in user namespace: /tmp/ipywidgets_bug/example.ipynb

Exception Traceback (most recent call last) /tmp/ipywidgets_bug/example.ipynb in <module> 2 o = ipywidgets.Output() 3 with o: ----> 4 raise Exception() 5 display(o) 6 print(“execution continues”)

Exception: Output() execution continues [TerminalIPythonApp] IPython not interactive…

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
vidartfcommented, Mar 17, 2022

Thanks for the PR @wookayin . As a placeholder, you can also create a new widget by inheritance (no action in ipywidgets repo needed):

class NocatchOutput(ipywidgets.Output):
    def __exit__(self, *args, **kwargs):
        super().__exit__(*args, **kwargs)
0reactions
wookayincommented, Mar 16, 2022

I submitted a PR #3417 that would fix this one. My proposal is to change the behavior on whether exceptions won’t be suppressed by default, but this would be up to core developers’ decision. Thank you for your considerations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Output widgets: leveraging Jupyter's display system - ipywidgets
After the widget is created, direct output to it using a context manager. ... widget') raise Exception('As does this') function_with_captured_output().
Read more >
Is there a way to suppress output of ipywidgets.interact ...
I am now having a problem with ipywidgets.interact, in that it is impossible to hide the output value (which is often None), even...
Read more >
contextlib - Useful Context Managers (with Statement) in Python
suppress() - This method accepts as many exception types that we want to suppress as input. It won't raise error and halt program...
Read more >
How can I more easily suppress previous exceptions when I ...
import logging try: import someProprietaryModule except Exception as e: if ... Silently suppressing exceptions is almost always an error and is most ...
Read more >
Interactive data analysis with figurewidget ipywidgets in Python
name | When used in a template, named items are created in the | output figure in ... Grid | color is lightened...
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