Created
May 24, 2016 16:17
-
-
Save cocomice/9270ea38d9e6198a2c35aa4fdc087348 to your computer and use it in GitHub Desktop.
print the progress bar during execution
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
| import sys as Sys | |
| # Print iterations progress | |
| def printProgress (iteration, total, prefix = '', suffix = '', decimals = 2, barLength = 100): | |
| """ | |
| Call in a loop to create terminal progress bar | |
| @params: | |
| iteration - Required : current iteration (Int) | |
| total - Required : total iterations (Int) | |
| prefix - Optional : prefix string (Str) | |
| suffix - Optional : suffix string (Str) | |
| decimals - Optional : number of decimals in percent complete (Int) | |
| barLength - Optional : character length of bar (Int) | |
| """ | |
| filledLength = int(round(barLength * iteration / float(total))) | |
| percents = round(100.00 * (iteration / float(total)), decimals) | |
| bar = '#' * filledLength + '-' * (barLength - filledLength) | |
| Sys.stdout.write('%s [%s] %s%s %s\r' % (prefix, bar, percents, '%', suffix)), | |
| Sys.stdout.flush() | |
| if iteration == total: | |
| print("\n") | |
| # | |
| # Sample Usage | |
| # | |
| items = ["a", "b", "c", "d", "e"] | |
| i = 0 | |
| l = len(items) | |
| # Initial call to print 0% progress | |
| printProgress(i, l, prefix = 'Progress:', suffix = 'Complete', barLength = 50) | |
| for item in items: | |
| # Do stuff... | |
| # Update Progress Bar | |
| i += 1 | |
| printProgress(i, l, prefix = 'Progress:', suffix = 'Complete', barLength = 50) | |
| # Sample Output | |
| Progress: [#############################################-----] 90.0% Complete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment