【问题标题】:Jupyter Notebooks not displaying progress barsJupyter Notebooks 不显示进度条
【发布时间】:2019-12-12 01:33:11
【问题描述】:

我正在尝试在 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 显示这些进度条的设置?

【问题讨论】:

  • 您是否在 Jupyter Notebook 或 Jupyter Lab 中遇到此问题?
  • 你在哪里运行单元格?我只在 PyCharm 中收到 HBox() 消息,但在我的 Chrome 浏览器中运行时它工作正常。
  • @MihaiChelaru 我在 Jupyter 实验室遇到了这个问题。
  • @Rafay 是的,我不确定这是否与普通的 Jupyter 有关。也许有人应该编辑问题标题和标签以反映这一点,所以很明显问题和答案是指 Jupyter Lab。

标签: python jupyter-notebook jupyter-lab tqdm


【解决方案1】:

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

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

这对我来说产生了可接受的输出。

【讨论】:

    【解决方案2】:

    在执行命令之前阅读所有内容:

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

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

    创建新环境并安装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实验室,试了代码:

    import ipywidgets as widgets
    widgets.IntSlider()
    

    它终于奏效了。所以我想缺少的是通过jupyter labextension安装js。

    【讨论】:

      【解决方案3】:

      答案在this GitHub issue

      关键是确保您使用以下命令启用了ipywidgets 笔记本扩展:

      jupyter nbextension enable --py widgetsnbextension
      

      对于旧的 JupyterLab 2.0,您还需要install the JupyterLab extension

      jupyter labextension install @jupyter-widgets/jupyterlab-manager
      

      对于旧版 JupyterLab 2.0,使用上述命令安装 JupyterLab 扩展需要您拥有 Node.js installed。 Node.js 网站的安装程序包括npm,这也是命令正常运行所必需的。

      使用 JupyterLab 3.0 时,当您使用 pipconda 安装扩展程序时,将自动与 ipywidgets 一起安装。 JupyterLab 3.0 不再需要 Node.js。

      【讨论】:

      • 我用最新版本的 Juypterlab 尝试了这个。第二个命令需要 10 分钟才能运行,但无法解决问题。
      • 值得一提的是需要node,js。我没有使用 conda,因此我在 mac 'brew install node' 上发布。
      • 我也安装了 npm。否则lab-manager安装失败。
      • 希望它能够自动安装并使用jupyter lab启用
      • 不幸的是,这个解决方案对我不起作用。安装了 node.js v12.16.1 和 ipywidgets 扩展后,我仍然可以得到简单的旧 HBOX(children=...) 静态打印。我相信我已经安装了 @jupyter-widgets/jupyterlab-manager,并且尝试再次安装它会导致约 20 页的递归错误回溯。
      【解决方案4】:

      我发现 Bartosz Mikulski 在他的博客上提供的解决方案非常简单且易于执行:How to display a progress bar in 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)
      

      您也可以参考this Stack Overflow answer by ChristopheD"3 Tips to Improving Your Data Science Workflow" on the Towards Data Science blog

      【讨论】:

        【解决方案5】:

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

        node -v
        

        另外,您可能安装了节点 >=10 的版本,但未选择。要查看已安装的节点版本列表,您可以使用节点版本管理器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浏览器标签后,它现在应该可以工作了。

        【讨论】:

        • 注意:JupyterLab 3.0+ 不需要 Node.js;扩展可以使用 pip 安装,并且应该自动激活。
        【解决方案6】:

        如果您没有安装节点,您可以按照此处的说明进行操作: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

        【讨论】:

          猜你喜欢
          • 2012-06-14
          • 1970-01-01
          • 1970-01-01
          • 2016-09-13
          相关资源
          最近更新 更多