[PYTHON] How to temporarily implement a progress bar in a scripting language

Introduction

When I'm running a script and I'm not sure what's going on, I use a lot of Print statements, but this is pretty ugly, so I'll implement a progress bar that uses "#".

Ruby

test.rb


(0..10).each do |i|
  sleep(1)
  print "\r" + "%3d" % (100.0 * i.to_f / 10.0) + "% " + "#" * i
end

puts "\nEND"

Python

test.py


import time

for i in range(11):
    time.sleep(1)
    print("\r{0:3d}%".format(int(100.0 * i / 10)), "#" * i , end='')
    
print('\nEND')

result

Execute the command.

python


$ ruby test.rb
100% ##########
END
$ python test.py
100% ##########
END

I don't know from the result alone, but the number of "#" has increased over time (^ _ ^;) It might be convenient ...

By the way, you can also use it with Jupyter notebook. However, in the case of Python, there is a dedicated library, so you may not use it (sweat)

Please check the following article for how to use it. http://qiita.com/mix_dvd/items/e613c2714c7ea0e81be9

Recommended Posts

How to temporarily implement a progress bar in a scripting language
How to implement a gradient picker in Houdini
How to display the progress bar (tqdm)
How to implement nested serializer in drf-flex-fields
How to get a stacktrace in python
How to implement Scroll View in pythonista 1
How to make a request to bitFlyer Lightning's Private API in Go language
How to clear tuples in a list (Python)
How to implement Rails helper-like functionality in Django
How to embed a variable in a python string
How to implement Discord Slash Command in Python
Create an executable file in a scripting language
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
How to multi-process exclusive control in C language
How to notify a Discord channel in Python
How to Implement a new CPUFreq Processor Driver
[Python] How to draw a histogram in Matplotlib
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to read a file in a different directory
How to Mock a Public function in Pytest
How to convert / restore a string with [] in python
Try to make a Python module in C language
[Python] How to expand variables in a character string
I tried to implement a pseudo pachislot in Python
A memorandum on how to use keras.preprocessing.image in Keras
How to display DataFrame as a table in Markdown
How to add color bars in matplotlib bar plot
How to execute a command using subprocess in Python
How to reference static files in a Django project
How to view progress bar on Jupyter Notebook to see progress
[Linux] How to put your IP in a variable
[Go language] How to get terminal input in real time
How to import NoteBook as a module in Jupyter (IPython)
How to output a document in pdf format with Sphinx
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to call a function
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
How to import a file anywhere you like in Python
How to define multiple variables in a python for statement
A note on how to load a virtual environment in PyCharm
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to generate a query using the IN operator in Django
How to hack a terminal
How to check if a value exists in an enum
How to get the last (last) value in a list in Python
How to implement Python EXE for Windows in Docker container
How to get a list of built-in exceptions in python
How to develop in Python
How to import NoteBook as a module in Jupyter (IPython)
A story about trying to implement a private variable in Python.
Try to select a language
Try HeloWorld in your own language (with How to & code)
How to get a quadratic array of squares in a spiral!
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
How to display a specified column of files in Linux (awk)