Skip to content

Instantly share code, notes, and snippets.

@spava
Last active March 5, 2023 08:09
Show Gist options
  • Save spava/becabf22df0ccb985db0e68e5cad0280 to your computer and use it in GitHub Desktop.
Save spava/becabf22df0ccb985db0e68e5cad0280 to your computer and use it in GitHub Desktop.
a simple and useful progress bar in python
import sys
import time
def progress(iterable, length=33):
count = avg = 0
total = len(iterable)
then = time.time()
for it in iter(iterable):
yield it
count += 1
avg += (time.time() - then - avg) / count
then = time.time()
percent = count / total
filled_len = round(length * percent)
bar = '█' * filled_len + ' ' * (length - filled_len)
sys.stdout.write(f'▕{bar}▏ {round(100 * percent)}% {round((total - count) * avg)}s \r')
sys.stdout.flush()
sys.stdout.write(f'▕{"█" * length}▏ 100% {round(avg * total)}s \n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment