pythonprogress-bar

Finished enlighten progressbar is still on the screen


I've done an implementation with multiple counters using enlighten progress bar package in python.

Problem: In the following program, 'A - first bar' is still on the screen while 'B - first bar' is counting up.

Requirement: Once run_first_loop is finished execution, I want the progress bars to be removed from the screen. Thus finally, while 'B - first bar' is counting up, there should not be any other progress bars.

Thanks in advance!

import time
import enlighten


def run_first_loop(manager):
    ticks = manager.counter(total=100, desc='A - first bar', unit='ticks', leave=False)
    tocks = manager.counter(total=20, desc='A - second bar', unit='tocks', leave=False)

    for num in range(100):
        time.sleep(0.1)  # Simulate work
        print(num)
        ticks.update()
        if not num % 5:
            tocks.update()
    ticks.close()
    tocks.close()


def run_sec_loop(manager):
    new_ticks = manager.counter(total=100, desc='B - first bar', unit='ticks', leave=False)

    for num in range(100):
        time.sleep(0.1)  # Simulate work
        print(num)
        new_ticks.update()
    new_ticks.close()


manager = enlighten.get_manager()

run_first_loop(manager)
time.sleep(2) # I'm expecting "A-first bar" should be removed from the screen. But, it's not.
run_sec_loop(manager) # at this point, the previous bar is also there on the screen, which is a little annoying.
manager.stop()

Solution

  • You can put clear=True to .close() method to clear the bar upon closing:

    import time
    import enlighten
    
    
    def run_first_loop(manager):
        ticks = manager.counter(total=100, desc="A - first bar", unit="ticks", leave=False)
        tocks = manager.counter(total=20, desc="A - second bar", unit="tocks", leave=False)
    
        for num in range(100):
            time.sleep(0.1)  # Simulate work
            print(num)
            ticks.update()
            if not num % 5:
                tocks.update()
    
        ticks.close(clear=True)  # <-- put clear=True here
        tocks.close(clear=True)  # <-- put clear=True here
    
    
    def run_sec_loop(manager):
        new_ticks = manager.counter(
            total=100, desc="B - first bar", unit="ticks", leave=False
        )
    
        for num in range(100):
            time.sleep(0.1)  # Simulate work
            print(num)
            new_ticks.update()
        new_ticks.close()
    
    
    manager = enlighten.get_manager()
    
    run_first_loop(manager)
    time.sleep(
        2
    )  # I'm expecting "A-first bar" should be removed from the screen. But, it's not.
    run_sec_loop(
        manager
    )  # at this point, the previous bar is also there on the screen, which is a little annoying.
    manager.stop()