Jupyter Notebooks 不显示进度条

问题描述 投票:0回答:7

我正在尝试在 Jupyter 笔记本中显示进度条。这是一台新电脑,我通常做的事情似乎不起作用:

from tqdm import tqdm_notebook
example_iter = [1,2,3,4,5]
for rec in tqdm_notebook(example_iter):
    time.sleep(.1)

产生以下文本输出并且不显示任何进度条

HBox(children=(IntProgress(value=0, max=5), HTML(value='')))

同样,这段代码:

from ipywidgets import FloatProgress
from IPython.display import display
f = FloatProgress(min=0, max=1)
display(f)
for i in [1,2,3,4,5]:
    time.sleep(.1)

产生此文本输出:

FloatProgress(value=0.0, max=1.0)

我是否缺少让 Jupyter 显示这些进度条的设置?

python jupyter-notebook jupyter-lab tqdm
7个回答
81
投票

答案就在这个 GitHub 问题

关键是确保您使用以下命令启用了

ipywidgets
笔记本扩展:

jupyter nbextension enable --py widgetsnbextension

对于旧的 JupyterLab 2.0,您还需要 安装 JupyterLab 扩展

jupyter labextension install @jupyter-widgets/jupyterlab-manager

对于旧的 JupyterLab 2.0,使用上述命令安装 JupyterLab 扩展需要您安装 Node.js 。 Node.js 网站上的安装程序包括

npm
,这也是命令正常运行所必需的。

使用 JupyterLab 3.0 时,当您使用

ipywidgets
pip
安装时,扩展将与
conda
一起自动安装。 JupyterLab 3.0 不再需要 Node.js。


10
投票

这里一个重要的考虑因素是节点版本 >=10.0.0 才能正常工作。 要检查您的节点版本,请使用:

node -v

此外,您可能安装了 >=10 的 Node 版本,但未选择。要检查已安装的节点版本列表,您可以使用节点版本管理器

nvm

nvm ls

在下面的示例中,选择的版本是 9.11.2:

->      v9.11.2
        v10.4.0
        v12.5.0

为了解决这个问题,我必须运行:

nvm use 12.5.0

现在,我可以运行@Mihai提到的两个命令了:

jupyter nbextension enable --py widgetsnbextension
jupyter labextension install @jupyter-widgets/jupyterlab-manager

刷新

Jupyter
浏览器选项卡后,它现在应该可以工作了。


8
投票

快速破解,如果你不想正确解决它:

运行命令行版本的

tqdm
,即将
from tqdm import tqdm_notebook
替换为
from tqdm import tqdm
并运行
for i in tqdm(range(10000)): pass

这为我产生了可接受的输出。


3
投票

在执行命令之前阅读全部内容:

我按照这里的所有说明进行了多次操作,但没有任何效果。

在我最后一次尝试中,我做到了:

创建新环境并安装jupyterlab

来自 https://github.com/nodesource/distributions/blob/master/README.md#debinstall

# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_15.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_15.x | bash -
apt-get install -y nodejs

然后:

conda install -c conda-forge nodejs
jupyter labextension install @jupyter-widgets/jupyterlab-manager
conda install -c conda-forge ipywidgets

还是没用。然后按照here的建议,我做了:

jupyter labextension install js

重新启动jupyter lab,尝试代码:

import ipywidgets as widgets
widgets.IntSlider()

终于成功了。所以我猜缺少的是通过jupyter labextension安装js。


1
投票

如果您没有安装节点,可以按照此处的说明进行操作:https://github.com/nodesource/distributions/blob/master/README.md#debinstall

curl -sL https://deb.nodesource.com/setup_15.x | bash -
apt-get install -y nodejsapt-get install -y nodejs

但有时最好通过 conda 安装:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
 ./Miniconda3-latest-Linux-x86_64.sh

然后:

conda install -c conda-forge nodejs
jupyter labextension install @jupyter-widgets/jupyterlab-manager

参考:https://ipywidgets.readthedocs.io/en/latest/user_install.html


0
投票

缺少 jupyter-widgets 扩展。

所有命令行安装选项均不适用于 JupyterLab 4。

通过 JupyterLab 界面安装扩展是可行的。

  1. 单击左侧边栏上的扩展管理器按钮。
  2. 搜索
    jupyter-widgets
  3. 安装扩展
  4. 刷新页面

-4
投票

我发现 Bartosz Mikulski 在他的博客上提供的解决方案非常简单且易于执行:如何在 Jupyter Notebook 中显示进度条

import time, sys
from IPython.display import clear_output

def update_progress(progress):
    bar_length = 20
    if isinstance(progress, int):
        progress = float(progress)
    if not isinstance(progress, float):
        progress = 0
    if progress < 0:
        progress = 0
    if progress >= 1:
        progress = 1

block = int(round(bar_length * progress))

clear_output(wait = True)
    text = "Progress: [{0}] {1:.1f}%".format( "#" * block + "-" * (bar_length - block), progress * 100)
    print(text)

您还可以参考 ChristopheD 在 Stack Overflow 上的回答迈向数据科学博客上的“改善数据科学工作流程的 3 个技巧”

© www.soinside.com 2019 - 2024. All rights reserved.