[PYTHON] I want to display the progress bar

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

I want to display the progress bar
I want to display the progress in Python!
How to display the progress bar (tqdm)
I want to pin Spyder to the taskbar
I want to output to the console coolly
I want to handle the rhyme part1
I want to handle the rhyme part3
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to display multiple images with matplotlib.
I want to handle the rhyme part7 (BOW)
I want to customize the appearance of zabbix
I want to use the activation function Mish
I want to see the file name from DataLoader
I want to grep the execution result of strace
I want to scroll the Django shift table, but ...
I want to solve Sudoku (Sudoku)
I want to inherit to the back with python dataclass
I want to fully understand the basics of Bokeh
I want to write in Python! (3) Utilize the mock
I want to handle the rhyme part6 (organize once)
I want to automate ssh using the expect command!
I want to publish the product at the lowest cost
I want to use the R dataset in python
I want to handle the rhyme part8 (finished once)
I want to increase the security of ssh connections
I want to read CSV line by line while converting the field type (while displaying the progress bar) and process it.
[TensorFlow] I want to master the indexing for Ragged Tensor
I want to use the latest gcc without sudo privileges! !!
I want to initialize if the value is empty (python)
I want to save the photos sent by LINE to S3
I want to automate ssh using the expect command! part2
maya Python I want to fix the baked animation again.
I want to move selenium for the time being [for mac]
I want to use only the normalization process of SudachiPy
I want to get the operation information of yahoo route
I want to change the Japanese flag to the Palau flag with Numpy
I want to calculate the allowable downtime from the operating rate
I tried to display the time and today's weather w
I want to judge the authenticity of the elements of numpy array
I want to know the features of Python and pip
I want to make the Dictionary type in the List unique
I want to map the EDINET code and securities number
Keras I want to get the output of any layer !!
I want to align the significant figures in the Numpy array
I want to know the legend of the IT technology world
I want to create a Dockerfile for the time being.
I didn't want to write the AWS key in the program
I want to understand systemd roughly
I want to scrape images to learn
I want to do ○○ with Pandas
I want to copy yolo annotations
I want to debug with Python
I tried to move the ball
I tried to estimate the interval.
I don't tweet, but I want to use tweepy: just display the search results on the console
[Selenium] I want to display the browser by hitting the driver on the host OS from WSL
I want to get the name of the function / method being executed
I want to record the execution time and keep a log.
I want to manually assign the training parameters of the [Pytorch] model