Progress bars in Python with tqdm
I ran a script which processed a large chunk of data, but was not sure what percentage of data has been processed and thought it would be nicer to have a progress bar, adding which was much simpler than I thought using tqdm.
In order to access the python library, we need to install it in the python environment, using:
pip install tqdm
Import package into the script:
from tqdm import tqdm
And just add the progress bar to your loop:
for i in tdqm(range(100)):
pass
To modify the progress bar:
for i in tqdm(range(100), ascii = True):
pass
Add description:
for i in tqdm(range(100), ascii = True, desc="bar"):
pass
Control width:
for i in tqdm(range(100), ncols=99):
pass
Modify color:
for i in tqdm(range(100), colour=green):
pass
You can also use a more optimised version of tqdm(range) i.e trange:
for i in trange(100):
pass
tqdm can also be used as pipes:
seq 9999999 | wc -l | tqdm
Let’s create a test directory with arbitrary large number of files:
mkdir -p test;touch sample{0001..9999}.txttar -zcf - test/ | tqdm --bytes --total `du -s test/ | cut -f1` > test.tgz
Resources:
Thanks for Reading !