Skip to content

Instantly share code, notes, and snippets.

@wzjoriv
Last active July 17, 2024 16:22
Show Gist options
  • Save wzjoriv/4906a6722805f2d1194a40ddfd158642 to your computer and use it in GitHub Desktop.
Save wzjoriv/4906a6722805f2d1194a40ddfd158642 to your computer and use it in GitHub Desktop.
Simple function to print a refreshing progress bar in python
"""
Author: Josue N Rivera
Date: 7/16/2024
Description: Function to print a progress bar into the console given a value between 0.0 and 1.0
"""
def progress_bar(percentage, size=100, end=False, fill_char='|'):
percentage = max(0.0, min(1.0, percentage)) # clamp to range [0.0, 1.0]
percentage_int = int(percentage*100) # convert to integer percentage
count = int(percentage*size) # determine number of characters to fill
print(f"({percentage_int:3d}%)[{fill_char*count+' '*(size-count)}]", end="\n" if percentage == 1.0 or end else "\r")
if __name__ == '__main__':
import time
import numpy as np
n = 1010
# Percentange of something
mask = np.random.random(n) > 0.5 ## number of random values greater than 0.5
progress_bar(sum(mask)/len(mask), end=True)
# Refreshing progress bar
progress_bar(0.0)
for i in range(n):
## Do something
time.sleep(0.005)
progress_bar((i+1.0)/n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment