Last active
November 14, 2024 11:22
-
-
Save aubricus/f91fb55dc6ba5557fbab06119420dd6a to your computer and use it in GitHub Desktop.
Python Progress Bar
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
Copyright 2020 Aubrey Taylor | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
# -*- coding: utf-8 -*- | |
# Print iterations progress | |
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=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 : positive number of decimals in percent complete (Int) | |
bar_length - Optional : character length of bar (Int) | |
""" | |
str_format = "{0:." + str(decimals) + "f}" | |
percents = str_format.format(100 * (iteration / float(total))) | |
filled_length = int(round(bar_length * iteration / float(total))) | |
bar = '█' * filled_length + '-' * (bar_length - filled_length) | |
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)), | |
if iteration == total: | |
sys.stdout.write('\n') | |
sys.stdout.flush() |
@aubricus thanks!
I like a more restraint version of the progress bar :-)
=> |============>--------------------------------------------------------------------| 14.7%
Below you will find the code that prepares the bar string in order to achieve the result shown above
bar = '=' * filled_length + '>' + '-' * (bar_length - filled_length)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@simonthor I've updated it with an MIT lic.