How to create a progress bar in Python

python progress bar


Creating a progress bar is really useful if you have a long-running task and you’d like to know how far along you are. In this post we’ll talk about two packages for creating progress bars in Python: tqdm and progressbar2.

Creating a progress bar with Python’s tqdm package

Let’s get started by installing tqdm. We can do that using pip:


pip install tqdm

Once we have tqdm installed, it’s straightforward to use. Essentially, we just need to wrap whatever object we’re looping over (i.e. any object that is iterable) inside tqdm. Then, when the code runs, Python will print out a progress bar so that you can know how far along you are.


from tqdm import tqdm

result = 0
for i in tqdm(range(10000000)):
    result += i

python tqdm

tqdm can also be used in list or dictionary comprehensions, like below. In this case, we use tqdm to give us the progress status of scraping price data for a collection of stocks.


import yahoo_fin.stock_info as si

tickers = si.tickers_dow()

stocks = [si.get_data(ticker) for ticker in tqdm(tickers)]

python progress bar

tqdm can also be utilized in asynchronous processes.


from tqdm.asyncio import trange, tqdm
async for i in trange(100):
    print(i)

You can learn more about tqdm by checking out its documentation here.

The progressbar2 package

Another option is to use the progressbar2 package. Similar to above, we can install progressbar2 using pip:


pip install progressbar2

We can replicate our example above using progressbar2 instead of tqdm.


import progressbar

tickers = si.tickers_dow()

data = {ticker : si.get_data(ticker) for ticker in tickers}

python progressbar2

Creating progress bars on unknown length

progessbar2 also handles progress bars with unknown length. For example, let’s take a look at the below code. Here, we have a while loop that runs continuously until the seconds of the current time equals 30. This can be useful at letting you know how much time has elapsed while a piece of code is running.


bar = progressbar.ProgressBar(max_value=progressbar.UnknownLength)
i = 0
while True:
    i += 1
    if time.localtime().tm_sec == 30:
        break
    bar.update(i)

python unknown progress bar length

Other types of progress bars

The progressbar2 package offers a collection of other progress bar types. For example, we can generate a minimalistic bar that shows just the percentage of how far the process has gone along.


bar = progressbar.ProgressBar(
    widgets=[
        progressbar.Percentage()
    ])

    
for i in bar(range(1000)):
    time.sleep(0.1)

python perentage progress bar

Here’s another example, where the progress bar slides back and forth until the loop is finished.


bar = progressbar.ProgressBar(
    widgets=[
        progressbar.BouncingBar()
    ])

    
for i in bar(range(1000)):
    time.sleep(0.1)

To learn more about progressbar2, see this link.

That’s all for now! If you liked learning how to create a progress bar with Python, please share this post with your friends. Learn more about Python / R / data science by checking out 365 Data Science’s great platform here, which includes my course on web scraping and APIs!