Python 进度条

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之外,还有其他第三方库,比如 progressbar2alive_progress ,它们提供了额外的功能和自定义选项来创建进度条。

progressbar2

progressbar2 是另一个流行的库,提供了多种进度条样式和选项。使用progressbar2库首先需要使用 pip 安装。

pip install progressbar2

示例

在这里,我们使用小部件列表创建了一个具有自定义小部件的进度条。我们使用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

活动进度

活动进度是一个现代化、功能丰富的库,可用于创建具有高级自定义选项的交互式进度条。要使用活动进度,首先我们需要使用 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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程