• 给Python代码加上酷炫进度条的几种姿势


    大家好,在下载某些文件的时候你一定会不时盯着进度条,在写代码的时候使用进度条可以便捷的观察任务处理情况,除了使用print来打印之外,今天本文就介绍几种给你的Python代码加上酷炫的进度条的方式。

    自定义ProgressBar

    最原始的办法就是不借助任何第三方工具,自己写一个进度条函数,使用time模块配合sys模块即可

    import sys
    import time
    
    def progressbar(it, prefix="", size=60, file=sys.stdout):
        count = len(it)
        def show(j):
            x = int(size*j/count)
            file.write("%s[%s%s] %i/%i
    " % (prefix, "#"*x, "."*(size-x), j, count))
            file.flush()        
        show(0)
        for i, item in enumerate(it):
            yield item
            show(i+1)
        file.write("
    ")
        file.flush()
    
        
    for i in progressbar(range(15), "Computing: ", 40):
        do_something()
        time.sleep(0.1)
    

    自己定义的好处就是可以将进度条定义成我们想要的形式比如上面就是使用#与·来输出,为什么不用print?因为sys.stdout就是print的一种默认输出格式,而sys.stdout.write()可以不换行打印,sys.stdout.flush()可以立即刷新输出的内容。当然也可以封装成类来更好的使用[1] ,但效果是类似的。

    from __future__ import print_function
    import sys
    import re
    
    
    class ProgressBar(object):
        DEFAULT = 'Progress: %(bar)s %(percent)3d%%'
        FULL = '%(bar)s %(current)d/%(total)d (%(percent)3d%%) %(remaining)d to go'
    
        def __init__(self, total, width=40, fmt=DEFAULT, symbol='=',
                     output=sys.stderr):
            assert len(symbol) == 1
    
            self.total = total
            self.width = width
            self.symbol = symbol
            self.output = output
            self.fmt = re.sub(r'(?P<name>%(.+?))d',
                r'g<name>%dd' % len(str(total)), fmt)
    
            self.current = 0
    
        def __call__(self):
            percent = self.current / float(self.total)
            size = int(self.width * percent)
            remaining = self.total - self.current
            bar = '[' + self.symbol * size + ' ' * (self.width - size) + ']'
    
            args = {
                'total': self.total,
                'bar': bar,
                'current': self.current,
                'percent': percent * 100,
                'remaining': remaining
            }
            print('
    ' + self.fmt % args, file=self.output, end='')
    
        def done(self):
            self.current = self.total
            self()
            print('', file=self.output)
            
    from time import sleep
    
    progress = ProgressBar(80, fmt=ProgressBar.FULL)
    
    for x in range(progress.total):
        progress.current += 1
        progress()
        sleep(0.1)
    progress.done()

    tqdm

    之前我们说了,自定义的好处就是可以自己修改,那么使用第三方库的好处就是可以偷懒,不用自己写,拿来就能用。比如提到Python进度条那肯定会想到常用的tqdm,安装很简单pip install tqdm即可,使用也很简单,几行代码即可实现上面的进度条

    from tqdm import trange
    import time
    for i in trange(10): 
        time.sleep(1)

    当然tqdm作为老牌的Python进度条工具,循环处理、多进程、多线程、递归处理等都是支持的,你可以在官方GitHub上学习[2] 、解锁更多的玩法。

    Rich

    上面两种实现Python进度条的方法都学会了吗,虽然简单但是看上去并不漂亮,颜色也比较单调。所以最后压轴出场的就是一款比较小众的第三方库Rich[3] 。Rich主要是用于在终端中打印丰富多彩的文本(最高支持1670万色)

    所以当然可以使用Rich打印进度条,显示完成百分比,剩余时间,数据传输速度等都可以。并且样式更加酷炫,并且它是高度可配置的,因此我们可以对其进行自定义以显示所需的任何信息。使用也很简单,比如我们使用Rich来实现一个最简单的进度条

    from rich.progress import track
    import  time
    
    for step in track(range(30)):
        print('早起Python')
        time.sleep(0.5)

    同时Rich支持多个进度条,这在多任务情况下监控的进度很有用(使用方法见官方文档)

    参考资料

    [1]

    stackoverflow: https://stackoverflow.com/questions/3160699/python-progress-bar

    [2]

    Tqdm: https://github.com/tqdm/tqdm

    [3]

    Rich: https://github.com/willmcgugan/rich

  • 相关阅读:
    python获取前几天的时间
    协程
    python实现进制之间的转换
    爬虫学习博客
    python 将base64字符串还原为图片
    python进行md5加密
    初始Hibernate4
    Linux安装tomcat
    centOS7安装jdk
    centOS7安装mysql
  • 原文地址:https://www.cnblogs.com/liuzaoqi/p/13041394.html
Copyright © 2020-2023  润新知