12

2019年的代码都写完了吗?不如做个Python进度条看看还剩多少

 4 years ago
source link: https://www.jiqizhixin.com/articles/2019-12-30-7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

vm2YNvA.gif

如果你之前没用过进度条,八成是觉得它会增加不必要的复杂性或者很难维护,其实不然。要加一个进度条其实只需要几行代码。在这几行代码中,我们可以看看如何在命令行脚本以及 PySimpleGUI UI 中添加进度条。

下文将介绍 4 个常用的 Python 进度条库:

Progress

第一个要介绍的 Python 库是 Progress。

你只需要定义迭代的次数、进度条类型并在每次迭代时告知进度条。

<code>import timefrom progress.bar </code><code>import IncrementalBarmylist = [1,2,3,4,5,6,7,8]</code><code>bar = IncrementalBar('Countdown', max = len(mylist))</code><code>for item in mylist: </code><code>  bar.next() </code><code>  time.sleep(1) </code><code>  bar.finish()</code>

Progress 实现的进度条效果如下:

mumeEjz.gif

Progressbar 的增量进度条

如果你不喜欢该进度条的格式,还可以从以下格式中挑选:

b2QRzm6.gif

ProgressBar 支持的进度条类型。

相关文档:https://pypi.org/project/progress/1.5/

tqdm

下面我们看一下 tqdm 库。

和之前见过的库差不多,这两行代码也非常相似,在设置方面有一点点不同:

<code>import timefrom tqdm </code><code>import tqdm</code>
<code>mylist = [1,2,3,4,5,6,7,8]</code>
<code>for i in tqdm(mylist): </code><code>  </code>
<code>    time.sleep(1)</code>

tqdm 实现的进度条效果如下:

3AF3Qbu.gif

这个进度条也提供了几种选项。

相关文档:https://tqdm.github.io/

Alive Progress

顾名思义,这个库可以使得进度条变得生动起来,它比原来我们见过的进度条多了一些动画效果。

YRJ3iyr.gif

从代码角度来说比较相似:

<code>from alive_progress import alive_bar</code>
<code>import </code><code>time</code>
<code>mylist = [1,2,3,4,5,6,7,8]</code>
<code>with alive_bar(len(mylist)) as bar: </code><code>  </code>
<code>    for i in mylist: </code><code>    </code>
<code>    bar() </code><code>    </code>
<code>    time.sleep(1)</code>

进度条的外观和预期差不多:

UVf2MzV.gif

这种进度条有一些与众不同的功能,使用起来会比较有趣,功能详情可见项目:https://github.com/rsalmei/alive-progress

PySimpleGUI

用 PySimpleGUI 得到图形化进度条

我们可以加一行简单的代码,在命令行脚本中得到图形化进度条。

r2mIVnq.gif

为了实现上述内容,我们需要的代码是:

<code>import PySimpleGUI as sg</code>
<code>import </code><code>timemylist = [1,2,3,4,5,6,7,8]</code>
<code>for i, item in enumerate(mylist): </code><code>  </code>
<code>    sg.one_line_progress_meter('This is my progress meter!', i+1, len(mylist), '-key-') </code><code>  </code>
<code>    time.sleep(1)</code>

PySimpleGUI 应用程序中的进度条

项目作者之前曾经在 GitHub 上讨论过「如何快速启动 Python UI,然后使用 UI 创建比较工具」。在这个项目里,作者还讨论了一下如何集成进度条。

eqeQZzz.gif

代码如下:

<code>import PySimpleGUI as sg</code>
<code>import </code><code>timemylist = [1,2,3,4,5,6,7,8]</code>
<code>progressbar = [ [sg.ProgressBar(len(mylist), orientation='h', size=(51, 10), key='progressbar')]]</code>
<code>outputwin = [ [sg.Output(size=(78,20))]]</code>
<code>layout = [ [sg.Frame('Progress',layout= progressbar)], [sg.Frame('Output', layout = outputwin)], [sg.Submit('Start'),sg.Cancel()]]</code>
<code>window = sg.Window('Custom Progress Meter', layout)</code>
<code>progress_bar = window['progressbar'] while True: </code><code> </code>
<code>    event, values = window.read(timeout=10) </code><code>  </code>
<code>    if event == 'Cancel' or event is None: </code><code>    </code>
<code>        break </code><code>  elif event == 'Start': </code><code>    </code>
<code>    for i,item in enumerate(mylist): </code><code>      </code>
<code>        print(item) </code><code>      </code>
<code>        time.sleep(1) </code><code>      </code>
<code>    progress_bar.UpdateBar(i + 1)window.close()</code>

没错,在 Python 脚本中使用进度条只需要几行代码,一点也不复杂。有了进度条,以后也不用再猜测脚本运行地怎么样了。

参考链接:

https://towardsdatascience.com/learning-to-use-progress-bars-in-python-2dc436de81e5


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK