Skip to content

Instantly share code, notes, and snippets.

@Da-Juan
Last active August 12, 2019 16:52
Show Gist options
  • Save Da-Juan/8da20a023b5acc013cfee3c2588f2b85 to your computer and use it in GitHub Desktop.
Save Da-Juan/8da20a023b5acc013cfee3c2588f2b85 to your computer and use it in GitHub Desktop.
Simple progress bar in Python 3.6
#!/usr/bin/env python3
"""Display a progressbar."""
import sys
import time
def progressbar(
progress: int, total: int,
percentage: bool = True, bar_width: int = 40,
todo: str = ' ', done: str = '=',
start: str = '[', end: str = ']'
) -> str:
"""
Build a progressbar.
Args:
progress: The progress step.
total: The total items count.
bar_width: The progress bar's width.
todo: Character to draw for progress yet to do.
done: Character to draw for progress done.
start: Character to draw at the beginning of the bar.
end: Character to draw at the end of the bar.
Returns:
The progress bar to display.
"""
percent = progress / total
completed = round(abs(percent * bar_width - 2))
bar = f'{start}{completed * done}{(bar_width - 2 - completed) * todo}{end}'
if percentage:
bar += f' {percent * 100:.2f}%'
return bar
nb_items = 200
for count in range(nb_items):
print(progressbar(count, nb_items), end='\r')
time.sleep(0.01)
sys.stdout.write('\033[K')
print('Done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment