Last active
June 27, 2024 20:04
-
-
Save greenstick/b23e475d2bfdc3a82e34eaa1f6781ee4 to your computer and use it in GitHub Desktop.
Python: printProgressBar function with autoresize option
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
# This version of the printProgressBar function implements an optional autoresize argument. | |
# It has been updated from a previous version to use the shutil Python module to determine | |
# the terminal size. This update should allow it to work on most operating systems and does | |
# speed up the autosize feature quite a bit – though it still slows things down quite a bit. | |
# For more robust features, it's recommended you use a progress bar library like tdqm (see: https://github.com/tqdm/tqdm) | |
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', autosize = False): | |
""" | |
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) | |
length - Optional : character length of bar (Int) | |
fill - Optional : bar fill character (Str) | |
autosize - Optional : automatically resize the length of the progress bar to the terminal window (Bool) | |
""" | |
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total))) | |
styling = '%s |%s| %s%% %s' % (prefix, fill, percent, suffix) | |
if autosize: | |
cols, _ = shutil.get_terminal_size(fallback = (length, 1)) | |
length = cols - len(styling) | |
filledLength = int(length * iteration // total) | |
bar = fill * filledLength + '-' * (length - filledLength) | |
print('\r%s' % styling.replace(fill, bar), end = '\r') | |
# Print New Line on Complete | |
if iteration == total: | |
print() | |
# Sample Usage | |
import shutil, time | |
# A List of Items | |
items = list(range(0, 57)) | |
l = len(items) | |
# Initial call to print 0% progress | |
printProgressBar(0, l, prefix = 'Progress:', suffix = 'Complete', autosize = True) | |
for i, item in enumerate(items): | |
# Do stuff... | |
time.sleep(0.1) | |
# Update Progress Bar | |
printProgressBar(i + 1, l, prefix = 'Progress:', suffix = 'Complete', autosize = True) |
OK man. No prob.
Em seg, 7 de out de 2019 às 16:49, Greenstick <[email protected]>
escreveu:
… @ezequias <https://github.com/ezequias> As I understand it, this issue
has to do with the return character for the printProgressBar function.
It's set to '\r' but you should be able to set it to '\r\n' or '\n' to make
it work. The upshot is that your IDE handles the return and newline
characters differently than the terminal the function was developed in.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/b23e475d2bfdc3a82e34eaa1f6781ee4?email_source=notifications&email_token=AAEDXKI3GRTZOQCPEUVYRHLQNOHDLA5CNFSM4IGXTTJKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF2BXM#gistcomment-3048310>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEDXKNEBINL7ZIZWSIUM2TQNOHDLANCNFSM4IGXTTJA>
.
--
[image: Bitmoji Image]
Ezequias Rodrigues da Rocha
https://www.linkedin.com/in/ezequiasrocha
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ezequias As I understand it, this issue has to do with the return character for the
printProgressBar
function. It's set to\r
but you should be able to set it to\r\n
or\n
to make it work. The upshot is that your IDE handles the return and newline characters differently than the terminal the function was developed in.