Python 多行输出刷新

发现了一个可以完美支持python多行实时输出刷新的库 - reprint。

git地址

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()
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值