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
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)]
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}
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)
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)
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!
Visit TheAutomatic.net for additional insight on this topic and to download the Python scripts.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from TheAutomatic.net and is being posted with its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.