惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
D
DataBreaches.Net
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
腾讯CDC
博客园_首页
The Cloudflare Blog
S
SegmentFault 最新的问题
C
Check Point Blog
美团技术团队
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Tqdm Issues and Tips
2020-09-05 · via jdhao's digital space

Tqdm is a popular package to show a progress bar. Here are a few tips when using tqdm.

Windows display issue#

On Windows, it seems that tqdm will print a new line when showing the progress, which is annoying.

According to issue here, this is due to buggy Unicode handling in Windows terminal. We can add ascii=True in tqdm() to prevent this behavior. Works for me on Windows for both Cmder and plain cmd.

Iterate over each line in a file and show progress?#

When we are processing files with large number of lines, we may want to see the progress. To see the progress, we need to know the number of lines in this file. Here is a sample code to show the progress bar using tqdm:

from tqdm import tqdm

file_path = "test.txt"
num_lines = sum(1 for line in open(file_path, 'r'))

with open(file_path, 'r') as f:
    for line in tqdm(f, total=num_lines):
        # deal with this line...
        pass

Ref

Show progress for concurrent.futures.ThreadPoolExecutor#

Here is an example of using tqdm together with concurrent.futures:

from concurrent.futures import ThreadPoolExecutor
import time

from tqdm import tqdm


def square(x):
    time.sleep(0.01)
    return x*x


def main():
    nums = list(range(10000))
    with ThreadPoolExecutor() as executor:
        results = list(tqdm(executor.map(square, nums), total=len(nums)))

if __name__ == "__main__":
    main()

The output of executor.map(square, nums) is a generator containing all the returned results from the function square(). So we need to supply the total number in the total argument.

In the latest version of tqdm, we can also simplify the above code by using tqdm.contrib.concurrent.thread_map() method, which is essentially a wrapper around above code:

from concurrent.futures import ThreadPoolExecutor
import time

from tqdm.contrib.concurrent import thread_map


def square(x):
    time.sleep(0.01)
    return x*x


def main():
    nums = list(range(10000))
    with ThreadPoolExecutor() as executor:
        results = thread_map(square, nums)

if __name__ == "__main__":
    main()

The above code is tested on tqdm version 4.48.2.

Ref: