Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Rich traceback not working proprely within notebook #2271

Closed
samsja opened this issue May 11, 2022 · 16 comments · Fixed by #2754
Closed

[BUG] Rich traceback not working proprely within notebook #2271

samsja opened this issue May 11, 2022 · 16 comments · Fixed by #2754

Comments

@samsja
Copy link

samsja commented May 11, 2022

Describe the bug

When the rich traceback is enabled in a notebook, the traceback is almost unreadable and show a criptic message:

tmp/ipykernel_1565558/3782956317.py:1 in <cell line: 1>                                  │                                                                                          
 [Errno 2] No such file or directory: '/tmp/ipykernel_1565558/3782956317.py'

Here is a screenshot that showcase the issue with a very simple traceback.
Screenshot from 2022-05-11 13-56-59

One could say that there is no point of activating the trackback from inside a notebook. However the problem persist when importing a library that enable rich traceback. (Ex in my case docarray or jina)

Screenshot from 2022-05-11 13-31-58

thanks in advance for any support 😃 !!

Platform

Click to expand

Linux, running from jupyter notebook

from rich.diagnose import report
report()
╭────────────────────── <class 'rich.console.Console'> ──────────────────────╮
│ A high level console interface.                                            │
│                                                                            │
│ ╭────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=93 ColorSystem.TRUECOLOR>                               │ │
│ ╰────────────────────────────────────────────────────────────────────────╯ │
│                                                                            │
│     color_system = 'truecolor'                                             │
│         encoding = 'utf-8'                                                 │
│             file = <ipykernel.iostream.OutStream object at 0x7f26f700bcd0> │
│           height = 100                                                     │
│    is_alt_screen = False                                                   │
│ is_dumb_terminal = False                                                   │
│   is_interactive = False                                                   │
│       is_jupyter = True                                                    │
│      is_terminal = False                                                   │
│   legacy_windows = False                                                   │
│         no_color = False                                                   │
│          options = ConsoleOptions(                                         │
│                        size=ConsoleDimensions(width=93, height=100),       │
│                        legacy_windows=False,                               │
│                        min_width=1,                                        │
│                        max_width=93,                                       │
│                        is_terminal=False,                                  │
│                        encoding='utf-8',                                   │
│                        max_height=100,                                     │
│                        justify=None,                                       │
│                        overflow=None,                                      │
│                        no_wrap=False,                                      │
│                        highlight=None,                                     │
│                        markup=None,                                        │
│                        height=None                                         │
│                    )                                                       │
│            quiet = False                                                   │
│           record = False                                                   │
│         safe_box = True                                                    │
│             size = ConsoleDimensions(width=93, height=100)                 │
│        soft_wrap = False                                                   │
│           stderr = False                                                   │
│            style = None                                                    │
│         tab_size = 8                                                       │
│            width = 93                                                      │
╰────────────────────────────────────────────────────────────────────────────╯

╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯

╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'xterm-color',         │
│     'COLORTERM': None,             │
│     'CLICOLOR': '1',               │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JPY_PARENT_PID': '1483554',   │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯

platform="Linux"
@artemisart
Copy link

Hello, we have the same issue, has anyone found a way to avoid this?
I tried with rich 12.6.0, 11.2.0, and 10.16.2 and still get the same problem (No such file or directory: /tmp/... for each line of the traceback wich comes from the notebook) and I'm pretty sure it worked at some point before upgrading but I didn't manage to get it working again.
Thanks!

@willmcgugan
Copy link
Collaborator

It appears as though by the time the traceback is displayed, Jupyter has removed the temporary file it created /tmp/.

There's been no relevant changes in Rich, so it looks to me like Jupyter has changed behaviour here. I think the best fix may be for Jupyter to ensure that the code isn't removed from the filesystem until after the traceback handlers is run.

There may be a workaround, which I'm considering. And that would be to snapshot the code at the time of the exception. But that is not without other potential issues.

@samsja
Copy link
Author

samsja commented Jan 12, 2023

Yeah indeed it looks like a Jupyter problem. Unfortunately as jupyter lab and notebook are widely adopted especially in ML I tend not to use the rich traceback anymore because it will break in notebook.

We had to remove from DocArray because of this.

If they were any solution even hacky one we would probably bring back the rich traceback

@willmcgugan
Copy link
Collaborator

There doesn't appear to be any way to workaround this in Rich. The paths reported in the traceback don't exist, so there is no way to get the relevant code.

I've reported this issue to the Jupyterlab repo.

@maartenbreddels
Copy link

IIRC instead of using the files directly, Rich could/should use the linecache module. This is also where IPython and/or ipykernel writes to.
I onced patched Rich to use that, but I lost the changes.

@maartenbreddels
Copy link

An excellent example on how to use linecache is the traceback module in the CPython distribution.

@willmcgugan
Copy link
Collaborator

Not sure that would help, since Rich needs the entire file to do Syntax highlighting.

@maartenbreddels
Copy link

each notebook cell is written to a 'file', which may not exist on disk, but is in the linecache module, see
https://github.com/ipython/ipython/blob/47abb68a6ad12539a161d03cd5a7a751cade9044/IPython/core/compilerop.py#L177

where it gets written to

@maartenbreddels
Copy link

so, linecache.getlines(filename) should get you the file lines you need.

@willmcgugan
Copy link
Collaborator

Ah, well that explains a lot. Thanks.

I can use that to workaround the issue. Although I'm not keen on the implementation in ipython. The cache attribute is not documented or exported in __all__.

@maartenbreddels
Copy link

I agree, but seeing it being used in traceback.py and ipython, I thought this was probably poured in concrete a long time ago.

@github-actions
Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

@willmcgugan
Copy link
Collaborator

Thanks for the explanation @maartenbreddels

Tracebacks are fixed in 13.1.0 (just released).

@samsja
Copy link
Author

samsja commented Jan 14, 2023

great news thanks !

@maartenbreddels
Copy link

Great, fix looks nice, glad I could help!

@loftusa
Copy link

loftusa commented Feb 9, 2023

Just popping on here to say that this issue was killing me for a few days... I had no idea this package existed, my jupyter just randomly broke.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants