rich icon indicating copy to clipboard operation
rich copied to clipboard

[BUG] Progress tasks break when run as a script inside jupyter notebooks

Open NeilBotelho opened this issue 2 years ago • 3 comments

  • [x] I've checked docs and closed issues for possible solutions.
  • [x] I can't find my issue in the FAQ.

Describe the bug If you run a script that uses a rich progress bar from a jupyter notebook it prints a line for every update to the progress bar. This only happens if the progress bar has multiple tasks and starts occuring from the time the second task starts. Running !python -m rich.diagnose shows is_jupyter=False. Setting rich.console.Console to force_jupyter=True results in a RecursionError.

I've recreated the bug on this gist

Platform

Click to expand

Linux, Python 3.11.5

Jupyter Versions:
IPython          : 8.17.2 
ipykernel        : 6.26.0 
ipywidgets       : 8.1.1  
jupyter_client   : 8.5.0  
jupyter_core     : 5.5.0  
jupyter_server   : 2.9.1  
jupyterlab       : 4.0.8  
nbclient         : 0.8.0  
nbconvert        : 7.10.0  
nbformat         : 5.9.2  
notebook         : 7.0.6  
qtconsole        : 5.4.4  
traitlets        : 5.13.0
rich.diagnose from terminal
╭───────────────────── <class 'rich.console.Console'> ─────────────────────╮
│ A high level console interface.                                          │
│                                                                          │
│ ╭──────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=76 ColorSystem.TRUECOLOR>                             │ │
│ ╰──────────────────────────────────────────────────────────────────────╯ │
│                                                                          │
│     color_system = 'truecolor'                                           │
│         encoding = 'utf-8'                                               │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w'           │
│                    encoding='utf-8'>                                     │
│           height = 49                                                    │
│    is_alt_screen = False                                                 │
│ is_dumb_terminal = False                                                 │
│   is_interactive = True                                                  │
│       is_jupyter = False                                                 │
│      is_terminal = True                                                  │
│   legacy_windows = False                                                 │
│         no_color = False                                                 │
│          options = ConsoleOptions(                                       │
│                        size=ConsoleDimensions(width=76, height=49),      │
│                        legacy_windows=False,                             │
│                        min_width=1,                                      │
│                        max_width=76,                                     │
│                        is_terminal=True,                                 │
│                        encoding='utf-8',                                 │
│                        max_height=49,                                    │
│                        justify=None,                                     │
│                        overflow=None,                                    │
│                        no_wrap=False,                                    │
│                        highlight=None,                                   │
│                        markup=None,                                      │
│                        height=None                                       │
│                    )                                                     │
│            quiet = False                                                 │
│           record = False                                                 │
│         safe_box = True                                                  │
│             size = ConsoleDimensions(width=76, height=49)                │
│        soft_wrap = False                                                 │
│           stderr = False                                                 │
│            style = None                                                  │
│         tab_size = 8                                                     │
│            width = 76                                                    │
╰──────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'alacritty',           │
│     'COLORTERM': 'truecolor',      │
│     'CLICOLOR': None,              │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Linux"
rich==13.5.2
rich.diagnose from jupyter
╭────────────────────── <class 'rich.console.Console'> ──────────────────────╮
│ A high level console interface.                                            │
│                                                                            │
│ ╭────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=115 ColorSystem.TRUECOLOR>                              │ │
│ ╰────────────────────────────────────────────────────────────────────────╯ │
│                                                                            │
│     color_system = 'truecolor'                                             │
│         encoding = 'utf-8'                                                 │
│             file = <ipykernel.iostream.OutStream object at 0x7f7a8677a950> │
│           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=115, height=100),      │
│                        legacy_windows=False,                               │
│                        min_width=1,                                        │
│                        max_width=115,                                      │
│                        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=115, height=100)                │
│        soft_wrap = False                                                   │
│           stderr = False                                                   │
│            style = None                                                    │
│         tab_size = 8                                                       │
│            width = 115                                                     │
╰────────────────────────────────────────────────────────────────────────────╯

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

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

platform="Linux"


NeilBotelho avatar Nov 03 '23 18:11 NeilBotelho

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Nov 03 '23 18:11 github-actions[bot]

It seems like this is caused by the fact that when you run a python script in jupyter like !python script.py it starts a separate python session without any of the imports or objects from the current session.

Rich uses get_ipython to test whether or not it is currently in a jupyter notebook. This is a method of InteractiveShell defined in the ipython library and is automatically loaded into the environment in ipython and jupyter sessions.

It seems to me as though, to print the progress bar output without the progress bar breaking, we need access to the global InteractiveShell instance of the jupyter notebook, which we wouldn't be able to get since we are running in a separate session

NeilBotelho avatar Nov 04 '23 11:11 NeilBotelho

What I'm unsure of is why this works with a single task but breaks for mutliple, even when they are sequential and the progress bars are transient.

NeilBotelho avatar Nov 04 '23 12:11 NeilBotelho