Search code examples
pythonprogress-barrich

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):

enter image description here

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

enter image description here

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?


Solution

  • 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):

    enter image description here