Skip to content

Instantly share code, notes, and snippets.

@splch
Forked from spava/progress.py
Last active April 21, 2024 11:34
Show Gist options
  • Save splch/b9ca7f11b945863890a9d0e716e085f5 to your computer and use it in GitHub Desktop.
Save splch/b9ca7f11b945863890a9d0e716e085f5 to your computer and use it in GitHub Desktop.
a simple and useful progress bar in python
import time, sys
def progress(iterable, length=33):
total, start = len(iterable), time.monotonic()
for count, it in enumerate(iterable, 1):
yield it
if count % max(1, total // 10000) == 0 or count == total:
percent = count / total
sys.stdout.write(
f'\r▕{"█" * int(length * percent) + " " * (length - int(length * percent))}▏ '
f'{percent * 100:.2f}% '
f'{(time.monotonic() - start) / count * (total - count):.2f}s'
), sys.stdout.flush()
sys.stdout.write(f'\r▕{"█" * length}▏ 100.00% {time.monotonic() - start:.2f}s\n')
@splch
Copy link
Author

splch commented Mar 5, 2023

  1. Import the progress function

  2. Test the function

for _ in progress(range(1000000)):
    pass
  1. Confirm the output appears like below
▕█████████████████████████████████▏ 100.00% 3.33s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment