Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
49.45k stars 1.72k forks source link

[BUG] rich.progress.track with stderr Console breaks print builtin #3490

Closed ericfrederich closed 1 month ago

ericfrederich commented 1 month ago

Describe the bug

While using rich.progress.track configured with a Console instantiated with stderr=True, print statements also go to stderr.

I would expect rich to not hijack the default behavior of print.

I understand I myself can use console.print() in my own code but I have no control over 3rd party plugins or libraries.

Provide a minimal code example that demonstrates the issue if you can. If the issue is visual in nature, consider posting a screenshot.

#!/usr/bin/env python

from rich.console import Console
from rich.progress import track

err_console = Console(stderr=True)
console = Console()

if __name__ == "__main__":
    for i in track(range(10), console=err_console):
        if i % 4 == 0:
            print(f"foo {i}")

    for i in range(10, 20):
        if i % 4 == 0:
            print(f"foo {i}")

image

Platform

Click to expand > What platform (Win/Linux/Mac) are you running on? What terminal software are you using? > > I may ask you to copy and paste the output of the following commands. It may save some time if you do it now. > > > If you're using Rich in a terminal: > > ``` > python -m rich.diagnose > pip freeze | grep rich > ``` Linux VM via WSL2. ```text ╰─▪ python -m rich.diagnose ╭─────────────────────── ───────────────────────╮ │ A high level console interface. │ │ │ │ ╭──────────────────────────────────────────────────────────────────────────╮ │ │ │ │ │ │ ╰──────────────────────────────────────────────────────────────────────────╯ │ │ │ │ color_system = '256' │ │ encoding = 'utf-8' │ │ file = <_io.TextIOWrapper name='' mode='w' │ │ encoding='utf-8'> │ │ height = 41 │ │ 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=80, height=41), │ │ legacy_windows=False, │ │ min_width=1, │ │ max_width=80, │ │ is_terminal=True, │ │ encoding='utf-8', │ │ max_height=41, │ │ justify=None, │ │ overflow=None, │ │ no_wrap=False, │ │ highlight=None, │ │ markup=None, │ │ height=None │ │ ) │ │ quiet = False │ │ record = False │ │ safe_box = True │ │ size = ConsoleDimensions(width=80, height=41) │ │ soft_wrap = False │ │ stderr = False │ │ style = None │ │ tab_size = 8 │ │ width = 80 │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─── ────╮ │ Windows features available. │ │ │ │ ╭───────────────────────────────────────────────────╮ │ │ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │ │ ╰───────────────────────────────────────────────────╯ │ │ │ │ truecolor = False │ │ vt = False │ ╰───────────────────────────────────────────────────────╯ ╭────── Environment Variables ───────╮ │ { │ │ 'TERM': 'xterm-256color', │ │ 'COLORTERM': None, │ │ '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" ```
github-actions[bot] commented 1 month ago

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

willmcgugan commented 1 month ago

The magic of having a progress bar stuck to the bottom of the screen may well break some of your expectations. You may well have to compromise, unless you can propose how to accomplish this.

github-actions[bot] commented 1 month ago

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual