首页 > 解决方案 > 检测 Jupyter notebook 时 tqdm 失败

问题描述

tqdm用来显示我的代码的进度条。有时我在终端中使用代码,有时我在Jupyter Notebook中使用代码。

除非我弄错了(可能是或者我不会写这个问题),tqdm否则应该使用以下代码自动确定我是在 Jupyter Notebook 中还是在终端中:

from tqdm.autonotebook import tqdm

def extend(index):
    # some code here

# X is a pandas DataFrame defined elsewhere

with mp.Pool(N) as pool:
    results = list(
        tqdm(pool.imap(extend, X.index), total=len(X)))

进度条在终端中很好地显示,如下所示:

100%|█████████████████████████████████████████| 15035/15035 [35:02<00:00,  7.15it/s]

但是当在 Jupyter Notebook 中调用相同的代码时,会改为显示:

HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=15035.0), HTML(value='')))

难道我做错了什么?我需要做什么才能tqdm在 Jupyter Notebook终端中很好地显示?

标签: pythonjupyter-notebookprogress-barjupytertqdm

解决方案


如果你在 JupyterLab 中编辑 notebook,你应该安装jupyterlab_widgets.


推荐阅读