Table of contents
PySimpleGUI graphical progress bar
Normal progress bar
You can perform statistical calculations during code iteration and use formatted strings to output the code running progress.
import sys
import time
for i in range(1, 101):
print("\r", end="")
print("下载进度{}%: ".format(i), "\33[1;91;43m▋\33[1;32;0m" * (i // 2), end="")
sys.stdout.flush()
time.sleep(0.05)
With time progress bar
import time
scale = 50
start = time.perf_counter()
for i in range(scale + 1):
a = "\33[1;91;101m▋\33[1;32;0m" * i
b = " " * (scale - i)
c = (i / scale) * 100
dur = time.perf_counter() - start
print("\r下载进度{:^3.0f}% {}{} {:.2f}s".format(c, a, b, dur), end="")
time.sleep(0.05)
progress progress bar
You only need to define the number of iterations, the type of progress bar and inform the progress bar during each iteration.
Official: https://pypi.org/project/progress
import time
from progress.bar import IncrementalBar
from progress.bar import Bar
i = 10
# bar = IncrementalBar('进度', max=1)
bar = Bar('进度', max=i, fill='\33[1;91;101m❤ \33[1;32;0m', suffix='%(percent)d%%')
for item in range(10):
bar.next()
time.sleep(1)
bar.finish()
alive_progress progress bar
This progress bar has some animation effects. Related documents: https://github.com/rsalmei/alive-progress
import time
from alive_progress import alive_bar
items = range(100)
with alive_bar(total=len(items)) as bar:
for item in items:
bar()
time.sleep(0.1)
PySimpleGUI graphical progress bar
import PySimpleGUI as sg
import time
num = 100
for i in range(num):
sg.one_line_progress_meter('任务下载进度', current_value=i, max_value=num, key='完成!', no_button=True, keep_on_top=True)
time.sleep(0.1)