How do I use a progress bar when my script is doing some task that is likely to take time?
For example, a function which takes some time to complete and returns True
when done. How can I display a progress bar during the time the function is being executed?
Note that I need this to be in real time, so I can't figure out what to do about it. Do I need a thread
for this? I have no idea.
Right now I am not printing anything while the function is being executed, however a progress bar would be nice. Also I am more interested in how this can be done from a code point of view.
A text progress bar is typically used to display the progress of a long running operation, providing a visual cue that processing is underway. The progressbar is based on the old Python progressbar package that was published on the now defunct Google Code.
tqdm is a library in Python which is used for creating Progress Meters or Progress Bars. tqdm got its name from the Arabic name taqaddum which means 'progress'. Implementing tqdm can be done effortlessly in our loops, functions or even Pandas.
We can utilize tqdm module with Jupyter Notebook to keep track of progress. In the machine learning area, when we train our model using a set of epochs, then to visualize the completion progress of each epoch, we can utilize tqdm . IPython/Jupyter is supported using the tqdm. notebook submodule.
With tqdm (conda install tqdm
or pip install tqdm
) you can add a progress meter to your loops in a second:
from time import sleep from tqdm import tqdm for i in tqdm(range(10)): sleep(3) 60%|██████ | 6/10 [00:18<00:12, 0.33 it/s]
Also, there is a notebook version:
from tqdm.notebook import tqdm for i in tqdm(range(100)): sleep(3)
You can use tqdm.auto
instead of tqdm.notebook
to work in both a terminal and notebooks.
tqdm.contrib
contains some helper functions to do things like enumerate
, map
, and zip
. There are concurrent maps in tqdm.contrib.concurrent
.
You can even get progress sent to your phone after disconnecting from a jupyter notebook using tqdm.contrib.telegram
or tqdm.contrib.discord
.
There are specific libraries (like this one here) but maybe something very simple would do:
import time import sys toolbar_width = 40 # setup toolbar sys.stdout.write("[%s]" % (" " * toolbar_width)) sys.stdout.flush() sys.stdout.write("\b" * (toolbar_width+1)) # return to start of line, after '[' for i in xrange(toolbar_width): time.sleep(0.1) # do real work here # update the bar sys.stdout.write("-") sys.stdout.flush() sys.stdout.write("]\n") # this ends the progress bar
Note: progressbar2 is a fork of progressbar which hasn't been maintained in years.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With