[英]How can I display the ETA and speed of nested TQDM progress bars?
我正在嘗試在 Jupyter Notebook 中顯示兩個 TQDM 進度條。 我將 VSC 與 Jupyter Notebook 和 Python 3.9 一起使用。
我有以下代碼:
from pathlib import Path
from tqdm.autonotebook import tqdm
outerdir = Path('some/path')
dirs = list(outerdir.iterdir())
for dir in tqdm(dirs, desc='outer'):
files = dir.iterdir()
for file in tqdm(files, desc='inner'):
**do something**
但是,我的output是這樣的:
outer: 0%| | 0/217 [00:00<?, ?it/s]
意思是,它只顯示外部進度條,而不顯示內部進度條。 此外,它不顯示ETA和速度。
有誰知道這是為什么?
注意:當我仍然使用os
而不是pathlib
來管理路徑和文件夾迭代時,它正在工作。
[編輯]:內部循環中的代碼每次運行至少需要 1 分鍾。 外循環的每次迭代大約有 50 次內循環運行,因此外循環每次運行至少需要 50 分鍾。 這意味着循環的持續時間應該不是問題。
這是我創建的不使用 TQDM 的解決方案,它根據 1 次迭代所花費的時間計算 ETA
import time
import sys
from colorama import init, Fore, Back, Style, ansi
init(autoreset=True)
def progress(count, total, eta, status=''):
bar_len = 50
filled_len = int(round(bar_len * count / float(total)))
tip = '>' if count < total else ''
n = Fore.GREEN +"Complete"+ Fore.WHITE if filled_len == bar_len else Fore.RED + status + Fore.WHITE
percents = round(100.0 * count / float(total), 1)
bar = Fore.MAGENTA+'=' * filled_len + tip + Fore.CYAN +'-' * (bar_len - filled_len)+Fore.WHITE
sys.stdout.write('(%s) [%s] %s%s ~%s\r' % (n, bar, percents, '%', eta))
sys.stdout.flush()
x = time.perf_counter()
k = 50
m, m_ = 0, 0
tm = 1
for i in range(1, k+1):
b = " "
#estimated time of arrival ETA
m_1 = Back.WHITE +Fore.BLUE+ " ETA {:.2f} sec ".format(abs(m-m_))
progress(i, k, eta=f' {m_1}', status = 'Running')
time.sleep(tm)
y = time.perf_counter()
benchmark = y-x
m = benchmark * (k/i)
m_ = (m/k) * i+ tm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.