網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
3種Python?實(shí)現(xiàn)酷炫進(jìn)度條的實(shí)用方法_python
作者:Python學(xué)習(xí)與數(shù)據(jù)挖掘 ? 更新時(shí)間: 2022-06-12 編程語(yǔ)言前言:
在下載某些文件的時(shí)候你一定會(huì)不時(shí)盯著進(jìn)度條,在寫代碼的時(shí)候使用進(jìn)度條可以便捷的觀察任務(wù)處理情況。
除了使用 print 來(lái)打印之外,今天本文我來(lái)給大家介紹幾種酷炫的進(jìn)度條的方式。
1、自定義ProgressBar
最原始的辦法就是不借助任何第三方工具,自己寫一個(gè)進(jìn)度條函數(shù),使用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\r" % (prefix, "#"*x, "."*(size-x), j, count)) ? ? ? ? file.flush() ? ? ? ? ? ? show(0) ? ? for i, item in enumerate(it): ? ? ? ? yield item ? ? ? ? show(i+1) ? ? file.write("\n") ? ? file.flush() ? ?? for i in progressbar(range(15), "Computing: ", 40): ? ? do_something() ? ? time.sleep(0.1)
自己定義的好處就是可以將進(jìn)度條定義成我們想要的形式比如上面就是使用#與·來(lái)輸出,為什么不用print?因?yàn)?code>sys.stdout就是print的一種默認(rèn)輸出格式,而sys.stdout.write()
可以不換行打印,sys.stdout.flush()
可以立即刷新輸出的內(nèi)容。當(dāng)然也可以封裝成類來(lái)更好的使用,但效果是類似的。
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('\r' + 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()
2、tqdm
之前我們說(shuō)了,自定義的好處就是可以自己修改,那么使用第三方庫(kù)的好處就是可以偷懶,不用自己寫,拿來(lái)就能用。比如提到Python進(jìn)度條那肯定會(huì)想到常用的tqdm
,安裝很簡(jiǎn)單pip install tqdm
即可,使用也很簡(jiǎn)單,幾行代碼即可實(shí)現(xiàn)上面的進(jìn)度條
from tqdm import trange import time for i in trange(10):? ? ? time.sleep(1)
當(dāng)然tqdm作為老牌的Python進(jìn)度條工具,循環(huán)處理、多進(jìn)程、多線程、遞歸處理等都是支持的,你可以在官方GitHub上學(xué)習(xí) 、解鎖更多的玩法。
3、Rich
上面兩種實(shí)現(xiàn)Python進(jìn)度條的方法都學(xué)會(huì)了嗎,雖然簡(jiǎn)單但是看上去并不漂亮,顏色也比較單調(diào)。所以最后壓軸出場(chǎng)的就是一款比較小眾的第三方庫(kù)Rich 。Rich主要是用于在終端中打印豐富多彩的文本(最高支持1670萬(wàn)色)
所以當(dāng)然可以使用Rich打印進(jìn)度條,顯示完成百分比,剩余時(shí)間,數(shù)據(jù)傳輸速度等都可以。并且樣式更加酷炫,并且它是高度可配置的,因此我們可以對(duì)其進(jìn)行自定義以顯示所需的任何信息。使用也很簡(jiǎn)單,比如我們使用Rich來(lái)實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的進(jìn)度條
from rich.progress import track import ?time for step in track(range(30)): ? ? time.sleep(0.5)
同時(shí)Rich支持多個(gè)進(jìn)度條,這在多任務(wù)情況下監(jiān)控的進(jìn)度很有用
原文鏈接:https://blog.csdn.net/weixin_38037405/article/details/124031504
相關(guān)推薦
- 2022-09-14 python重寫方法和重寫特殊構(gòu)造方法_python
- 2021-12-07 C語(yǔ)言SetConsoleTextAttribute函數(shù)使用方法_C 語(yǔ)言
- 2022-04-07 一篇文章帶你學(xué)習(xí)Python3的高級(jí)特性(2)_python
- 2022-05-22 C#編程之依賴倒置原則DIP_C#教程
- 2022-09-21 詳解C語(yǔ)言中typedef和#define的用法與區(qū)別_C 語(yǔ)言
- 2023-06-18 C#Process的OutputDataReceived事件不觸發(fā)問(wèn)題及解決_C#教程
- 2022-06-17 Ruby操作CSV格式數(shù)據(jù)方法詳解_ruby專題
- 2022-08-21 Android設(shè)置重復(fù)文字水印背景的方法_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支