发现了一个可以完美支持python多行实时输出刷新的库 - reprint。
git地址
安装库
pip install reprint
效果
官方给的动态图
场景Demo
场景1
有四种水果,工厂每次生产随机一种,实时统计输出每种水果各有多少个
from reprint import output
from random import choice
from time import sleep
def produce():
products = ["apple","banana","grape","peach"]
return choice(products)
def multrefresh():
with output(output_type='dict') as output_lines:
for _ in range(100):
product = produce()
if product not in output_lines.keys():
output_lines[product] = 1
else:
output_lines[product] += 1
sleep(0.05)
if __name__ == "__main__":
multrefresh()
场景2
输出两个进度条,每次第一个进度条完成后增加第二个进度条1%进度
from threading import main_thread
from reprint import output
from time import sleep
def doubleprogressbar():
with output(output_type='dict') as output_lines:
for i in range(1,101):
for progress in range(1,101):
output_lines['Partial Progress'] = "[{done}{padding}] {percent}%".format(
done = "#" * int(progress/5),
padding = " " * (20 - int(progress/5)),
percent = progress
)
sleep(0.01)
output_lines['Total Progress'] = "[{done}{padding}] {percent}%".format(
done = "#" * int(i/5),
padding = " " * (20 - int(i/5)),
percent = i
)
if __name__ == "__main__":
doubleprogressbar()