Reputation: 35
For a large scan over a parameter space for a scientific calculation which takes several hours on my computer, I want to implement a progress bar (like from the tqdm
package). During the computations of the program I want the program to print where exactly it is on the grid over which it searches and some error messages using print()
. Using the standard code from tqdm
resulted in a bar in every some lines of code but not in a progress bar in the last line of the terminal as would be good for my application.
Here is a minimal version of what I mean:
from tqdm import tqdm
from time import sleep
for i in tqdm(range(10), position=0, leave=True):
print("\nAfter this comment there will be a new progress bar.")
sleep(0.5)
The code snippet yields an output like:
0%| | 0/10 [00:00<?, ?it/s]
After this comment there will be a new progress bar.
10%|████▍ | 1/10 [00:00<00:04, 2.00it/s]
After this comment there will be a new progress bar.
20%|████████▊ | 2/10 [00:01<00:04, 2.00it/s]
After this comment there will be a new progress bar.
30%|█████████████▏ | 3/10 [00:01<00:03, 2.00it/s]
After this comment there will be a new progress bar.
40%|█████████████████▌ | 4/10 [00:02<00:03, 1.99it/s]
After this comment there will be a new progress bar.
50%|██████████████████████ | 5/10 [00:02<00:02, 1.99it/s]
After this comment there will be a new progress bar.
60%|██████████████████████████▍ | 6/10 [00:03<00:02, 1.99it/s]
After this comment there will be a new progress bar.
70%|██████████████████████████████▊ | 7/10 [00:03<00:01, 1.99it/s]
After this comment there will be a new progress bar.
80%|███████████████████████████████████▏ | 8/10 [00:04<00:01, 1.99it/s]
After this comment there will be a new progress bar.
90%|███████████████████████████████████████▌ | 9/10 [00:04<00:00, 1.99it/s]
After this comment there will be a new progress bar.
100%|███████████████████████████████████████████| 10/10 [00:05<00:00, 1.99it/s]
Do you know of an alternative for tqdm, a quick implementation of a progress bar that remains below and just grows (together with the functionality that tqdm delivers) or maybe the right arguments I would just have to give tqdm?
Thank you!
Upvotes: 1
Views: 1759
Reputation: 2508
You could use Rich which allow you to print while displaying a progress bar.
Upvotes: 1
Reputation: 1794
It sounds like you're looking for the .write() method of tqdm.
That said, my solution here would be to log to a file if you really need text output and the progress bar.
Notice that with the defaults you know which iteration the error / output was printed without having to provide that in via a variable/exc_info or similar. I very rarely use tqdm.write() for this reason.
Upvotes: 1