You are probably aware of what Python libraries are. They are bundles of code that can be used over and over to accomplish similar goals. Libraries contain modules that let developers introduce functionalities without extensively programming them. tqdm in Python is one such library. In this post, we will learn what tqdm is, how you can use the tqdm package for python, how you can install it, and whatnot. Apart from that, we will also learn how to use the tqdm_notebook() module and how it is different.
What is tqdm () in Python?
tqdm is a Python external library that is used to create progress bars. Progress bars are the rectangular bars that fill up as a process moves toward completion. For instance, we have all witnessed the bars filling up with green juice as we download something from the internet, install an application, or transfer data from one place to another.
The term tqdm derives from the Arabic word taqadum which means progress. So, you get the idea that this library is specifically designed to help developers create progress bars. You can wrap the tqdm() module around any iterable to create a smart progress bar in Python – tqdm(iterable).
How are the tqdm progress bars used?
Using Python for processes like data scraping and training machine learning modules is often all about running excessively large loops behind the scenes and scraping through huge datasets. The processes can take a long time and there is no way of knowing whether the kernel is at all working. This is where the progress bars come in.
You can use a progress bar to
- Monitor the progress of a process through visual representation
- Determine whether the process is at all working
- Get an estimated time of completion
How to install tqdm in Python?
In order to install tqdm and time libraries, open your Jupiter Notebook and enter
!pip install tqdm
!pip install time
Then, restart the kernel to complete the installation process. After that, you need to import the tqdm and time libraries that you just installed, enter
from tqdm import tqdm
Using tqdm() in Python with a for loop
We can use the tqdm() with a for loop where the code runs with a predetermined delay after each iteration. The progress bar reaches 100% when all the iterations are done. We can also add a description to the progress bar as a prefix. Let’s see how it’s done.
To create a progress bar for 30 iterations, enter
for i in tq(range(30)):
In the code above, i denotes the values between 0 to 29 during each iteration. The code also implies that the system will sleep for 0.5 seconds between executing two iterations. The executed code gives you a progress bar along with the total number of iterations (30/30 in this case) and the time elapsed during each iteration.
As we mentioned earlier, you can also add a description as a prefix to the progress bar just by modifying the code a little:
from tqdm import tqdm
for i in tqdm(range(30), desc = ‘tqdm() Progress Bar’):
In this case, the progress bar will be prefixed by tqdm() Progress Bar
How does the tqdm_notebook() work?
The tqdm_notebook() works a little differently from the tqdm(). The former gives a color-coded progress bar. The tqdm_notebook() has three default colors – blue to signify that the process is in progress, green to signify completion of the process, and red to denote that the process is halted, paused, or cancelled midway.
The code for using tqdm_notebook is pretty similar to that of tqdm(). For the same loop that we used as an example earlier, the code in tqdm_notebook() would be
from tqdm.notebook import tqdm_notebook
for i in tqdm_notebook(range(30)):
The executed code will show a moving blue bar while the iterations are in progress and a stable green bar when the process is completed.
How is using tqdm_notebook() different from using tqdm()
We have already discussed the addition of colors in tqdm_notebook, but there is something else too.
In tqdm() each iteration of a single loop is represented by a separate progress bar. In tqdm_notebook() you get just one progress bar for each loop. That makes the progress bars more intuitive and makes it easier for you to estimate the completion time.
Is tqdm used for training machine learning algorithms?
Yes, the tqdm and tqdm_notebook modules are actively used in machine learning. It lets you create progress bars and configure it to track certain metrics while the machine-learning experiments run.
A quick recap
tqdm is an external library that you can use in Python for creating and configuring progress bars. You can also integrate it with the Pandas library to add progress bars to your data science projects.
It is useful because a) it looks good b) it gives you an effective visual representation of progress c) it helps you estimate the time of completion pretty accurately.
The tqdm_notebook() module is used for creating color-coded progress bars. We are quite familiar with the default colors – blue, red, and green – on a tqdm_notebook() progress bar. They are a bit easier to use and interpret than the tqdm() module.
You can wrap the tqdm modules around any iteration quite easily by using the tqdm(iterable) format. It’s an easy-to-use and effective tool for a range of tasks, from data scraping to designing neural networks.
Python is one of the most popular languages to ever exist primarily because of its libraries. There are very few things for which there isn’t a Python library. The best part is that you can get access to most of these with a simple pip command. tqdm is just one such library that developers use abundantly. It proves to be useful in many different situations. Congratulations, now you know how to use it.