Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing text below tqdm progress bar

Tags:

python

tqdm

I would like to display text in a ring-like buffer below a tqdm progress bar. This text would not necessarily be updated with every update of the bar. When using the .write() function of tqdm, text is only printed to the right of the bar, which is not desired. I'm not sure if this is possible with tqdm or not.

I would like it to look like this:

70%|███████   | 7/10 [00:00<00:00,  9.65it/s]
Message 2 ....
Message 3 ....
Message 4 ....
Message 5 ....

When a new Message is printed, Message 2 is deleted and the messages move up in the stack. I'm not tied to tqdm but I have been using it so far.

like image 346
pdice Avatar asked Aug 02 '17 15:08

pdice


1 Answers

You could use a second progress bar, where you only use the description.

import random
import time
from tqdm import tqdm

n_iter = 1000

with tqdm(total=n_iter, position=1, bar_format='{desc}', desc='No high number so far.') as desc:
    for i in tqdm(range(n_iter), total=n_iter, position=0):
        x = random.random()
        if x > 0.95:
            desc.set_description('High random number: %f' % x)
        time.sleep(0.1)
like image 66
Frank Zalkow Avatar answered Nov 19 '22 15:11

Frank Zalkow