我正在尝试在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显示这些进度条的设置?
答案 0 :(得分:5)
答案在this GitHub issue中。
关键是要确保使用以下命令启用ipywidgets
笔记本扩展名:
jupyter nbextension enable --py widgetsnbextension
您还需要install the JupyterLab extension:
jupyter labextension install @jupyter-widgets/jupyterlab-manager
答案 1 :(得分:4)
这里重要的考虑因素是使节点版本> = 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
浏览器标签后,它现在应该可以工作。
答案 2 :(得分: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 lab,试了一下代码:
import ipywidgets as widgets
widgets.IntSlider()
它终于奏效了。所以我猜缺少的是通过 jupyter labextension 安装 js。
答案 3 :(得分:0)
如果未安装节点,则可以按照此处的说明进行操作: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
答案 4 :(得分:-5)
我发现 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。