本文适宜有 Python 根底的敌人

本文作者:HelloGitHub-Anthony

HelloGitHub 推出的《解说开源我的项目》系列,本期介绍让你疾速领有完满进度条的 Python 三方库——alive-progress

我的项目地址:https://github.com/rsalmei/al...

不知你是否有过这样的经验:你写了一个程序,每次运行都会消耗很长时间。在期待程序运行期间你一次次的按下回车避免程序卡死。亦或者你的工作须要实时把握程序运行进度但你基本不晓得程序执行到了哪里...

当初,alive-progress 来了,它是一个 Python 下的进度条库,不仅使用方便而且反对多种炫酷显示成果!让咱们先来看看示例成果:

上面让咱们一起玩转这个库!

一、装置

在 Python 下应用 pip 进行装置:

pip install alive-progress

二、疾速入门

2.1 间接应用

在循环中应用 alive-progress 是最常见的用法,脚本能够这样写:

# 导入 alive-progress 库from alive_progress import alive_barimport time# 应用 with 语句创立一个进度条with alive_bar(100) as bar:    # 给 alive_bar 传入进度条总数目(这里是 100)    for item in range(100):        # 期待 1s        time.sleep(.1)        #更新进度条,进度 +1        bar()
请留神,如果无奈失常显示动画则尝试在 alive_bar 中加上 force_tty=True 参数。

运行以上代码咱们能够看到在终端中呈现了一个还算富丽的动静进度条:

须要留神的是 alive-progress 并不像 tqdm 等进度条库一样会自动更新,只有咱们程序调用了 bar 才会让进度条 +1.

当然,咱们也能够不给进度条传入总数目这个参数,此时进度条将不显示进度,并进入未定义模式:

有时候咱们想间接操纵显示的地位,这时候能够设定 alive_barmanual 参数为 True

from alive_progress import alive_barimport timetotal = 100with alive_bar(total, manual=True) as bar:    # total 能够不指定,这时候只有百分比    bar(0.5) # 进度到 50%    time.sleep(0.5)    bar(0.1) # 进度到 10%     time.sleep(0.5)    bar(0.75) # 进度到 75%    time.sleep(0.5)    bar(1.0) # 进度到 100%    time.sleep(0.5)    bar(10) # 进度到 1000%    for i in range(1,101):        bar(i/100) # 设定进度为 i%        time.sleep(0.05)

当然,在运行过程中咱们也须要输入一些提示信息,间接应用 print 能够在不毁坏进度条的状况下输入一行提示信息,text 办法则能够在进度条尾部增加后缀字符,而 title 参数则能够给进度条增加题目(前缀信息),具体应用办法及成果如下:

from alive_progress import alive_barimport time# 定义题目(前缀字符)为 HelloGitHubwith alive_bar(10, title="HelloGitHub") as bar:    for i in range(10):        time.sleep(1)        bar()   # 让进度 +1        bar.text("Doning Work #%d"%(i+1))   # 更新进度条后缀        print("Work #%d finished"%i)        # 输入一行信息

2.2 添点花色

看多了传统的进度条款式想换换花色?没问题,alive-progress 不仅内置了多种进度条款式,还反对自定义格局。

进度条能够自定义的款式分为两种:barspinner,只须要在调用 alive_bar 的时候传入对应的参数即可。

以这个进度条为例,两头最长的是 bar,旁边来回晃动的 www.HelloGitHub.comspinner

alive-progress 内置了多种 bar 和 spinner 款式,只须要调用 show_bars 或者 show_spinners 即可疾速预览相应的款式,例如:

from alive_progress import show_barsshow_bars() # 查看内置 bar 款式

from alive_progress import show_spinnersshow_spinners() # 查看内置 spinner 款式

默认款式应用起来非常简单,例如我想应用 bubbles 这个 bar 和 message_scrolling 这个 spinner,间接传入对应名称即可:

from alive_progress import alive_barimport time# 间接传入对应名字即可with alive_bar(            100,            title="HelloGitHub",             bar="bubbles", spinner="message_scrolling"            ) as bar:    for i in range(100):        time.sleep(.1)        bar()

如果不晓得 total 的数目,能够应用 unknown 参数(这时候将替换 bar 为 spinner):

from alive_progress import alive_barimport timewith alive_bar(            title="HelloGitHub",             # 留神:这里 bar 被换成了unknow,内置款式名称与 spinner 的雷同            unknown="stars", spinner="message_scrolling"            ) as bar:    for i in range(100):        time.sleep(.1)        bar()

三、私人定制

或者比起间接应用内置模板你更喜爱本人定制的进度条,对此 alive-progress 也提供了对应办法。

3.1 定制 bar

应用 standard_bar_factory 办法能够疾速定制 bar,bar 能够设置的参数有五个:

  • chars:正在执行单元的动画,依照进度顺次显示。
  • borders:进度条边界,显示在左右两边。
  • background:未执行到单元显示的内容。
  • tip:执行单元的前导符号。
  • errors:出错时(进度未走全,超出 total 值等)时显示的字符。

例如咱们想做一个如图所示的 bar:

则能够这样来写:

from alive_progress import alive_bar, standard_bar_factoryimport time##-------自定义 bar-------##my_bar = standard_bar_factory(    # 以下参数均有默认值,不用一次全副批改                            chars="123456789#", # 加载时依据进度顺次显示,长度任意                            borders="<>",        # bar 中间的边界                            background=".",        # 未加载局部用 "." 填充                            tip=">",            # 批示进度方向的疏导符号(宰割 "#" 与 ".")                            errors="⚠❌" # 产生谬误时显示的内容(未实现,溢出)                                )##-------自定义完结-------####--------动画演示-------##with alive_bar(            10,            title="HelloGitHub",             bar=my_bar, # 这里传入刚刚自定义的 bar            spinner="message_scrolling",            manual=True            ) as bar:    for i in range(50):        time.sleep(.1)        bar(i/100)    bar(.5)    time.sleep(2)    bar(10)    print("上溢")    time.sleep(1)    bar(1)    print("100% 实现")    time.sleep(1)    bar(.1)    print("未实现")

3.2 定制 spinner

对于 spinner,alive-progress 提供了更多种的动画定义形式:

frame_spinner_factory:将传入的字符串挨个输入:

from alive_progress import alive_bar, frame_spinner_factoryimport timemy_spinner = my_spinner = frame_spinner_factory(                                r'-----',                                r'1----',                                r'-2---',                                r'--3--',                                r'---4-',                                r'----5'                                )    # 间接传入字符串with alive_bar(            title="HelloGitHub",            spinner=my_spinner            ) as bar:    while True:        bar()        time.sleep(.1)

能够看到字符串挨个循环输入。

scrolling_spinner_factory:将字符串滚动播出

from alive_progress import alive_bar, scrolling_spinner_factoryimport timemy_spinner = scrolling_spinner_factory(                                    chars="HelloGitHub", # 想要播放的字符串                                    length=15,    # spinner 区域宽度                                    blank='.'    # 空白局部填充字符                                    )with alive_bar(            title="HelloGitHub",            spinner=my_spinner            ) as bar:    while True:        bar()        time.sleep(.1)

bouncing_spinner_factory:将两个字符串交替滚动播出

from alive_progress import alive_bar, bouncing_spinner_factoryimport timemy_spinner = bouncing_spinner_factory(                                    right_chars="I love", # 从右边进入的字符串                                    length=15, # spinner 区域长度                                    left_chars="HelloGitHub", # 从左边进入的字符串                                    blank='.',     # 空白区域填充字符                                    )with alive_bar(            title="HelloGitHub",            spinner=my_spinner            ) as bar:    while True:        bar()        time.sleep(.1)

当然,也能够省略 left_chars 这个参数,其成果相当于 I love 将会像弹球一样左右弹动。

unknown_bar_factory:将 spinner 转换为能应用在未定义模式中的格局:

from alive_progress import alive_bar, unknown_bar_factory, bouncing_spinner_factoryimport timemy_spinner = bouncing_spinner_factory("www.HelloGitHub.com",15,hiding=False)my_unknown_bar = unknown_bar_factory(my_spinner)    # 传入定义的 spinnerwith alive_bar(            title="HelloGitHub",            unknown=my_unknown_bar            ) as bar:    while True:        bar()        time.sleep(.1)

四、结尾

到这里,置信你曾经把握了 alive_progress 的根本玩法,alive-progress 还提供了一些在不同场合所需的非凡性能,有趣味的敌人能够通过浏览官网文档或源代码进行更加深刻的理解。本次的内容就到这里了,快去创立一个属于本人的进度条吧!


关注 HelloGitHub 公众号 收到第一工夫的更新。

还有更多开源我的项目的介绍和宝藏我的项目期待你的挖掘。