Rockhopper-Technologies / enlighten

Enlighten Progress Bar for Python Console Apps
https://python-enlighten.readthedocs.io
Mozilla Public License 2.0
421 stars 25 forks source link

How to recreate a counter in the same position (i.e. how to destroy a counter as well as its position) #39

Closed yusanshi closed 3 years ago

yusanshi commented 3 years ago

Is your feature request related to a problem? Please describe. Sometimes I want to create several counters in the same specified position, but it will raise Counter position {} is already occupied. error. So I would like to know how can I release a counter and then create another in the same position.

An example:

import enlighten
import numpy as np

batches = np.random.uniform(size=(20, 20))

def train(batch):
    pass

manager = enlighten.get_manager()

for epoch in range(10):
    pbar = manager.counter(total=len(batches), position=0)
    for batch in pbar(batches):
        train(batch)

Describe the solution you'd like A function to destroy a counter as well as its position.

Describe alternatives you've considered A function to reset a counter (reset its escape time and current count value).

avylove commented 3 years ago

I have been thinking about adding a replace() method to make transitions like this easier. However, I think for your use case, you can add leave=False and then close the counter when you're finished. You may also find using position 1 will give you better results than position 0.

import time

import enlighten
import numpy as np

batches = np.random.uniform(size=(20, 20))

def train(batch):
    time.sleep(0.1)

manager = enlighten.get_manager()

for epoch in range(10):
    pbar = manager.counter(desc=str(epoch), total=len(batches), position=1, leave=False)
    for batch in pbar(batches):
        train(batch)
    pbar.close()
avylove commented 3 years ago

Additionally, in this use case, you'll find you don't need to specify the position at all, though perhaps in your project you do. You can also close implicitly using a context manager:

for epoch in range(10):
    with manager.counter(desc=str(epoch), total=len(batches), leave=False) as pbar:
        for batch in pbar(batches):
            train(batch)
yusanshi commented 3 years ago

Thanks for your timely reply. leave=False does the trick.