A Brief Introduction of the Tqdm Module in Python

DateAuthorVersionNote
2024.02.28Dog TaoV1.0Release the note.

A Brief Introduction of the Tqdm Module in Python

Introduction

Tqdm is a versatile Python library that provides a fast, extensible progress bar for loops and other iterable processes. The name tqdm is derived from the Arabic word “taqaddum” (تقدّم), meaning “progress,” and is pronounced as “ta-qe-dum.” Its simplicity and efficiency have made it a go-to choice for adding progress indicators to Python code, especially in data processing, file I/O, and long-running computations.

Key Features

  • Easy to Use: Tqdm can be added to your loops with minimal code changes, instantly providing visual feedback on the progress.
  • Highly Customizable: While simple to implement with default settings, tqdm also offers a wide range of customization options, including custom messages, progress bar formatting, and manual control over the progress updates.
  • Lightweight with Minimal Dependencies: It is designed to be lightweight and requires no heavy dependencies, making it suitable for various projects.
  • Versatile: Works with loops, iterable objects, and can even be used to track progress in pandas operations with tqdm.pandas().

Installation

  • Using pip

To install tqdm using pip, open your terminal (or command prompt/PowerShell in Windows) and run the following command:

pip install tqdm

If you are working in a virtual environment (which is recommended to avoid conflicts between different projects), make sure it is activated before running the pip install command.

  • Using conda

To install tqdm using conda, you should have Anaconda or Miniconda installed on your system. Open your Anaconda Prompt (or terminal in Linux/macOS) and run the following command:

conda install -c conda-forge tqdm

Using the -c conda-forge flag specifies that conda should install tqdm from the conda-forge channel, which is a community-maintained collection of conda packages.

Usage Examples

Basic Usage

The most common use of tqdm is to wrap it around any iterable in a for loop.

from tqdm import tqdm
import time

for i in tqdm(range(1000)):
    # Simulated task
    time.sleep(0.001)

The output example:

在这里插入图片描述

Advanced Usage

  • Customization: You can customize the progress bar with various parameters such as desc (description), total, leave, ncols (width), unit, and more.
for i in tqdm(range(100), desc="Loading", ascii=False, ncols=75):
    time.sleep(0.01)
  • Manual Updates: For tasks that don’t fit neatly into a loop, tqdm can be manually updated.
pbar = tqdm(total=100)
for i in range(10):
    time.sleep(0.1)
    pbar.update(10)  # Manually update the progress bar by 10
pbar.close()

The output example:

在这里插入图片描述

  • Integration with Pandas: Tqdm can be integrated with Pandas operations using tqdm.pandas(). This is particularly useful for applying functions to DataFrame columns or rows and visualizing the progress.
import pandas as pd
from tqdm import tqdm
tqdm.pandas()

df = pd.DataFrame({'x': range(10000)})
df['y'] = df['x'].progress_apply(lambda x: x**2)

The output example:

在这里插入图片描述

  • Working with Concurrent Futures: Tqdm can also be used with concurrent programming modules like concurrent.futures for tracking the progress of asynchronous tasks.
from concurrent.futures import ThreadPoolExecutor, as_completed

with ThreadPoolExecutor(max_workers=5) as executor:
    futures = [executor.submit(time.sleep, 0.1) for _ in range(100)]
    for f in tqdm(as_completed(futures), total=len(futures)):
        pass

The output example:

在这里插入图片描述

Tqdm’s simplicity, combined with its powerful features, makes it an invaluable tool for enhancing the user experience in command-line applications and Jupyter notebooks by providing clear and customizable progress indications.

  • 33
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全能骑士涛锅锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值