TQDM#
This page focuses on features related to working with the tqdm
Python library. This is a library that allows you to create terminal progress bars in your python code. See the official documentation for more details.
import tqdm
from random import randint, seed
from IPython.display import clear_output
tqdm object#
The tqdm.tqdm
function returns a tqdm.std.tqdm
object, which is iterable. Each next
call on this object will update the progress line.
The following cell shows type of the object returned by tqdm.tqdm
.
bar = tqdm.tqdm([1,2,3,4])
bar.close()
clear_output()
display(type(bar))
tqdm.std.tqdm
So, as with any other iterabe, you can manually iterate over it using the next
function.
bar = tqdm.tqdm([1,2,3,4])
bar = iter(bar)
next(bar)
next(bar)
bar.close()
25%|██▌ | 1/4 [00:00<00:00, 4660.34it/s]
You can also use the update
method to manually advance the progress bar.
bar = tqdm.tqdm(total=100, ncols=100)
bar.update(10)
bar.update(20)
bar.close()
30%|█████████████████▍ | 30/100 [00:00<00:00, 408536.10it/s]
Total#
In cases where tqdm
cannot determine the length of the sequence it is iterating through, it will not display the percentage of completed iterations. This is typical for generators. Instead, it will only show the number of iterations completed. To address this, you can specify the total
argument when using tqdm.tqdm
.
As an example, consider the generator specified in the following line:
def my_generator():
for i in range(10):
yield
Here is a typical output for such a case:
for i in tqdm.tqdm(my_generator()):
pass
10it [00:00, 170500.16it/s]
No information about how many iterations are left. The following cell fixes this problem by simply specifying the total=10
argument.
for i in tqdm.tqdm(my_generator(), total=10):
pass
100%|██████████| 10/10 [00:00<00:00, 196915.68it/s]
Output stream#
You can use the file
argument to set the output stream for the progress bar.
The following cell introduces code that uses simple output file as arguemnt file
. And creates process bar with 100 stems.
with open("test", "w") as f:
for i in tqdm.tqdm(range(100), file=f):
pass
bar.update(10)
bar.close()
Now lets check gotten file:
%%bash
cat test
rm test
100%|██████████| 100/100 [00:00<00:00, 2304562.64it/s]
Position#
The position
argument specifies the line where the progress bar will be displayed.
The following example shows code that creates tqdm
bars, each with a random position.
seed(17)
ans = [
tqdm.tqdm(position=i, total=10).update(10)
for i in range(5) if randint(0,1)
]
100%|██████████| 10/10 [00:00<00:00, 241051.95it/s]
100%|██████████| 10/10 [00:00<00:00, 285326.80it/s]
100%|██████████| 10/10 [00:00<00:00, 272357.40it/s]
100%|██████████| 10/10 [00:00<00:00, 181571.60it/s]