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 ...
Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
Mike
  • 53
  • 1
  • 1
  • 3
  • 1
    have a look [here](https://stackoverflow.com/questions/3160699/python-progress-bar/26761413#26761413) and [here](https://stackoverflow.com/questions/3160699/python-progress-bar) – ZF007 Feb 02 '18 at 00:19
  • 2
    Possible duplicate of [Python Progress Bar](https://stackoverflow.com/questions/3160699/python-progress-bar) – ZF007 Feb 02 '18 at 00: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: https://github.com/rsalmei/alive-progress ![alive-progress](https://raw.githubusercontent.com/rsalmei/alive-progress/master/img/main.gif) – rsalmei Aug 23 '19 at 06:45

3 Answers3

6

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

SomeGuyOnAComputer
  • 5,414
  • 6
  • 40
  • 72
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.

Razmooo
  • 457
  • 1
  • 7
  • 19
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

Arun Karunagath
  • 1,593
  • 10
  • 24
  • 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. – Azhar Mar 10 '22 at 01:30