Last active
January 10, 2021 22:30
-
-
Save rg3915/b6368374f74d00d9ea045470718a8ddd to your computer and use it in GitHub Desktop.
Progress bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
# https://gist.github.com/rg3915/afb3d988b09a2ad6f95d98392d6ff4ac | |
# https://stackoverflow.com/a/3160819 | |
def progressbar(it, prefix="", size=60, file=sys.stdout): | |
count = len(it) | |
def show(j): | |
x = int(size * j / count) | |
file.write("%s[%s%s] %i/%i\r" % | |
(prefix, "#" * x, "." * (size - x), j, count)) | |
file.flush() | |
show(0) | |
for i, item in enumerate(it): | |
yield item | |
show(i + 1) | |
file.write("\n") | |
file.flush() | |
users = ['Regis', 'Abel', 'Eduardo', 'Elaine'] | |
for user in progressbar(users, "Processing: "): | |
time.sleep(0.1) | |
# Do something. | |
for i in progressbar(range(42), "Processing: "): | |
time.sleep(0.05) | |
# Do something. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click | |
from time import sleep | |
from random import randint | |
fill_char = click.style('>', fg='yellow') | |
for i in range(1, 10): | |
max_length = randint(80, 300) | |
label = f'Loading... {i}/10 -> {str(max_length).zfill(3)}' | |
with click.progressbar(range(max_length), label=label, fill_char=fill_char) as bar: | |
for i in bar: | |
sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://codingdose.info/posts/how-to-use-a-progress-bar-in-python/