Skip to content

Instantly share code, notes, and snippets.

@rg3915
Last active January 10, 2021 22:30
Show Gist options
  • Save rg3915/b6368374f74d00d9ea045470718a8ddd to your computer and use it in GitHub Desktop.
Save rg3915/b6368374f74d00d9ea045470718a8ddd to your computer and use it in GitHub Desktop.
Progress bar
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.
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)
@rg3915
Copy link
Author

rg3915 commented Jul 20, 2019

@rg3915
Copy link
Author

rg3915 commented Jul 20, 2019

@rg3915
Copy link
Author

rg3915 commented Jul 22, 2019

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