You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在使用papermill执行Notebook时显示tqdm进度条?

在Papermill执行Notebook时显示tqdm进度条的解决方案

方法1:切换到控制台版tqdm

直接替换Notebook专属的tqdm.notebook.tqdm为标准tqdm,进度条会输出到终端控制台,Papermill执行时能实时看到更新:

from tqdm import tqdm
import time

# 模拟10分钟左右的循环(100次×6秒)
for i in tqdm(range(100)):
    time.sleep(6)

方法2:兼容Notebook和Papermill的条件切换

通过检测环境自动选择tqdm版本,既保留Notebook里的交互式进度条,又能在Papermill运行时输出到控制台:

import sys
import time
from tqdm import tqdm
from tqdm.notebook import tqdm as notebook_tqdm

# 判断是否由Papermill驱动执行
is_running_with_papermill = 'papermill' in sys.modules

# 选择对应的进度条实现
progress = notebook_tqdm if not is_running_with_papermill else tqdm

for i in progress(range(100)):
    time.sleep(6)

方法3:强制Notebook版tqdm输出到标准流

如果一定要用Notebook风格的进度条,可指定输出到sys.stdout并开启强制刷新,避免输出缓冲:

import sys
import time
from tqdm.notebook import tqdm

for i in tqdm(range(100), file=sys.stdout, flush=True):
    time.sleep(6)

原理说明

Papermill在非交互式环境下运行Notebook,默认的tqdm.notebook依赖IPython的交互式显示系统,无法在终端渲染。切换到控制台版tqdm或强制输出到标准输出流,就能让进度条直接在运行Papermill的终端中实时显示。

内容的提问来源于stack exchange,提问作者David Davíd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 03:42:37