[Python] A progress bar on the terminal

What you should have when you play a slightly longer script and leave it

An example of turning something n times with a for statement

import sys
import time


def something():
    time.sleep(t_sleep)
    pass

def update_progbar(progress):
    prog_length = int(bar_length * progress)
    return ('[[' + '-' * prog_length +
            ('>' if prog_length < bar_length else '') +
            '*' * (bar_length - prog_length) +
            ']] %.1f%%' % (progress * 100.))



n = 123
t_sleep = 0.05
bar_length = 30


t0 = time.time()
t1 = t0

print '\n'

for i in range(n):
    
    something()
    
    t2 = time.time()
    progress = i/float(n)
    t_remain = (t2 - t1) * (n - i)
    sys.stderr.write('\r\033[K' + update_progbar(progress)+
                     " Remaining: %.1f sec"%t_remain)
    sys.stderr.flush()

    t1 = time.time()

sys.stderr.write('\r\033[K' + update_progbar(1.0))
print '\n'
print 'Total: %.1f sec'%(t1 - t0), time.ctime()
print '\n'

reference: http://www.yunabe.jp/docs/terminal_progress_bar.html

However, even if you don't do this, there seems to be a module called tqdm these days ... https://pypi.python.org/pypi/tqdm http://postd.cc/my-top-5-new-python-modules-of-2015/

Recommended Posts

[Python] A progress bar on the terminal
The one that displays the progress bar in Python
Create a GUI on the terminal using curses
[Python] Adjusting the color bar
Make a breakpoint on the c layer with python
Show progress bar and remaining time on console (python)
Write a log-scale histogram on the x-axis in python
Building a Python environment on Mac
Building a Python environment on Ubuntu
Progress bar in pop-up in Python Kivy
[Python] Make the function a lambda function
Create a python environment on centos
Build a python3 environment on CentOS7
Sakura Use Python on the Internet
Life game with Python [I made it] (on the terminal & Tkinter)
Control the motor with a motor driver using python on Raspberry Pi 3!
Get the number of readers of a treatise on Mendeley in Python
Write the test in a python docstring
Build a python environment on MacOS (Catallina)
Create a python environment on your Mac
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Search the maze with the python A * algorithm
Run the Python interpreter in a script
Creating a python virtual environment on Windows
Download files on the web with Python
A little more detail on python comprehensions
I want to display the progress bar
Build a Python + OpenCV environment on Cloud9
[Python] A program that rounds the score
Create a shortcut to run a Python file in VScode on your terminal
[Python, ObsPy] I drew a beach ball on the map with Cartopy + ObsPy.
[2015/11/19] How to register a service locally using the python SDK on naoqi os
A memo of a tutorial on running python on heroku
Periodically run a python program on AWS Lambda
[Python] Get the files in a folder with Python
Created a Python wrapper for the Qiita API
Python on Windows
twitter on python3
Simply build a Python 3 execution environment on Windows
Get the caller of a function in Python
A note on customizing the dict list class
Build a Python environment on Mac (Mountain Lion)
Python: Try using the UI on Pythonista 3 on iPad
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
Make a copy of the list in Python
A note on optimizing blackbox functions in Python
Python: Prepare a serializer for the class instance:
A note about the python version of python virtualenv
Create a color bar with Python + Qt (PySide)
Folium: Visualize data on a map with Python
Build a Python development environment on your Mac
Try CIing the pushed python code on GitHub.
Building multiple Python environments on the same system
A story about running Python on PHP on Heroku
A memorandum for touching python Flask on heroku
Introduction to Python with Atom (on the way)
Calculate the probability of outliers on a boxplot
A memorandum about the Python tesseract wrapper library
[Python] A program that calculates the difference between the total numbers on the diagonal line.
Sound the buzzer using python on Raspberry Pi 3!