alive-progress进度条可视化你的项目过程

Posted K'illCode

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了alive-progress进度条可视化你的项目过程相关的知识,希望对你有一定的参考价值。

只需使用pip安装:

$ pip install alive-progress

唤醒它

打开这样的上下文管理器:

fromalive_progressimportalive_baritems=range(1000)# retrieve your set of itemswithalive_bar(len(items))asbar:# declare your expected totalforiteminitems:# iterate as usual# process each itembar()# call after consuming one item

效果展示


在上图中,我对中间部分情况进行了省略,一开始本来打算对测试用的8张图片做一个动态进度条,结果效果并不理想,大概如下

 

|方  | 1/8 in 40s
 
|方方 | 2/8 in 40s
 
...
 
|方方方方方方方 | 7/8 in 40s
40多秒一显示,完全没有视觉效果,所以便改成如上图所示的效果,个人感觉还是蛮不错的。

 

以下是包里集成的全部效果,图片来源自作者Github地址

 

具体实现

from alive_progress import alive_bar
import time
 
def alive_progress_show(input_path):
	name_count = 0
   
	for input in input_path:
		img = cv2.imread(input)
 
		# 输出图片位次
		print("Image " + str(name_count) + " proceeding: ")
        # 可视化进度条
		with alive_bar(3000) as bar:
			for i in range(img.shape[0]):
				for j in range(img.shape[1]):
					# 自定义操作
				bar()
 
		name_count += 1


以上是我具体的实现,下面简单解释一下alive-progress包的使用,该包通过 with alive_bar(len(进度条)) as bar: 使用,当进度增长1的时候,使用bar()函数即可,可以通过bar(text='message')命令输出对应的message。

以上实现了一个嵌套的进度条,还有想进阶了解的请看作者的README,其config功能非常强大。
 

以上是关于alive-progress进度条可视化你的项目过程的主要内容,如果未能解决你的问题,请参考以下文章