现在,alive-progress来了。是Python下的一个进度条库。不仅简单易用还支持多种炫酷显示效果!我们先来看看示例效果:一起来玩转这个库吧!1.Python下安装,使用pip安装:pipinstallalive-progress2.快速入门2.1直接使用循环使用alive-progress是最常见的用法,脚本可以这样写:#Importalive-progresslibraryfromalive_progressimportalive_barimporttime#使用with语句创建进度条withalive_bar(100)asbar:#将进度条总数输入alive_bar(此处为100)foriteminrange(100):#等待1stime.sleep(.1)#更新进度条,progress+1bar()请注意,如果动画不能正常显示,尝试在alive_bar中加入参数force_tty=True。运行上面的代码,我们可以看到终端中出现了一个相当华丽的动态进度条:需要注意的是alive-progress不会像tqdm等进度条库那样自动更新,只有当我们的程序调用bar时才会让Progressbar+1当然我们也可以不给进度条传递总数这个参数。这时候进度条不会显示进度,进入未定义模式:有时候我们想直接操作显示的位置。这时候我们可以设置alive_bar的manual参数为True:fromalive_progressimportalive_barimporttimetotal=100withalive_bar(total,manual=True)asbar:#total不能指定,此时只能指定百分比bar(0.5)#progressto50%time。睡眠(0.5)bar(0.1)#progressto10%time.sleep(0.5)bar(0.75)#progressto75%time.sleep(0.5)bar(1.0)#progressto100%time.sleep(0.5)bar(10)#progressto1000%foriinrange(1,101):bar(i/100)#设置进度为i%time.sleep(0.05)当然我们在运行过程中还需要输出一些提示信息,直接使用print在不破坏进度条的情况下输出一行提示信息,text方法可以在进度条末尾添加后缀字符,title参数可以为进度条添加标题(前缀信息)。具体用法和效果如下:fromalive_progressimportalive_barimporttime#定义标题(前缀字符)为HelloGitHubwithalive_bar(10,title="HelloGitHub")asbar:foriinrange(10):time.sleep(1)bar()#让进度+1bar.text("DoningWork#%d"%(i+1))#更新进度条后缀print("Work#%dfinished"%i)#输出一行信息2.2加点技巧看了太多传统进度条样式,想改变模式?没问题,alive-progress不仅内置了多种进度条样式,还支持自定义格式。进度条可以自定义两种样式:bar和spinner,调用alive_bar时只需要传入相应的参数即可。以这个进度条为例,中间最长的是进度条,旁边来回晃动的www.HelloGitHub.com是微调器。alive-progress内置了多种bar和spinner样式,你只需要调用show_bars或show_spinners即可快速预览对应的样式,例如:fromalive_progressimportshow_barsshow_bars()#查看内置bar样式fromalive_progressimportshow_spinnersshow_spinners()#查看内置-在spinnerstyles中默认的样式使用起来很简单比如我要使用气泡的bar和message_scrolling的spinner,直接传入对应的name即可:fromalive_progressimportalive_barimporttime#直接传入对应的name和alive_bar(100,title="HelloGitHub",bar="bubbles",spinner="message_scrolling")asbar:foriinrange(100):time.sleep(.1)bar()如果你不知道总数,你可以使用未知参数(本例将bar替换为spinner):fromalive_progressimportalive_barimporttimewithalive_bar(title="HelloGitHub",#注:此处bar替换为unknown,内置样式名称与spinne相同runknown="stars",spinner="message_scrolling")asbar:foriinrange(100):time.sleep(.1)bar()三、私人定制可能比直接使用内置模板更喜欢自定义progressbar自己实现,alive-progress也为此提供了相应的方法。3.1自定义bar可以使用standard_bar_factory方法快速自定义bar。bar可以设置的参数有5个:chars:该单元的动画正在执行中,会根据进度依次显示。borders:进度条边框,显示在左右两侧。背景:本机显示的内容不执行。tip:执行单元的前导符号。errors:出现错误时显示的字符(进度未完成,超过总值等)。比如我们要制作如图所示的bar:我们可以这样写:fromalive_progressimportalive_bar,standard_bar_factoryimporttime##------自定义bar------##my_bar=standard_bar_factory(#后面的参数都是有默认值的,不用一下子全部修改chars="123456789#",#加载时会根据进度依次显示,长度为任意边框="<>",#Border边框在#barbackground两端=".",#对未加载的部分使用"。"Filltip=">",#Guidingsymbols表示前进的方向(分隔“#”and".")errors="??"#发生错误时显示的内容(未完成,溢出))##-------自定义结束------####-------动画演示--------##withalive_bar(10,title="HelloGitHub",bar=my_bar,#引入新定制的barspinner="message_scrolling",manual=True)asbar:foriinrange(50):time.sleep(.1)bar(i/100)bar(.5)time.sleep(2)bar(10)print("overflow")time.sleep(1)bar(1)print("100%complete")time.sleep(1)bar(.1)print("notcompleted")3.2自定义spinner对于spinner,alive-progress提供了更多的动画定义方法:frame_spinner_factory:将传入的字符串一一输出:fromalive_progressimportalive_bar,frame_spinner_factoryimporttimemy_spinner=my_spinner=frame_spinner_factory(r'-----',r'1----',r'-2---',r'--3--',r'---4-',r'----5')#直接传入字符串withalive_bar(title="HelloGitHub",spinner=my_spinner)asbar:whileTrue:bar()time.sleep(.1)可以看到字符串是循环输出的scrolling_spinner_factory(chars="HelloGitHub",#你要播放的字符串length=15,#spinnerareawidthblank='.'#空白部分填充字符)withalive_bar(title="HelloGitHub",spinner=my_spinner)asbar:whileTrue:bar()time.sleep(.1)bouncing_spinner_factory:交替滚动两个字符串广播fromalive_progressimportalive_bar,bouncing_spinner_factoryimporttimemy_spinner=bouncing_spinner_factory(right_chars="Ilove",#stringlength=15fromtheleft,#spinnerarealengthleft_chars="HelloGitHub",#从右边输入的字符串blank='.',#空白区域填充字符)withalive_bar(title="HelloGitHub",spinner=my_spinner)asbar:whileTrue:bar()time.sleep(.1)当然left_chars参数也可以省略,效果就相当于我爱会像弹球一样左右弹跳。unknown_bar_factory:将spinner转换为能使用在未定义模式中的格式:fromalive_progressimportalive_bar,unknown_bar_factory,bouncing_spinner_factoryimporttimemy_spinner=bouncing_spinner_factory("www.HelloGitHub.com",15,hiding=False)my_unknown_bar=unknown_bar_factory(my_spinner)#传入定义的spinnerwithalive_bar(title="HelloGitHub",unknown=my_unknown_bar)asbar:whileTrue:bar()time.sleep(.1)4、最后相信你已经掌握了alive_progress的基本玩法。alive-progress还提供了一些有兴趣的朋友可以通过阅读官方文档或源码了解更多场合需要的特殊功能。
