from tqdm import tqdm
import time, random
all_list =[1,2,3,4,5,6,7,8]with tqdm(total=100)as p_bar:for item in all_list:
time.sleep(random.random())
p_bar.update(100/(len(all_list)))
p_bar.set_description("Processing {}-th iteration".format(i+1))
"""
A Utility to redirect tqdm progress bar to a log file instead of stdout.
"""
__version__ ='1.0.1'
__author__ ='Adarsh Anand'
__author_email__ ='adarsh.anand15@gmail.com'
__license__ ='GNU General Public License v3.0'
__url__ ='https://github.com/adarsh-anand15/tqdm_logger'import io
import os
import sys
__all__ =['TqdmLogger']classTqdmLogger(io.StringIO):"""
Output stream for tqdm which will output to log file instead of
the stdout.
It is necessary to reset the logger stream everytime before starting a new tqdm progress bar.
"""def__init__(self, log_file, bar_stdout_flag =True):"""
Initialize with a log file to use for logging,
a buf to catch logs, and a bar index that stores the line number of the bar in file,
initialized to -1 to indicate that progress bar has not started yet.
Args:
log_file (str): Path of the file to write logs into
"""super(TqdmLogger, self).__init__()
self.logger = log_file
self.buf =''
self.bar_index =-1
self.bar_stdout_flag = bar_stdout_flag
# Create the log file if it doesn't already existsifnot os.path.isfile(self.logger):
fstream =open(self.logger,'w', encoding='utf-8')
fstream.close()defreset(self):"""
Reset the bar index to -1 before starting a new progress bar
to avoid updating an earlier progress bar.
"""
self.bar_index =-1
self.buf =''defwrite(self, buf, log_type =None):"""
Capture the buf to be written incase of progress bar else flush the buf to log file.
"""if buf.strip()and(log_type =='text'or('it/s'notin buf.strip()and's/it'notin buf.strip())):withopen(self.logger,'a', encoding='utf-8')as ofile:
ofile.write(buf)
ofile.flush()
sys.stdout.write(buf.rstrip()+'\n')
sys.stdout.flush()else:
self.buf = buf.strip('\r\n\t ')if self.bar_stdout_flag:
sys.stdout.write(buf)
sys.stdout.flush()defflush(self):"""
Update the progress bar. It does that by reading the entire file content, replacing the line
containing the progress bar, and writes back everything.
"""#skip if buffer is emptyifnot self.buf.strip():return
sys.stdout.flush()# Read the entire content of the log file
ifstream =open(self.logger,'r', encoding='utf-8')
lines = ifstream.readlines()
ifstream.close()# Replace the progress bar line with new valueif self.bar_index !=-1:del lines[self.bar_index]
lines.append(self.buf.strip())
self.bar_index =len(lines)-1for i, line inenumerate(lines):
lines[i]= line.strip()+'\n'# write back everything
ofstream =open(self.logger,'w', encoding='utf-8')
ofstream.writelines(lines)
ofstream.close()# reset the buffer for next cycle
self.buf =''