Python教程-Python tqdm 模块 | 如何创建终端进度条
每当我们安装任何 Python 库、模块或安装软件时,都会在屏幕上看到进度条,表示小进度条,它估计了进程完成或渲染需要多长时间。它给我们一种活动的印象,可以使人放心。我们都熟悉各种不同的进度条。有些吸引人,有些则比较无聊。
在本文中,我们将创建进度条,而不涉及 Python 的核心日志库。
进度条根据完成任务所需进度的百分比填充。进度可以通过将 已处理的项目数 除以 总输入项目数 来计算。各种因素会影响进度条,例如网络速度、延迟,以及将持久数据写入本地存储以获得更准确的预计到达时间(ETA)。
我们可以使用名为 tqdm 的 Python 外部库创建简单而无麻烦的进度条。我们可以将其添加到代码中,使其看起来更漂亮。
tqdm 代表阿拉伯语中的 taqadum,意思是进度。Python tqdm 模块可以在各种平台上工作,如 Linux、Windows、Mac 等,它还与 IPython/Jupyter 笔记本兼容。
进度条的需求
如果我们处理较小的数据集,进度不会影响我们的工作流程。然而,进度条可以用于迭代数据集、训练模型或编码大量信息集。
- 进度条为我们提供了进程的估计,给出了它可能需要更多时间的时间近似值。
- 它为我们提供了进度仍在运行并且没有被粗鲁地终止的信息。
先决条件
系统必须安装 Python 3,并且我们还可以创建一个虚拟环境来安装 tqdm 库。
安装
打开命令行终端,输入以下内容。
pip install tqdm
或
pip3 install tqdm
以上命令将成功在系统中安装 tqdm。我们可以使用以下语句验证它。
import tqdm
如果没有错误,这意味着该库已成功安装。
为循环添加进度条
程序
import time
import sys
from tqdm import trange
def do_something():
time.sleep(1)
def do_another_something():
time.sleep(1)
for i in trange(10, file=sys.stdout, desc='outer loop'):
do_something()
for j in trange(100,file=sys.stdout, leave=False, unit_scale=True, desc='inner loop'):
do_another_something()
输出:
outer loop: 0%| | 0/10 [00:00<?, ?it/s]
inner loop: 0%| | 0.00/100 [00:00<?, ?it/s]
inner loop: 1%| | 1.00/100 [00:01<01:40, 1.02s/it]
inner loop: 2%|▏ | 2.00/100 [00:02<01:39, 1.02s/it]
inner loop: 3%|▎ | 3.00/100 [00:03<01:38, 1.01s/it]
inner loop: 4%|▍ | 4.00/100 [00:04<01:37, 1.01s/it]
inner loop: 5%|▌ | 5.00/100 [00:05<01:35, 1.01s/it]
inner loop: 6%|▌ | 6.00/100 [00:06<01:34, 1.01s/it]
inner loop: 7%|▋ | 7.00/100 [00:07<01:34, 1.01s/it]
inner loop: 8%|▊ | 8.00/100 [00:08<01:33, 1.02s/it]
inner loop: 9%|▉ | 9.00/100 [00:09<01:32, 1.02s/it]
inner loop: 10%|█ | 10.0/100 [00:10<01:31, 1.02s/it]
inner loop: 11%|█ | 11.0/100 [00:11<01:30, 1.01s/it]
inner loop: 12%|█▏ | 12.0/100 [00:12<01:29, 1.01s/it]
inner loop: 13%|█▎ | 13.0/100 [00:13<01:28, 1.01s/it]
inner loop: 14%|█▍ | 14.0/100 [00:14<01:29, 1.04s/it]
inner loop: 15%|█▌ | 15.0/100 [00:15<01:27, 1.03s/it]
inner loop: 16%|█▌ | 16.0/100 [00:16<01:26, 1.02s/it]
inner loop: 17%|█▋ | 17.0/100 [00:17<01:24, 1.02s/it]
inner loop: 18%|█▊ | 18.0/100 [00:18<01:23, 1.02s/it]
inner loop: 19%|█▉ | 19.0/100 [00:19<01:22, 1.02s/it]
…………………………………………………………………………………………….
……………………………………………………………………………………………..
It will go till it reached at 100%.
我们再了解一个例子。
示例-1
# importing modules
from tqdm import trange
from time import sleep
# creating loop
for i in trange(10, desc="loop "):
# slowing the for loop
sleep(0.1)
输出:
loop : 100%|██████████| 10/10 [00:01<00:00, 9.08it/s]
示例 -2
# importing modules
from tqdm import tnrange
from time import sleep
# creating loop
for i in tnrange(2, dec="loop 1"):
# creating nested loop
for j in tnrange(5, dec="loop 2"):
# slowing the for loop
sleep(0.3)
输出:
outer loop: 0%| | 0/10 [00:00<?, ?it/s]
inner loop: 0%| | 0.00/100 [00:00<?, ?it/s]
inner loop: 1%| | 1.00/100 [00:01<01:39, 1.00s/it]
inner loop: 2%|▏ | 2.00/100 [00:02<01:38, 1.00s/it]
inner loop: 3%|▎ | 3.00/100 [00:03<01:37, 1.00s/it]
inner loop: 4%|▍ | 4.00/100 [00:04<01:36, 1.00s/it]
inner loop: 5%|▌ | 5.00/100 [00:05<01:35, 1.00s/it]
inner loop: 6%|▌ | 6.00/100 [00:06<01:34, 1.01s/it]
inner loop: 7%|▋ | 7.00/100 [00:07<01:33, 1.01s/it]
inner loop: 8%|▊ | 8.00/100 [00:08<01:32, 1.01s/it]
inner loop: 9%|▉ | 9.00/100 [00:09<01:31, 1.00s/it]
inner loop: 10%|█ | 10.0/100 [00:10<01:30, 1.01s/it]
示例 -3
# importing modules
import time
import sys
from tqdm import trange
# creating random function
def random_function():
time.sleep(0.5)
# another random function
def another_random_function():
time.sleep(0.2)
# defining outer loop
for i in trange(3, file=sys.stdout, desc='Outer loop'):
random_function()
# inner loop
for j in trange(5, file=sys.stdout, desc='Inner loop'):
another_random_function()
输出:
Outer loop: 0%| | 0/3 [00:00<?, ?it/s]
Inner loop: 0%| | 0/5 [00:00<?, ?it/s]
Inner loop: 20%|██ | 1/5 [00:00<00:00, 4.62it/s]
Inner loop: 40%|████ | 2/5 [00:00<00:00, 4.64it/s]
Inner loop: 60%|██████ | 3/5 [00:00<00:00, 4.63it/s]
Inner loop: 80%|████████ | 4/5 [00:00<00:00, 4.65it/s]
Outer loop: 33%|███▎ | 1/3 [00:01<00:03, 1.58s/it]
Inner loop: 0%| | 0/5 [00:00<?, ?it/s]
Inner loop: 20%|██ | 1/5 [00:00<00:00, 4.91it/s]
Inner loop: 40%|████ | 2/5 [00:00<00:00, 4.85it/s]
Inner loop: 60%|██████ | 3/5 [00:00<00:00, 4.79it/s]
Inner loop: 80%|████████ | 4/5 [00:00<00:00, 4.77it/s]
Outer loop: 67%|██████▋ | 2/3 [00:03<00:01, 1.58s/it]
Inner loop: 0%| | 0/5 [00:00<?, ?it/s]
Inner loop: 20%|██ | 1/5 [00:00<00:00, 4.43it/s]
Inner loop: 40%|████ | 2/5 [00:00<00:00, 4.56it/s]
Inner loop: 60%|██████ | 3/5 [00:00<00:00, 4.65it/s]
Inner loop: 80%|████████ | 4/5 [00:00<00:00, 4.72it/s]
Outer loop: 100%|██████████| 3/3 [00:04<00:00, 1.57s/it]
预测性手动更新进度条
tqdm 模块提供了手动在特定间隔更新进度条的功能。当我们以多部分方式下载文件或处理流数据时,可以手动更新进度条函数。让我们了解以下示例。
示例 -
# importing modules
import time
import sys
from tqdm import tqdm
def task():
time.sleep(1)
with tqdm(total=100, file=sys.stdout) as pbar:
for i in range(10):
task()
# Here we are updating progress bar manually, useful for streams such as reading files.
pbar.update(10)
# Updates in increments of 10 stops at 100
输出:
100%|██████████| 100/100 [00:10<00:00, 9.93it/s]
解释:
在上面的代码中,我们设置了总数属性为100。调用的函数在每次迭代中递增十次,直到达到100%。我们可以将任何值传递给 update()
方法。
多线程进度条
我们还可以将 Python tqdm 包嵌套到 Python 线程中。多进程是使用所有核心的最佳方式。tqdm 的位置参数允许我们指定要打印此进度条的行偏移。在未编写脚本的情况下,默认设置为自动。让我们了解以下示例。必须指定该参数以管理多个进度条。如果忽略此参数,我们的进度条将被不同线程覆盖。
示例 -
import time
from random import randrange
from multiprocessing.pool import ThreadPool
from tqdm import tqdm
def func_call(position, total):
text = 'progressbar #{position}'.format(position=position)
with tqdm(total=total, position=position, desc=text) as progress:
for _ in range(0, total, 5):
progress.update(5)
time.sleep(randrange(3))
pool = ThreadPool(10)
tasks = range(5)
for i, url in enumerate(tasks, 1):
pool.apply_async(func_call, args=(i, 100))
pool.close()
pool.join()
输出:
progressbar #5: 0%| | 0/100 [00:00<?, ?it/s]
progressbar #2: 0%| | 0/100 [00:00<?, ?it/s]
progressbar #1: 0%| | 0/100 [00:00<?, ?it/s]
progressbar #4: 0%| | 0/100 [00:00<?, ?it/s]
progressbar #3: 0%| | 0/100 [00:00<?, ?it/s]
progressbar #3: 10%|█ | 10/100 [00:01<00:09, 9.91it/s]
progressbar #5: 10%|█ | 10/100 [00:01<00:09, 9.91it/s]
progressbar #1: 10%|█ | 10/100 [00:02<00:18, 4.97it/s]
progressbar #2: 10%|█ | 10/100 [00:02<00:18, 4.96it/s]
progressbar #4: 10%|█ | 10/100 [00:02<00:18, 4.96it/s]
progressbar #5: 15%|█▌ | 15/100 [00:02<00:11, 7.64it/s]
progressbar #3: 15%|█▌ | 15/100 [00:02<00:11, 7.64it/s]
progressbar #4: 15%|█▌ | 15/100 [00:03<00:17, 4.96it/s]
progressbar #2: 15%|█▌ | 15/100 [00:03<00:17, 4.95it/s]
progressbar #3: 20%|██ | 20/100 [00:04<00:16, 4.71it/s]
progressbar #5: 25%|██▌ | 25/100 [00:04<00:11, 6.58it/s]
progressbar #1: 20%|██ | 20/100 [00:04<00:16, 4.97it/s]
progressbar #2: 20%|██ | 20/100 [00:04<00:16, 4.96it/s]
在 Tqdm 进度条中添加颜色
颜色可以使进度条变得非常吸引人。但是,它不会为进度条的工作方式添加任何新功能。tqdm 可以与 Colorama 一起使用,Colorama 是 Python 中的简单跨平台颜色终端文本。让我们了解以下示例。
示例 -
from tqdm import trange
from colorama import Fore
# Cross-platform colored terminal text.
color_bars = [Fore.BLACK,
Fore.RED,
Fore.GREEN,
Fore.YELLOW,
Fore.BLUE,
Fore.MAGENTA,
Fore.CYAN,
Fore.WHITE]
for color in color_bars:
for i in trange(int(7e7),
bar_format="{l_bar}%s{bar}%s{r_bar}" % (color, Fore.RESET)):
pass
输出:
结论
我们已经讨论了与进度条相关的所有基本概念。Python 自带了 tqdm 模块,可以帮助我们手动设计进度条。我们为重要操作定义了合适的示例,可以在 tqdm 模块中执行这些操作。tqdm 模块可以与子进程和线程协作,我们可以同时运行多个进度条。