Last active
August 12, 2019 16:52
-
-
Save Da-Juan/8da20a023b5acc013cfee3c2588f2b85 to your computer and use it in GitHub Desktop.
Simple progress bar in Python 3.6
This file contains hidden or 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
#!/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