Advertisement
jack06215

[Python] progress bar

Jun 28th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. import time, sys
  2.  
  3. # update_progress() : Displays or updates a console progress bar
  4. ## Accepts a float between 0 and 1. Any int will be converted to a float.
  5. ## A value under 0 represents a 'halt'.
  6. ## A value at 1 or bigger represents 100%
  7. def update_progress(progress):
  8.     barLength = 30 # Modify this to change the length of the progress bar
  9.     status = ""
  10.     if isinstance(progress, int):
  11.         progress = float(progress)
  12.     if not isinstance(progress, float):
  13.         progress = 0
  14.         status = "error: progress var must be float\r\n"
  15.     if progress < 0:
  16.         progress = 0
  17.         status = "Halt...\r\n"
  18.     if progress >= 1:
  19.         progress = 1
  20.         status = "Done...\r\n"
  21.     block = int(round(barLength*progress))
  22.     progress = int(progress * 100)
  23.     text = "\rProgress: [{0}] {1}% {2}".format( "■"*block + "."*(barLength-block), progress, status)
  24.     sys.stdout.write(text)
  25.     sys.stdout.flush()
  26.    
  27. # print("progress : 0->1")
  28. # for i in range(101):
  29. #     time.sleep(0.1)
  30. #     update_progress(float(i/100.0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement