Python 中的进度条
进度条在 Python 中是可视化的指示器,用于提供任务或操作进度的反馈。它们对于长时间运行的进程或迭代非常有用,因为它们可以显示已完成的工作量以及剩余的工作量。
进度条通常包含一个可视化表示,例如水平条或文本表示,它会动态更新以反映任务的进度。它还包括其他信息,如完成百分比、剩余估计时间以及任何相关的消息或标签。
以下是 Python 中进度条所起的作用。
视觉反馈
时间估计
用户体验
以下是一些在 Python 中实现进度条的方法。让我们详细了解每种方法。
使用 tqdm 库
tqdm 库是 Python 中创建进度条的常用选择。它提供了一个简单灵活的 API,只需少量代码即可创建进度条。tqdm 库会根据可迭代对象的长度自动计算并显示循环的进度。它还提供了其他功能,如经过时间、剩余估计时间以及可自定义的外观。
要使用 tqdm,我们需要先使用 Python 环境中的 pip 安装它。
示例
pip install tqdm
输出
F:\>pip install tqdm Defaulting to user installation because normal site-packages is not writeable Collecting tqdm Downloading tqdm-4.65.0-py3-none-any.whl (77 kB) ---------------------------------------- 77.1/77.1 kB 1.1 MB/s eta 0:00:00 Requirement already satisfied: colorama in c:\users\krishna\appdata\roaming\python\python311\site-packages (from tqdm) (0.4.6) Installing collected packages: tqdm WARNING: The script tqdm.exe is installed in 'C:\Users\Krishna\AppData\Roaming\Python\Python311\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed tqdm-4.65.0
示例
在这个示例中,我们迭代数据范围,tqdm 包装了循环,并在每次迭代完成后实时显示一个更新的进度条。time.sleep(0.5) 行模拟了每次迭代中完成的一些工作。
from tqdm import tqdm import time data = range(10) for item in tqdm(data): time.sleep(0.5)
输出
100%|██████████████████████████████████████████| 10/10 [00:05<00:00, 1.99it/s]
手动更新进度条
在这种方法中,我们手动计算进度百分比,并通过使用回车符 (\r) 字符打印到控制台来更新进度条,以覆盖前一行。以下是使用 print 语句手动更新进度条的示例。
示例
import time
total_iterations = 10
for i in range(total_iterations):
time.sleep(0.5)
progress = (i + 1) / total_iterations * 100
print(f"Progress: {progress:.1f}%", end="\r")
输出
Progress: 10.0% Progress: 20.0% Progress: 30.0% Progress: 40.0% Progress: 50.0% Progress: 60.0% Progress: 70.0% Progress: 80.0% Progress: 90.0% Progress: 100.0%
使用第三方库
除了 tqdm 之外,还有其他第三方库,如 progressbar2 和 alive_progress,它们为进度条提供了更多功能和自定义选项。
progressbar2
progressbar2 是另一个流行的库,它提供了各种进度条样式和选项。要使用 progressbar2 库,我们首先必须使用 pip 安装它。
pip install progressbar2
示例
在这里,我们使用 widgets 列表创建了一个带有自定义部件的进度条。我们使用 max_value 参数将进度条的最大值指定为 10。bar.update(i + 1) 行更新每次迭代的进度条。
from progressbar import ProgressBar
import time
total_iterations = 10
with ProgressBar(max_value=total_iterations) as bar:
for i in range(total_iterations):
time.sleep(0.5)
bar.update(i)
输出
100% (10 of 10) |########################| Elapsed Time: 0:00:04 Time: 0:00:04
Alive-progress
Alive-progress 是一个现代的、功能丰富的库,用于创建具有高级自定义选项的交互式进度条。要使用 alive-progress,首先必须使用 pip 安装库。
pip install alive-progress
Defaulting to user installation because normal site-packages is not writeable
Collecting alive-progress
Downloading alive_progress-3.1.4-py3-none-any.whl (75 kB)
-------------------------------------- 75.9/75.9 kB 842.2 kB/s eta 0:00:00
Collecting about-time==4.2.1 (from alive-progress)
Downloading about_time-4.2.1-py3-none-any.whl (13 kB)
Collecting grapheme==0.6.0 (from alive-progress)
Downloading grapheme-0.6.0.tar.gz (207 kB)
-------------------------------------- 207.3/207.3 kB 4.2 MB/s eta 0:00:00
Preparing metadata (setup.py) ... done
Building wheels for collected packages: grapheme
Building wheel for grapheme (setup.py) ... done
Successfully built grapheme
示例
在这个示例中,我们使用 alive_bar 上下文管理器来创建进度条。len(data) 指定迭代的总数。bar() 函数在循环中被调用以更新进度条。
from alive_progress import alive_bar
import time
data = range(10)
with alive_bar(len(data)) as bar:
for item in data:
time.sleep(0.5)
bar()
输出
|████████████████████████████████████████| 10/10 [100%] in 5.1s (1.91/s) ←[K ←[?25h←[J
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP