Last active
March 16, 2017 06:06
-
-
Save marty-Wallace/cc4fe14ad1dbec4e7c75c88323a915ae to your computer and use it in GitHub Desktop.
python progress bar for terminals
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 | |
def drawProgressBar(percent_done, barLength = 40): | |
"""Display an updating progress bar in a terminal | |
:percent_done: the percent done to display | |
:barLength: how many chars long the bar is | |
:returns: None | |
""" | |
sys.stdout.write("\r") | |
progress = "" | |
for i in range(barLength): | |
if i <= int(barLength * percent_done): | |
if i+1 <= int(barLength * percent_done): | |
progress += "=" | |
else: | |
progress += ">" | |
else: | |
progress += " " | |
sys.stdout.write("[%s] %.2f%%" % (progress, percent_done * 100)) | |
sys.stdout.flush() | |
def main(): | |
"""Main function. Displays an example of progress bar | |
:returns: None | |
""" | |
for k in range(1,5): | |
print("Processing Very Important item %d" % k) | |
for i in range(50000): | |
drawProgressBar(i/50000, 20) | |
for j in range(1000): | |
pass | |
print() | |
# won't run if it's imported | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment