'tqdm with logger on separate lines

I looked at Change logging "print" function to "tqdm.write" so logging doesn't interfere with progress bars and Python Progress Bar THROUGH Logging Module and they don't have what I want.

I want to have logging messages and tqdm progress bars on different, separate lines.

I tried the answer from https://stackoverflow.com/a/38739634/10732321 which gives me the following output:

 50%|█████     | 50/100 [00:05<00:05,  9.65it/s]Half-way there!
100%|██████████| 100/100 [00:10<00:00,  9.71it/s]

Sure the progress bar and logging message is mutually exclusive, but I want the Half-way there! on the next line, i.e.

 50%|█████     | 50/100 [00:05<00:05,  9.65it/s]
Half-way there!
100%|██████████| 100/100 [00:10<00:00,  9.71it/s]

How do to this? Thanks!



Solution 1:[1]

I actually prefer to avoid tqdm and just do this. You can add your own flavour and variations to this.

import time


n = 25
for i in range(n):
    time.sleep(0.1)
    progress = int(i / n * 50)
    print(f'running {i+1} of {n} {progress*"."}', end='\r', flush=True)
    if i == int(n/2):
        print('\n >>> half way there')
    if i+1 == n:
        print('\nfinished process !!')

The output looks like this:

running 13 of 25 ........................
 >>> half way there
running 25 of 25 ................................................
finished process !!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 D.L