Invisible Progress tasks not transient. [BUG]
See original GitHub issueDescribe the bug
I have a function which has a flag to indicate whether a progress bar should be shown or not. By using the visible
task kwarg I can effectively hide the progress bar while keeping the rest of the iteration code the same.
However, when the progress bar is hidden, an empty line of output is created when it completes. I thought that setting Progress(transient=True)
might suppress the extraneous new line. In my code I use Progress(transient = not show_progress)
but set it to always be True
here for demonstration purposes.
When Progress(transient=True)
and visible=True
in the task, the progress bar displayed and then correctly erased upon completion and there is no extra empty line. However, when Progress(transient=True)
and visible=False
an empty line still appears.
Contents of bug.py
:
import sys
import time
from rich.progress import Progress
def scan(show_progress: bool = False) -> None:
total = 100
with Progress(transient=True) as progress:
scan_task = progress.add_task(
"scanning items", total=total, visible=show_progress
)
for i in range(total):
progress.update(scan_task, advance=1)
time.sleep(0.01)
if __name__ == "__main__":
show_progress = bool(int(sys.argv[1]))
scan(show_progress=show_progress)
Output when showing progress, where displayed progress bar is erased:
$ python bug.py 1
$
Output when suppressing progress with extra new line:
$ python bug.py 0
$
Is this behavior expected? Is there a better way to easily let the caller enable/disable progress bars that doesnโt use an if/else where one branch uses the Progress context manager and the other doesnโt with otherwise duplicated iteration code? Open to suggestions.
Platform
Click to expand
โญโโโโโโโโโโโโโโโโโโโโโโโโโ <class 'rich.console.Console'> โโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ A high level console interface. โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ <console width=167 ColorSystem.TRUECOLOR> โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ color_system = 'truecolor' โ
โ encoding = 'utf-8' โ
โ file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> โ
โ height = 16 โ
โ 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=167, height=16), โ
โ legacy_windows=False, โ
โ min_width=1, โ
โ max_width=167, โ
โ is_terminal=True, โ
โ encoding='utf-8', โ
โ max_height=16, โ
โ justify=None, โ
โ overflow=None, โ
โ no_wrap=False, โ
โ highlight=None, โ
โ markup=None, โ
โ height=None โ
โ ) โ
โ quiet = False โ
โ record = False โ
โ safe_box = True โ
โ size = ConsoleDimensions(width=167, height=16) โ
โ soft_wrap = False โ
โ stderr = False โ
โ style = None โ
โ tab_size = 8 โ
โ width = 167 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โญโโโ <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': 'vscode', โ
โ 'COLUMNS': None, โ
โ 'LINES': None, โ
โ 'JPY_PARENT_PID': None, โ
โ 'VSCODE_VERBOSE_LOGGING': None โ
โ } โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
platform="Linux"
rich @ file:///home/vscode/.cache/pypoetry/artifacts/75/b2/7e/cbc8a075a6318a98ac06848a183696936d11430bd373be488dc0ef50ce/rich-12.4.4-py3-none-any.whl
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:6
Using
Progress(disable=not show_progress)
solved my issue and I can now get the desired effect without usingvisible=True
! Thank you! ๐ ๐I donโt know if the different behavior for
transient
with respect to visible/not visible progress bars is still an issue worth investigating for you or not.Itt might be worth highlighting the existence of the
disable
flag in the docs here. Iโm happy to submit a PR in the documentation if you would like.Thanks again
Did I solve your problem?
Why not buy the devs a coffee to say thanks?