简体   繁体   中英

Change progress bar text when completing 100%

In this example the total progress is 5 seconds (I'm building a model that can be malleable, that I can use any amount of total seconds that will work the same):

from rich.progress import Progress
import time
import sys

def main(timeout):
    start_time = time.time()

    penultimate_quarter = timeout-1
    with Progress() as progress:
        task1 = progress.add_task("[green]Processing...", total=100)
        while not progress.finished:
            progress.update(task1, advance=100/penultimate_quarter)
            time.sleep(1)
        time.sleep(1)
        progress.update(task1, description="[blue]Complete Task", advance=100)

    print("--- %s seconds ---" % (time.time() - start_time))

if __name__ == "__main__":
    main(5)

Output (same line):

在此处输入图像描述

But the output I wanted does not contain this 100% with the message Processing... :

在此处输入图像描述

I tried to find a way to replace the progress.finished with a method that I could put in how many quarters of value have already been passed and pause the looping, something like:

penultimate_quarter = timeout-1
while progress.total_advance <= (penultimate_quarter/timeout):
    # rest of code...

How can I make this correctly and professionally according to rich usage?

Modification 1: Object value defining total progress

Before:

total=100
advance=100/penultimate_quarter
advance=100

After:

total=timeout
advance=timeout/penultimate_quarter
advance=timeout

Modification 2: position and use of time.sleep

Before:

penultimate_quarter = timeout-1
with Progress() as progress:
    task1 = progress.add_task("[green]Processing...", total=100)
    while not progress.finished:
        progress.update(task1, advance=100/penultimate_quarter)
        time.sleep(1)
    time.sleep(1)
    progress.update(task1, description="[blue]Complete Task", advance=100)

After:

penultimate_quarter = timeout-1
with Progress() as progress:
    task1 = progress.add_task("[green]Processing...", total=timeout)
    time.sleep(1)
    while not progress.finished:
        time.sleep(1)
        progress.update(task1, advance=timeout/penultimate_quarter)
    progress.update(task1, description="[blue]Complete Task", advance=timeout)

Final Code:

from rich.progress import Progress
import time
import sys

def main(timeout):
    start_time = time.time()

    penultimate_quarter = timeout-1
    with Progress() as progress:
        task1 = progress.add_task("[green]Processing...", total=timeout)
        time.sleep(1)
        while not progress.finished:
            time.sleep(1)
            progress.update(task1, advance=timeout/penultimate_quarter)
        progress.update(task1, description="[blue]Complete Task", advance=timeout)

    print("--- %s seconds ---" % (time.time() - start_time))

if __name__ == "__main__":
    main(5)

Output (update same line):

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM