Skip to content

Instantly share code, notes, and snippets.

@Jerakin
Created January 30, 2017 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jerakin/3b7c24f9324d00eac03c1464a1191540 to your computer and use it in GitHub Desktop.
Save Jerakin/3b7c24f9324d00eac03c1464a1191540 to your computer and use it in GitHub Desktop.
# Implementation of Brian Khuu's progress bar from http://stackoverflow.com/questions/3160699/python-progress-bar
# with some small tweaks to formatting
#
# 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%
#
# Usage
#for i in len(10):
# progressbar.update_progress((i+1)/len(10))
#
import sys
def update_progress(progress):
bar_length = 50 # 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(bar_length*progress))
text = "\rPercent: [{}] {:.1f}% {}".format("#"*block + "-"*(bar_length-block), progress*100, status)
sys.stdout.write(text)
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment