jupyterlab 交互图
jupyterlab interactive plot
使用旧的 Jupyter 笔记本,我可以通过以下方式创建交互式图表:
import matplotlib.pyplot as plt
%matplotlib notebook
x = [1,2,3]
y = [4,5,6]
plt.figure()
plt.plot(x,y)
然而,在 JupyterLab 中,这给出了一个错误:
JavaScript output is disabled in JupyterLab
我也试过魔术(安装了jupyter-matplotlib
):
%matplotlib ipympl
但那只是 returns:
FigureCanvasNbAgg()
内联图有效,但它们不是交互式图:
%matplotlib inline
根据 ,这是由于 Node.js 未安装造成的。
要启用 jupyter-matplotlib 后端,请使用 matplotlib Jupyter magic:
%matplotlib widget
import matplotlib.pyplot as plt
plt.figure()
x = [1,2,3]
y = [4,5,6]
plt.plot(x,y)
这里有更多信息jupyter-matplotlib on GitHub
JupyterLab 3.0+
安装 jupyterlab
和 ipympl
.
pip
用户:
pip install --upgrade jupyterlab ipympl
conda
用户:
conda update -c conda-forge jupyterlab ipympl
重启 JupyterLab。
用 header:
修饰包含绘图代码的单元格
%matplotlib widget
# plotting code goes here
JupyterLab 2.0
安装nodejs
,例如conda install -c conda-forge nodejs
.
安装ipympl
,例如conda install -c conda-forge ipympl
.
[可选,但推荐。]更新 JupyterLab,例如
conda update -c conda-forge jupyterlab==2.2.9==py_0
.
[可选,但推荐。] 对于本地用户安装,运行:
export JUPYTERLAB_DIR="$HOME/.local/share/jupyter/lab"
.
安装扩展:
jupyter labextension install @jupyter-widgets/jupyterlab-manager
jupyter labextension install jupyter-matplotlib
启用小部件:jupyter nbextension enable --py widgetsnbextension
。
重启 JupyterLab。
用%matplotlib widget
装饰。
这个 solution 在 jupyterlab 中有效
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import clear_output
n = 10
a = np.zeros((n, n))
plt.figure()
for i in range(n):
plt.imshow(a)
plt.show()
a[i, i] = 1
clear_output(wait=True)
JupyterLab 3 的步骤*
我以前曾多次使用 Mateen's ,但是当我使用 JupyterLab 3.0.7 尝试它们时,我发现 jupyter labextension install @jupyter-widgets/jupyterlab-manager
返回了一个错误并且我损坏了小部件。
经过大量的头痛和谷歌搜索后,我想我会 post 为任何发现自己在这里的人提供解决方案。
现在简化了这些步骤,我可以通过以下方式重新开始交互式绘图:
pip install jupyterlab
pip install ipympl
- 装饰
%matplotlib widget
第 2 步将自动处理其余的依赖项,包括替换(现在折旧?)@jupyter-widgets/jupyterlab-manager
希望这能为其他人节省一些时间!
总结
在复杂的设置中,jupyter-lab
进程和 Jupyter/IPython 内核进程 运行 在不同的 Python 虚拟环境中,注意与 Jupyter 相关的 Python 包和 Jupyter 扩展(例如 ipympl
、jupyter-matplotlib
)版本及其在环境之间的兼容性。
即使在单个 Python 虚拟环境中,也要确保您遵守 ipympl
compatibility table.
例子
几个示例如何 运行 JupyterLab。
简单(st)
我想,运行 JupyterLab 最简单的跨平台方式是从 Docker 容器中 运行 安装它。您可以像这样构建 运行 JupyterLab 3 容器。
docker run --name jupyter -it -p 8888:8888 \
# This line on a Linux- and non-user-namespaced Docker will "share"
# the directory between Docker host and container, and run from the user.
-u 1000 -v $HOME/Documents/notebooks:/tmp/notebooks \
-e HOME=/tmp/jupyter python:3.8 bash -c "
mkdir /tmp/jupyter; \
pip install --user 'jupyterlab < 4' 'ipympl < 0.8' pandas matplotlib; \
/tmp/jupyter/.local/bin/jupyter lab --ip=0.0.0.0 --port 8888 \
--no-browser --notebook-dir /tmp/notebooks;
"
当它完成时(这需要一段时间),终端中最底部的行应该是这样的。
To access the server, open this file in a browser:
...
http://127.0.0.1:8888/lab?token=abcdef...
您只需单击 link,JupyterLab 就会在您的浏览器中打开。关闭 JupyterLab 实例后,容器将停止。您可以使用 docker start -ai jupyter
.
重新启动它
复杂
这种GitHub Gist illustrates the idea how to build a Python virtual environment with JupyterLab 2 and also building all required extensions with Nodejs in the container, without installing Nodejs on host system. With JupyterLab 3 and pre-build extensions这种方法变得不那么重要了。
上下文
我今天在调试 JupyterLab 2 中不工作的 %matplotlib widget
时抓耳挠腮。我有单独的预构建 JupyterLab venv(如上所述),它将本地 JupyterLab 作为 Chromium“应用程序模式”(即c.LabApp.browser = 'chromium-browser --app=%s'
在配置中),以及来自具有特定依赖项(很少更改)的简单 Python venvs 的一些 IPython 内核和一个将自身公开为 IPython 内核的应用程序。交互式“小部件”模式的问题以不同的方式表现出来。
例如,有
在 JupyterLab“主机”venv 中:jupyter-matplotlib v0.7.4 扩展和 ipympl==0.6.3
在内核venv中:ipympl==0.7.0
和matplotlib==3.4.2
在浏览器控制台中我遇到了这些错误:
Error: Module jupyter-matplotlib, semver range ^0.9.0 is not registered as a widget module
Error: Could not create a model.
Could not instantiate widget
在 JupyterLab UI:
%matplotlib widget
重启成功
- 图表卡在“正在加载小部件...”中
- 带有图表输出的单元格运行 上没有任何内容
- 在之前的尝试中
%matplotlib widget
可能会引发类似 KeyError: '97acd0c8fb504a2288834b349003b4ae'
的问题
在浏览器控制台的内核 venv 中降级 ipympl==0.6.3
:
Could not instantiate widget
Exception opening new comm
Error: Could not create a model.
Module jupyter-matplotlib, semver range ^0.8.3 is not registered as a widget module
一旦我根据 ipympl
compatibility table 制作了 packages/extensions:
在 JupyterLab“主机”venv 中:jupyter-matplotlib v0.8.3 扩展,ipympl==0.6.3
内核中的venv:ipympl==0.6.3
,matplotlib==3.3.4
它或多或少按预期工作。好吧,除了我在图表的每个单元格中放置 %matplotlib widget
之外,还有很多小故障,比如在重新启动时,第一个图表“累积”笔记本中所有图表的所有内容。每个单元格 %matplotlib widget
,一次只有一个图表处于“活动”状态。并且在重新启动时仅呈现最后一个小部件(但手动重新 运行 单元修复)。
使用旧的 Jupyter 笔记本,我可以通过以下方式创建交互式图表:
import matplotlib.pyplot as plt
%matplotlib notebook
x = [1,2,3]
y = [4,5,6]
plt.figure()
plt.plot(x,y)
然而,在 JupyterLab 中,这给出了一个错误:
JavaScript output is disabled in JupyterLab
我也试过魔术(安装了jupyter-matplotlib
):
%matplotlib ipympl
但那只是 returns:
FigureCanvasNbAgg()
内联图有效,但它们不是交互式图:
%matplotlib inline
根据
要启用 jupyter-matplotlib 后端,请使用 matplotlib Jupyter magic:
%matplotlib widget
import matplotlib.pyplot as plt
plt.figure()
x = [1,2,3]
y = [4,5,6]
plt.plot(x,y)
这里有更多信息jupyter-matplotlib on GitHub
JupyterLab 3.0+
安装
jupyterlab
和ipympl
.pip
用户:pip install --upgrade jupyterlab ipympl
conda
用户:conda update -c conda-forge jupyterlab ipympl
重启 JupyterLab。
用 header:
修饰包含绘图代码的单元格%matplotlib widget # plotting code goes here
JupyterLab 2.0
安装
nodejs
,例如conda install -c conda-forge nodejs
.安装
ipympl
,例如conda install -c conda-forge ipympl
.[可选,但推荐。]更新 JupyterLab,例如
conda update -c conda-forge jupyterlab==2.2.9==py_0
.[可选,但推荐。] 对于本地用户安装,运行:
export JUPYTERLAB_DIR="$HOME/.local/share/jupyter/lab"
.安装扩展:
jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter labextension install jupyter-matplotlib
启用小部件:
jupyter nbextension enable --py widgetsnbextension
。重启 JupyterLab。
用
%matplotlib widget
装饰。
这个 solution 在 jupyterlab 中有效
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import clear_output
n = 10
a = np.zeros((n, n))
plt.figure()
for i in range(n):
plt.imshow(a)
plt.show()
a[i, i] = 1
clear_output(wait=True)
JupyterLab 3 的步骤*
我以前曾多次使用 Mateen's jupyter labextension install @jupyter-widgets/jupyterlab-manager
返回了一个错误并且我损坏了小部件。
经过大量的头痛和谷歌搜索后,我想我会 post 为任何发现自己在这里的人提供解决方案。
现在简化了这些步骤,我可以通过以下方式重新开始交互式绘图:
pip install jupyterlab
pip install ipympl
- 装饰
%matplotlib widget
第 2 步将自动处理其余的依赖项,包括替换(现在折旧?)@jupyter-widgets/jupyterlab-manager
希望这能为其他人节省一些时间!
总结
在复杂的设置中,jupyter-lab
进程和 Jupyter/IPython 内核进程 运行 在不同的 Python 虚拟环境中,注意与 Jupyter 相关的 Python 包和 Jupyter 扩展(例如 ipympl
、jupyter-matplotlib
)版本及其在环境之间的兼容性。
即使在单个 Python 虚拟环境中,也要确保您遵守 ipympl
compatibility table.
例子
几个示例如何 运行 JupyterLab。
简单(st)
我想,运行 JupyterLab 最简单的跨平台方式是从 Docker 容器中 运行 安装它。您可以像这样构建 运行 JupyterLab 3 容器。
docker run --name jupyter -it -p 8888:8888 \
# This line on a Linux- and non-user-namespaced Docker will "share"
# the directory between Docker host and container, and run from the user.
-u 1000 -v $HOME/Documents/notebooks:/tmp/notebooks \
-e HOME=/tmp/jupyter python:3.8 bash -c "
mkdir /tmp/jupyter; \
pip install --user 'jupyterlab < 4' 'ipympl < 0.8' pandas matplotlib; \
/tmp/jupyter/.local/bin/jupyter lab --ip=0.0.0.0 --port 8888 \
--no-browser --notebook-dir /tmp/notebooks;
"
当它完成时(这需要一段时间),终端中最底部的行应该是这样的。
To access the server, open this file in a browser:
...
http://127.0.0.1:8888/lab?token=abcdef...
您只需单击 link,JupyterLab 就会在您的浏览器中打开。关闭 JupyterLab 实例后,容器将停止。您可以使用 docker start -ai jupyter
.
复杂
这种GitHub Gist illustrates the idea how to build a Python virtual environment with JupyterLab 2 and also building all required extensions with Nodejs in the container, without installing Nodejs on host system. With JupyterLab 3 and pre-build extensions这种方法变得不那么重要了。
上下文
我今天在调试 JupyterLab 2 中不工作的 %matplotlib widget
时抓耳挠腮。我有单独的预构建 JupyterLab venv(如上所述),它将本地 JupyterLab 作为 Chromium“应用程序模式”(即c.LabApp.browser = 'chromium-browser --app=%s'
在配置中),以及来自具有特定依赖项(很少更改)的简单 Python venvs 的一些 IPython 内核和一个将自身公开为 IPython 内核的应用程序。交互式“小部件”模式的问题以不同的方式表现出来。
例如,有
在 JupyterLab“主机”venv 中:jupyter-matplotlib v0.7.4 扩展和
ipympl==0.6.3
在内核venv中:
ipympl==0.7.0
和matplotlib==3.4.2
在浏览器控制台中我遇到了这些错误:
Error: Module jupyter-matplotlib, semver range ^0.9.0 is not registered as a widget module
Error: Could not create a model.
Could not instantiate widget
在 JupyterLab UI:
%matplotlib widget
重启成功- 图表卡在“正在加载小部件...”中
- 带有图表输出的单元格运行 上没有任何内容
- 在之前的尝试中
%matplotlib widget
可能会引发类似KeyError: '97acd0c8fb504a2288834b349003b4ae'
的问题
在浏览器控制台的内核 venv 中降级 ipympl==0.6.3
:
Could not instantiate widget
Exception opening new comm
Error: Could not create a model.
Module jupyter-matplotlib, semver range ^0.8.3 is not registered as a widget module
一旦我根据 ipympl
compatibility table 制作了 packages/extensions:
在 JupyterLab“主机”venv 中:jupyter-matplotlib v0.8.3 扩展,
ipympl==0.6.3
内核中的venv:
ipympl==0.6.3
,matplotlib==3.3.4
它或多或少按预期工作。好吧,除了我在图表的每个单元格中放置 %matplotlib widget
之外,还有很多小故障,比如在重新启动时,第一个图表“累积”笔记本中所有图表的所有内容。每个单元格 %matplotlib widget
,一次只有一个图表处于“活动”状态。并且在重新启动时仅呈现最后一个小部件(但手动重新 运行 单元修复)。