5

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 ...
3
  • 1
    have a look here and here Commented Feb 2, 2018 at 0:19
  • 2
    Possible duplicate of Python Progress Bar Commented Feb 2, 2018 at 0:20
  • I've published a new kind of progress bar, which you can print, see throughput and eta, even pause it, besides the very cool animations! Please take a look: github.com/rsalmei/alive-progress !alive-progress Commented Aug 23, 2019 at 6:45

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

Comments

3

You can use the bar object this way:

import time
import progressbar


def job():
    bar = progressbar.ProgressBar()
    for i in bar(range(100)):
        ... # Code that you want to run
        #time.sleep(0.02)

job()

If the code you want to execute has fast execution time, you can put a time.sleep() inside in order not to have progressbar set to 100% in the beginning.

Comments

2

From the documentation, it seems really straight forward

pbar = ProgressBar().start()
def job():
   total_steps = 7
    # script 1
    pbar.update((1/7)*100)  # current step/total steps * 100
    # script 2
    pbar.update((2/7)*100)  # current step/total steps * 100
    # ....
    pbar.finish()

Also, don't be afraid to check out the source code https://github.com/niltonvolpato/python-progressbar/blob/master/progressbar/progressbar.py

1 Comment

This answer is actually the best (atm) answering the context of the question that is progress bar for the tasks in the function rather than on an iterable. It would be better if it can generally wrap any function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.