python写代码酷炫插件_介绍几种给你的Python代码加上酷炫的进度条的方式

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

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

08c1e90fc2d701086d335434afe8c1c2.gif

自定义ProgressBar

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

importsysimporttimedef progressbar(it, prefix="", size=60, file=sys.stdout):

count=len(it)defshow(j):

x= int(size*j/count)

file.write("%s[%s%s] %i/%i\r" % (prefix, "#"*x, "."*(size-x), j, count))

file.flush()

show(0)for i, item inenumerate(it):yielditem

show(i+1)

file.write("\n")

file.flush()for i in progressbar(range(15), "Computing:", 40):

do_something()

time.sleep(0.1)

606a84666234a352532fb4fd35da9efc.gif

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

from __future__ importprint_functionimportsysimportreclassProgressBar(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) == 1self.total=total

self.width=width

self.symbol=symbol

self.output=output

self.fmt= re.sub(r'(?P%\(.+?\))d',

r'\g%dd' %len(str(total)), fmt)

self.current=0def __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('\r' + self.fmt % args, file=self.output, end='')defdone(self):

self.current=self.total

self()print('', file=self.output)from time importsleep

progress= ProgressBar(80, fmt=ProgressBar.FULL)for x inrange(progress.total):

progress.current+= 1progress()

sleep(0.1)

progress.done()

2d47a64c23aeebb41216fdc2e403eb73.gif

7cc3e8239ce5a816dde1224c908036f3.gif

tqdm

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

from tqdm importtrangeimporttimefor i in trange(10):

time.sleep(1)

863f043567bb6927891788ba680886a4.gif

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

Rich

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

5bf1c86d7c8707fcddaf515e456b89d1.png

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

from rich.progress importtrackimporttimefor step in track(range(30)):print('早起Python')

time.sleep(0.5)

5041dd5fd0d72da17e63ce265aec43c8.gif

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

691d49a3a91b4d388d114e5b9df3d61e.gif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值