-
|
I'm processing entities inside of rich.Progress.track(), and some of them are invalid. Is there a way to decrease total when such entity happens? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Yes — you can update the from rich.progress import Progress
entities = ["valid", "invalid", "valid", "invalid", "valid"]
with Progress() as progress:
task = progress.add_task("Processing...", total=len(entities))
for entity in entities:
if entity == "invalid":
# Decrease total for invalid entities
progress.update(task, total=progress.tasks[task].total - 1)
continue
# Process valid entity
progress.update(task, advance=1)The progress bar will correctly show e.g. "3/3" instead of "3/5" after skipping 2 invalid entities. If you still want to use from rich.progress import Progress
entities = list(range(100))
with Progress() as progress:
task_id = progress.add_task("Processing", total=len(entities))
for entity in entities:
if is_invalid(entity):
progress.update(task_id, total=progress.tasks[task_id].total - 1)
continue
process(entity)
progress.update(task_id, advance=1)Both |
Beta Was this translation helpful? Give feedback.
Yes — you can update the
totalof a running task viaprogress.update(). Thetrack()helper doesn't expose this directly, but you can switch to the manual API which gives you full control:The progress bar will correctly show e…