Return to Snippet

Revision: 22070
at December 30, 2009 19:21 by gdvickery


Updated Code
import sys
import time
import math

# Output example: [=======   ] 75%

# width defines bar width
# percent defines current percentage
def progress(width, percent):
    marks = math.floor(width * (percent / 100.0))
    spaces = math.floor(width - marks)
    
    loader = '[' + ('=' * int(marks)) + (' ' * int(spaces)) + ']'
      
    sys.stdout.write("%s %d%%\r" % (loader, percent))
    if percent >= 100:
        sys.stdout.write("\n")
    sys.stdout.flush()
    
# Simulate doing something...
for i in xrange(100):
    progress(50, (i + 1)) # +1 because xrange is only 99
    time.sleep(0.1) # Slow it down for demo

Revision: 22069
at December 30, 2009 19:20 by gdvickery


Updated Code
import sys
import time
import math

# width defines bar width
# percent defines current percentage
def progress(width, percent):
    marks = math.floor(width * (percent / 100.0))
    spaces = math.floor(width - marks)
    
    loader = '[' + ('=' * int(marks)) + (' ' * int(spaces)) + ']'
      
    sys.stdout.write("%s %d%%\r" % (loader, percent))
    if percent >= 100:
        sys.stdout.write("\n")
    sys.stdout.flush()
    
for i in xrange(100):
    progress(50, (i + 1)) # +1 because xrange is only 99
    time.sleep(0.1) # Slow it down for demo

# Output: [=======   ] 75%

Revision: 22068
at December 30, 2009 19:15 by gdvickery


Initial Code
import sys
import time
import math

def progress(width, percent):
    marks = math.floor(width * (percent / 100.0))
    spaces = math.floor(width - marks)
    
    loader = '[' + ('=' * int(marks)) + (' ' * int(spaces)) + ']'
      
    sys.stdout.write("%s %d%%\r" % (loader, percent))
    if percent >= 100:
        sys.stdout.write("\n")
    sys.stdout.flush()
    
for i in xrange(100):
    progress(10, (i + 1)) # +1 because xrange is only 99
    time.sleep(0.1) # Slow it down for demo

Initial URL


Initial Description
This is a function that will show a progress bar of the given percentage. Useful when performing time consuming tasks.

Initial Title
Python CLI (Command Line) Progress Bar

Initial Tags
python

Initial Language
Python