Python Progress Bar Python Progress Bar python python

Python Progress Bar


With tqdm (conda install tqdm or pip install tqdm) you can add a progress meter to your loops in a second:

from time import sleepfrom tqdm import tqdmfor i in tqdm(range(10)):    sleep(3) 60%|██████    | 6/10 [00:18<00:12,  0.33 it/s]

Also, there is a notebook version:

from tqdm.notebook import tqdmfor i in tqdm(range(100)):    sleep(3)

You can use tqdm.auto instead of tqdm.notebook to work in both a terminal and notebooks.

tqdm.contrib contains some helper functions to do things like enumerate, map, and zip. There are concurrent maps in tqdm.contrib.concurrent.

You can even get progress sent to your phone after disconnecting from a jupyter notebook using tqdm.contrib.telegram or tqdm.contrib.discord.


There are specific libraries (like this one here) but maybe something very simple would do:

import timeimport systoolbar_width = 40# setup toolbarsys.stdout.write("[%s]" % (" " * toolbar_width))sys.stdout.flush()sys.stdout.write("\b" * (toolbar_width+1)) # return to start of line, after '['for i in xrange(toolbar_width):    time.sleep(0.1) # do real work here    # update the bar    sys.stdout.write("-")    sys.stdout.flush()sys.stdout.write("]\n") # this ends the progress bar

Note: progressbar2 is a fork of progressbar which hasn't been maintained in years.


The above suggestions are pretty good, but I think most people just want a ready made solution, with no dependencies on external packages, but is also reusable.

I got the best points of all the above, and made it into a function, along with a test cases.

To use it, just copy the lines under "def update_progress(progress)" but not the test script. Don't forget to import sys. Call this whenever you need to display or update the progress bar.

This works by directly sending the "\r" symbol to console to move cursor back to the start. "print" in python does not recongise the above symbol for this purpose, hence we need 'sys'

import time, sys# update_progress() : Displays or updates a console progress bar## Accepts a float between 0 and 1. Any int will be converted to a float.## A value under 0 represents a 'halt'.## A value at 1 or bigger represents 100%def update_progress(progress):    barLength = 10 # Modify this to change the length of the progress bar    status = ""    if isinstance(progress, int):        progress = float(progress)    if not isinstance(progress, float):        progress = 0        status = "error: progress var must be float\r\n"    if progress < 0:        progress = 0        status = "Halt...\r\n"    if progress >= 1:        progress = 1        status = "Done...\r\n"    block = int(round(barLength*progress))    text = "\rPercent: [{0}] {1}% {2}".format( "#"*block + "-"*(barLength-block), progress*100, status)    sys.stdout.write(text)    sys.stdout.flush()# update_progress test scriptprint "progress : 'hello'"update_progress("hello")time.sleep(1)print "progress : 3"update_progress(3)time.sleep(1)print "progress : [23]"update_progress([23])time.sleep(1)print ""print "progress : -10"update_progress(-10)time.sleep(2)print ""print "progress : 10"update_progress(10)time.sleep(2)print ""print "progress : 0->1"for i in range(101):    time.sleep(0.1)    update_progress(i/100.0)print ""print "Test completed"time.sleep(10)

This is what the result of the test script shows (The last progress bar animates):

progress : 'hello'Percent: [----------] 0% error: progress var must be floatprogress : 3Percent: [##########] 100% Done...progress : [23]Percent: [----------] 0% error: progress var must be floatprogress : -10Percent: [----------] 0% Halt...progress : 10Percent: [##########] 100% Done...progress : 0->1Percent: [##########] 100% Done...Test completed