Created
January 11, 2025 05:30
-
-
Save Ryaang/28f8e35fef32f4113af229749cd9584a to your computer and use it in GitHub Desktop.
rich进度条
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # from rich_show import progress | |
| from rich.console import Console | |
| from rich.progress import Progress, TimeElapsedColumn, TimeRemainingColumn, BarColumn, MofNCompleteColumn, SpinnerColumn | |
| from rich.live import Live | |
| # from rich import print | |
| console = Console() | |
| # 创建 Progress 对象和任务 | |
| progress = Progress( | |
| SpinnerColumn(), | |
| "[progress.description]{task.description}", | |
| MofNCompleteColumn(), | |
| BarColumn(bar_width=None), | |
| "[progress.percentage]{task.percentage:>3.0f}%", | |
| TimeElapsedColumn(), | |
| TimeRemainingColumn(), | |
| speed_estimate_period=240.0, # 增加速度估算周期 | |
| refresh_per_second=1, # 降低刷新频率 | |
| transient=False # 保持进度条可见 | |
| ) | |
| # 创建 Live 对象 | |
| live = Live( | |
| progress, | |
| console=console, | |
| refresh_per_second=10 | |
| ) | |
| if __name__ == "__main__": | |
| from time import sleep | |
| with live: | |
| task = progress.add_task("Processing", total=3438756) | |
| for i in range(3438756): | |
| inner_task = progress.add_task("Inner Processing", total=10) | |
| for j in range(10): | |
| progress.update(inner_task, advance=1) | |
| sleep(76) # 模拟处理时间 | |
| progress.update(task, advance=1) | |
| sleep(0.1) # 模拟处理时间 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment