Python to print out status bar and percentage

Posted on

Question :

Python to print out status bar and percentage

To implement a status bar like below:

[==========                ]  45%
[================          ]  60%
[==========================] 100%

I want to this to be printed out to stdout, and keep refreshing it, not print to another line. How to do this?

Asked By: Stan

||

Answer #1:

There’s a Python module that you can get from PyPI called progressbar that implements such functionality. If you don’t mind adding a dependency, it’s a good solution. Otherwise, go with one of the other answers.

A simple example of how to use it:

import progressbar
from time import sleep
bar = progressbar.ProgressBar(maxval=20, 
    widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
for i in xrange(20):
    bar.update(i+1)
    sleep(0.1)
bar.finish()

To install it, you can use easy_install progressbar, or pip install progressbar if you prefer pip.

Answered By: icktoofay

Answer #2:

The 'r' character (carriage return) resets the cursor to the beginning of the line and allows you to write over what was previously on the line.

from time import sleep
import sys

for i in range(21):
    sys.stdout.write('r')
    # the exact output you're looking for:
    sys.stdout.write("[%-20s] %d%%" % ('='*i, 5*i))
    sys.stdout.flush()
    sleep(0.25)

I’m not 100% sure if this is completely portable across all systems, but it works on Linux and OSX at the least.

Answered By: Mark Rushakoff

Answer #3:

I found useful library tqdm (https://github.com/tqdm/tqdm/, previously: https://github.com/noamraph/tqdm). It automatically estimates time of completion and can be used as iterator.

Usage:

import tqdm
import time

for i in tqdm.tqdm(range(1000)):
    time.sleep(0.01)
    # or other long operations

Results in:

|####------| 450/1000  45% [elapsed: 00:04 left: 00:05, 99.15 iters/sec]

tqdm can wrap any iterable.

Answered By: omikron

Answer #4:

You can use r (carriage return). Demo:

import sys
total = 10000000
point = total / 100
increment = total / 20
for i in xrange(total):
    if(i % (5 * point) == 0):
        sys.stdout.write("r[" + "=" * (i / increment) +  " " * ((total - i)/ increment) + "]" +  str(i / point) + "%")
        sys.stdout.flush()
Answered By: Matthew Flaschen

Answer #5:

Here you can use following code as a function:

def drawProgressBar(percent, barLen = 20):
    sys.stdout.write("r")
    progress = ""
    for i in range(barLen):
        if i < int(barLen * percent):
            progress += "="
        else:
            progress += " "
    sys.stdout.write("[ %s ] %.2f%%" % (progress, percent * 100))
    sys.stdout.flush()

With use of .format:

def drawProgressBar(percent, barLen = 20):
    # percent float from 0 to 1. 
    sys.stdout.write("r")
    sys.stdout.write("[{:<{}}] {:.0f}%".format("=" * int(barLen * percent), barLen, percent * 100))
    sys.stdout.flush()
Answered By: Jacob CUI

Answer #6:

based on the above answers and other similar questions about CLI progress bar, I think I got a general common answer to all of them. Check it at https://stackoverflow.com/a/15860757/2254146

Here is a copy of the function, but modified to fit your style:

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 = 20 # 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 floatrn"
    if progress < 0:
        progress = 0
        status = "Halt...rn"
    if progress >= 1:
        progress = 1
        status = "Done...rn"
    block = int(round(barLength*progress))
    text = "rPercent: [{0}] {1}% {2}".format( "="*block + " "*(barLength-block), progress*100, status)
    sys.stdout.write(text)
    sys.stdout.flush()

Looks like

Percent: [====================] 99.0%

Answered By: Brian Khuu

Answer #7:

Further improving, using a function as :

import sys

def printProgressBar(i,max,postText):
    n_bar =10 #size of progress bar
    j= i/max
    sys.stdout.write('r')
    sys.stdout.write(f"[{'=' * int(n_bar * j):{n_bar}s}] {int(100 * j)}%  {postText}")
    sys.stdout.flush()

calling example:

total=33
for i in range(total):
    printProgressBar(i,total,"blah")
    sleep(0.05)  

output:

[================================================  ] 96%  blah  
Answered By: STG

Answer #8:

If you are developing a command line interface, I suggest you to take a look at click which is very nice:

import click
import time

for filename in range(3):
    with click.progressbar(range(100), fill_char='=', empty_char=' ') as bar:
        for user in bar:
            time.sleep(0.01)

Here the output you get:

$ python test.py
  [====================================]  100%
  [====================================]  100%
  [=========                           ]   27%
Answered By: nowox

Leave a Reply

Your email address will not be published.