Table of contents
1.
Introduction
2.
tqdm - Progress Bars for Python
2.1.
Introduction to tqdm library
2.2.
Important Parameters
2.3.
Hands-on implementation with tqdm
2.3.1.
Installation
2.3.2.
Importing Libraries
2.3.3.
Importing Notebooks
2.3.4.
Using the tqdm library with for loop
2.3.5.
Multiple Progress Bars
2.4.
Applications of tqdm
3.
Frequently Asked Questions
3.1.
What is the indentation in Python?
3.2.
Explain the dictionary in the context of Python.
3.3.
Describe lists.
3.4.
List the type of operators Python offers.
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

tqdm - Progress Bars for Python

Author Rupal Saluja
0 upvote

Introduction

Python is a popular programming language designed for general-purpose programming. It was developed, focusing mainly on code readability. It allows programmers to convey their idea in fewer lines of code. You can use Python for various basic purposes such as software development, web development, performing mathematical calculations, and shell scripting.

logo of Python

In this blog, we will learn how to produce progress bars using the tqdm library in Python. We will also discuss in brief about tqdm library and its applications.

tqdm - Progress Bars for Python

The word ‘tqdm’ comes from the Arabic word ‘Taqadum’, which means progress. Whenever you are executing any task, it feels good to see little progress. If you have a simple progress bar in your code or script to tell your progress or give you proper feedback, it eases your mind. This way, your hours of hard work won’t go in vain.

progress bar diagram

Introduction to tqdm library

The tqdm is a Python library that creates a small progress bar for the Python loops. You only have to wrap tqdm with any iterable to execute. These progress bars can be used for data processing, downloading, machine learning models, etc. It not only shows you how much time has passed but also shows you the estimated time remaining for the completion. 

Also see, Divmod in Python, Swapcase in Python

Important Parameters

Look into the table below for the several parameters offered along with the tqdm library.

Parameter

Description

Syntax

desc It specifies the description of your progress bar.  tqdm (self, iterable, desc= “Text You want”)
total It mentions the total number of expected iterations.  tqdm (self, iterable, total= 500)
disable It is used to disable the progress bar completely. tqdm (self, iterable, disable= true)
ncols It frames the entire width of the output message. It is dynamic to the window’s size. tqdm (self, iterable, ncols= 100)
mininterval You can change the minimum progress display using this parameter. The default is set to 0.1 seconds. tqdm (self, iterable, mininterval= 3)
ascii Using this parameter, you can use the ASCII characters to fill the progress bar. tqdm (self, iterable, ascii= “123456789$”)
unit The unit of time can be changed using this parameter. The default is set to “it”. tqdm (self, iterable, unit= “ticks”)
intial Using this parameter, you can change the initial value of the progress bar tqdm (self, iterable, initial= 5)

Hands-on implementation with tqdm

Now you are well versed with the several aspects of the tqdm library, we will move to the implementation part. 

Installation

The tqdm library is a part of the Python Package Index (PyPI). You can use the syntax below to install the library.

pip install tqdm

Importing Libraries

To import the tqdm library, start with the following line of code.

from tqdm import tqdm

Importing Notebooks

Suppose you want to export any notebooks such as IPython or Jupyter. In that case, you can import it while working with the tqdm library. You can use the syntax below for the same.

from tqdm.notebook import tqdm, trange

Using the tqdm library with for loop

You can use the syntax to look into the syntax of using the tqdm library with for loop.

for i in tqdm(range(1000)):
    pass

Multiple Progress Bars

Use the Python code below to create Multiple Progress Bars.

from tqdm.notebook import tqdm
for i in range(20, desc= ‘test’):
       sleep(0.01)
       for x in range(1000, desc= ‘t’):
             sleep(0.01)

Applications of tqdm

There can be various applications of the tqdm library. Some prominent ones are discussed further.

Data Science

Use the steps below to understand the concept better. Here, we will be using an example of a Hotel Booking dataset. We will integrate tqdm into the pandas dataframe and use progress_apply to apply functions to the dataframe with the progress bar.

1. We will load the dataset and display the top 5 rows using the following Python code.

import pandas as p
d = p.read_csv("bookings.csv")
df.head(5)

 

2. Create a new column name using the customer’s name.

3. Use the following code to generate progress bars for the pandas dataframe, add a bar with the label Processing the column, lower the string, replace space with ‘-’, and display the top two rows.

tqdm.pandas(desc="Processing the column")
def name(text):   
  return text.lower().replace(" ","-")
df['name'] = df['name'].progress_apply(name)
df.head(2)


Parallel Processing Utility

The tqdm library offers a utility tqdm.contrib.concurrent for parallel processing. Use the code below to extract email_provider from the email column. It will display the top five columns of the email_provider column.

from tqdm.contrib.concurrent import map
def extraction(email):
    return email.split("@")[1].split(".")[0]
df["provider"] = map(
    extraction,
    df["email"],
    workers=10,
    size=128,
    desc="Extracting provider",
    colour='green'
)
df["provider"].head().to_frame()

 

Read about Bitwise Operators in C here.

Practice this code with the help of Online Python Compiler

Frequently Asked Questions

What is the indentation in Python?

In most cases, compilers/interpreters must know how we must execute the statements in a code. As a result, we separate the code into numerous blocks and indent it. This indentation aids them in comprehending the order in which we should execute each block/statement.

Explain the dictionary in the context of Python.

Dictionaries are one of the fundamental data structures in Python. A dictionary can hold any number of objects, each identified by a distinct dictionary key.

Describe lists.

Lists in python are used to hold many things in a single variable. Lists are one of four built-in data types in Python; used to store data collections.

List the type of operators Python offers.

Python allows a variety of operators. These are Arithmetic Operators, Comparison Operators, Logical Operators, Bitwise Operators, Assignment Operators, Identity Operators, and Membership Operators.

Conclusion

On the whole, we understood the various concepts of the tqdm library of Python. This includes its introduction, its applications, and its implementation.

We hope the above discussion helped you learn the tqdm library of Python more clearly and can be used for future reference whenever needed. To learn more about the tqdm library and its components, you can refer to blogs on Python OperatorsIndentation in PythonRecursion in PythonLoops in Python, and Patterns in Python.

Also check out - Data Dictionary In Software Engineering

Python data analytics

 Python filename extensions

Visit our website to read more such blogs. Make sure you enroll in our courses, take mock tests, solve problems, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.

Happy Coding!

Live masterclass