There was no slogan, and I woke up like the title in the morning and cut off the water. I wondered what to do, and when I googled It was written in various ways on StackOverflow / questions / 3173320 / text-progress-bar-in-the-console).
From that post, this post does not depend on other libraries, I just flushed and rewritten, so I thought it was simple, so reference I was allowed to. Somehow this is not displayed 100%, and I wondered what it was, so I improved it a little. However, I modified it to 100% at the end.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import time
from random import random
def progress_bar(label, end_val, bar_length=40, slug='#', space=' '):
    def writing_bar(label, bar, percent):
        sys.stdout.write("\r{label}: [{bar}] {percent}%".format(
            label=label, bar=bar, percent=percent
        ))
        sys.stdout.flush()
    for i in range(0, end_val):
        percent = float(i) / end_val
        slugs = slug * int(round(percent * bar_length))
        spaces = space * (bar_length - len(slugs))
        # Some processing...
        # Is provisional
        time.sleep(random() * 0.1)
        writing_bar(label, slugs + spaces, int(round(percent * 100)))
    writing_bar(label, slugs + spaces, 100)
    sys.stdout.write('\n')
if __name__ == '__main__':
    progress_bar("Processing", 100)
Since the time.sleep part is the processing, replace it as appropriate or Or make progress_bar itself a decorator, Otherwise it will not be usable normally. However, the progress bar is Wget or curl -O in a cli application You get resources on the net, so you don't have to think so much ...?
Recommended Posts