There is such a thing as "too much TQDM"
Double Progress Bar in Python - Stack Overflow
[tqdm.rich] latest rich breaks the example
python - rich progress bar not showing correct results with sockets file sharing - Stack Overflow
TIL that 20% of the runtime of my program was being dedicated to making cute little loading bars with fancy colors and emojis.
Turns out loops in Python are not that efficient, and I was putting loops where none were needed just to get nice loading bars.
Use the nested progress bars feature of tqdm, an extremely low overhead, very customisable progress bar library:
$ pip install -U tqdm
Then:
from tqdm import tqdm
# from tqdm.auto import tqdm # notebook compatible
import time
for i1 in tqdm(range(5)):
for i2 in tqdm(range(300), leave=False):
# do something, e.g. sleep
time.sleep(0.01)
(The leave=False is optional - needed to discard the nested bars upon completion.)
You can also use from tqdm import trange and then replace tqdm(range(...)) with trange(...). You can also get it working in a notebook.
Alternatively if you want just one bar to monitor everything, you can use tqdm's version of itertools.product:
from tqdm.contrib import itertools
import time
for i1, i2 in itertools.product(range(5), range(300)):
# do something, e.g. sleep
time.sleep(0.01)
I basically just want to add to the answer of @casper.dcl. In the slightly different case, where you have two nested for loops and want just a SINGLE progress bar you can do the following.
from tqdm import tqdm
import time
n = 5
m = 300
with tqdm(total=n * m) as pbar:
for i1 in tqdm(range(n)):
for i2 in tqdm(range(m)):
# do something, e.g. sleep
time.sleep(0.01)
pbar.update(1)
I know that was not the question, but it might be still helpful for some folks.