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] Progress with multiple tasks and transient will delete written lines on stop() and start() #3121

Open
2 tasks done
charlydelta opened this issue Sep 3, 2023 · 1 comment

Comments

@charlydelta
Copy link

charlydelta commented Sep 3, 2023

Describe the bug

I have a Progress with multiple tasks. At some point, I want to have a user interaction by prompting. To not clutter the output, I made the Progress transient. The idea was to stop() the progress, query the user, and start() the progress again. If I do so, however, several lines are deleted from the output. I created a MWE to demonstrate this bug (ignore the function input_with_prefill for the beginning):

import time

from rich.console import Console
from rich.progress import Progress


def input_with_prefill(prompt="", prefill=""):
    """
    This is a function I use to offer the user a prompt with a prefill
    they can edit. I don't think it has something to do with this bug.
    """

    import readline

    def hook():
        readline.insert_text(prefill)
        readline.redisplay()

    readline.set_pre_input_hook(hook)
    # Write the prompt with prefil to the terminal.
    result = input(f"Line 3: {prompt}").strip()
    readline.set_pre_input_hook()

    return result


console = Console()

# Print two lines of info. This is important info for the user.
console.print("Line 1: Print some important info.")
console.print("Line 2: Print some more important info.")

with Progress(console=console, transient=True) as progress:
    # Do some work and analysize stuff...
    task_a = progress.add_task("Analyzing your fridge...")
    task_b = progress.add_task("Analyzing bottom fridge drawer...")
    task_c = progress.add_task("Analyzing left corner of drawer...")  # <-- [1] Try out-commenting.
    time.sleep(0.5)

    # We want to ask the user something, so hide the progress bars.
    progress.stop()

    # Ask the user.
    favorite_food = input_with_prefill(prompt="Favorite Food? ", prefill="Pizza")

    #progress.print()  # <-- [2] Try remove comment.
    #progress.print()  # <-- [2] Try remove comment.

    # Now that we have the answer we can show the progress bars again and keep processing
    progress.start()
    time.sleep(0.5)

    # Print the result of the analysis.
    progress.print(f"Line 4: Favorite food is {favorite_food}.")

Expected Behavior

I thought the output would look like this:

Line 1: Print some important info.
Line 2: Print some more important info.
Line 3: Favorite Food? Hamburger
Line 4: Favorite food is Hamburger.

Actual Behavior

Somehow, two lines are deleted:

Line 1: Print some important info.
Line 4: Favorite food is Hamburger.

Insights

  • It has to do with the number of tasks. If you out-comment one task (e.g., task_c, see [1]), it will only remove one line instead of two.
  • If you know the number of tasks, you can compensate by printing n-1 new lines (see [2]), with n = number of tasks.

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

Platform

Click to expand Platform Info
name@host  ~  lsb_release -a

No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 11 (bullseye)
Release:	11
Codename:	bullseye
name@host  ~  lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 11 (bullseye)
Release:	11
Codename:	bullseye
name@host  ~  uname -a
Linux phi-deianeira 5.10.0-25-amd64 #1 SMP Debian 5.10.191-1 (2023-08-16) x86_64 GNU/Linux
name@host  ~  python --version
Python 3.9.2
name@host  ~  zsh --version
zsh 5.8 (x86_64-debian-linux-gnu)

Output of python -m rich.diagnose

(venv)  chris@phi-deianeira  ~  python -m rich.diagnose
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=140 ColorSystem.TRUECOLOR>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'truecolor'                                                   │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 77                                                            │
│    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=140, height=77),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=140,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=77,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=140, height=77)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 140                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'xterm-256color',      │
│     '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"

Output of pip freeze | grep rich

name@host  ~  pip freeze | grep rich
rich==13.5.2
@github-actions
Copy link

github-actions bot commented Sep 3, 2023

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

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

No branches or pull requests

1 participant