Hay alguna manera de hacer una barra de progreso en python ? O alguna librería?. Por ejm: si tengo una función que a su vez se compone de otras funciones o un ciclo for que deba iterar en una lista inmesa. Me gustaría mostrarle al usuario cuanto es el tiempo restante.alguna idea?
2 respuestas
Usa el paquete progress.
from progress.bar import Bar
bar = Bar('Processing', max=20)
for i in range(20):
# Do some work
bar.next()
bar.finish()
Para instalarlo usa pip: pip install progress
Si no querés instalar nada. Una buena implementación para python 2 es:
# -*- 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()
La podés encontrar en el siguiente link: https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a
Espero que te sirva!