Progress bar

0

Is there a way to make a progress bar in python? Or some bookstore ?. For example: if I have a function that in turn is composed of other functions or a cycle for which I must iterate in an immediate list. I would like to show the user how much time is left. Any ideas?

    
asked by pythonprub 21.07.2017 в 05:07
source

2 answers

1

Use the progress package.

from progress.bar import Bar

bar = Bar('Processing', max=20)
for i in range(20):
    # Do some work
    bar.next()
bar.finish()

To install it use pip: pip install progress

    
answered by 21.07.2017 в 06:03
0

If you do not want to install anything. A good implementation for python 2 is:

# -*- 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()

You can find it at the following link: link

I hope it serves you!

    
answered by 22.07.2017 в 23:28