Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add progress bar to a python function

Appreciate any help for a beginner :) I tried the below but not sure how to wrap the def Job():

import time
from progressbar import ProgressBar


pbar = ProgressBar()
def job():
        Script ....
        Script ...
        Script ...
        Script ...
like image 513
Mike Avatar asked Dec 23 '22 10:12

Mike


1 Answers

You can use progressbar like this:

import time
from progressbar import ProgressBar

pbar = ProgressBar()

def job():
    for i in pbar(xrange(5)):
        print(i)

job()

Output looks like this:

0 0% |                                                                         |
120% |##############                                                           |
240% |#############################                                            |
360% |###########################################                              |
480% |##########################################################               |
100% |#########################################################################

I like tqdm a lot more and it works the same way.

from tqdm import tqdm
for i in tqdm(range(10000)):
    pass

Image

enter image description here

like image 180
SomeGuyOnAComputer Avatar answered Dec 25 '22 22:12

SomeGuyOnAComputer