Python 中的进度条

pythonserver side programmingprogramming

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,还有其他第三方库,如 progressbar2alive_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


相关文章