在 Jupyter Lab 单元格中显示 Pyvis HTML 时出错

问题描述

我正在使用 Pyvis 库通过 HTML 格式在 Jupyter 实验室(在单元格中)生成显示网络。我设法创建了文件并在外部打开它(使用 Pyvis save_graph 函数)。但是,当使用 Pyvis 的 net.Network(notebook=True) 选项时,Jupyter 输出

404:未找到。
您请求的页面不存在!

虽然终端(从我启动 Jupyter 的地方)输出

404 GET /files/Documents/(Git)%20Projects/Simsalar/Jupyter%20Notebooks/TRIAL2.html?_xsrf=2%7C9665d25e%7Cbc3fea246105c6b3a9d39f6bbb10d156197010d39f6bbb10d156970.html%10d156970.0000000.html 8888/lab/tree/Documents/(Git)%20Projects/Simsalar/Jupyter%20Notebooks/Workshop%20Analysis.ipynb

从 Pyvis 文档中获取错误代码非常简单:

import networkx as nx
from pyvis import network as net

g = net.Network(notebook=True)
nxg = nx.complete_graph(5)
g.from_nx(nxg)
g.show('TRIAL2.html')

澄清一下,HTML 文件的创建没有任何问题(因此可以在外部打开),问题在于在 JupyterLab 内(在单元格中)显示文件。我已经多次重新安装 Jupyter,并使用了多个版本的 Jupyter Lab(现在是 2.4、2.6 和 3.0)。我在完整的 conda 环境规范下面留下了。我还使用 OS 模块给出了 g.show('TRIAL2.html') 函数的不同路径。但是,它仍然不起作用。

# Name                    Version                   Build  Channel
anyio                     2.0.2            py39h6e9494a_3    conda-forge
appnope                   0.1.2            py39h6e9494a_0    conda-forge
argon2-cffi               20.1.0           py39h5a22ff9_2    conda-forge
async_generator           1.10                       py_0    conda-forge
attrs                     20.3.0             pyhd3deb0d_0    conda-forge
babel                     2.9.0              pyhd3deb0d_0    conda-forge
backcall                  0.2.0              pyh9f0ad1d_0    conda-forge
backports                 1.0                        py_2    conda-forge
backports.functools_lru_cache 1.6.1                      py_0    conda-forge
bleach                    3.2.1              pyh9f0ad1d_0    conda-forge
brotlipy                  0.7.0           py39h66d5b7b_1001    conda-forge
ca-certificates           2020.12.8            hecd8cb5_0  
certifi                   2020.12.5        py39h6e9494a_0    conda-forge
cffi                      1.14.4           py39h7786acb_1    conda-forge
chardet                   4.0.0            py39h6e9494a_0    conda-forge
cryptography              3.3.1            py39h79a2c39_0    conda-forge
cycler                    0.10.0                     py_2    conda-forge
decorator                 4.4.2                      py_0    conda-forge
defusedxml                0.6.0                      py_0    conda-forge
entrypoints               0.3             pyhd8ed1ab_1003    conda-forge
freetype                  2.10.4               h3f75d11_0    conda-forge
icu                       68.1                 h74dc148_0    conda-forge
idna                      2.10               pyh9f0ad1d_0    conda-forge
importlib-Metadata        3.3.0            py39h6e9494a_2    conda-forge
importlib_Metadata        3.3.0                hd8ed1ab_2    conda-forge
ipykernel                 5.4.2            py39h71a6800_0    conda-forge
ipython                   7.19.0           py39h71a6800_0    conda-forge
ipython_genutils          0.2.0                      py_1    conda-forge
ipywidgets                7.6.2              pyhd3deb0d_0    conda-forge
jedi                      0.18.0           py39h6e9494a_1    conda-forge
jinja2                    2.11.2             pyh9f0ad1d_0    conda-forge
jpeg                      9d                   hbcb3906_0    conda-forge
json5                     0.9.5              pyh9f0ad1d_0    conda-forge
jsonpickle                1.4.2              pyhd3eb1b0_0  
jsonschema                3.2.0                      py_2    conda-forge
jupyter_client            6.1.7                      py_0    conda-forge
jupyter_core              4.7.0            py39h6e9494a_0    conda-forge
jupyter_server            1.1.3            py39h6e9494a_0    conda-forge
jupyterlab                3.0.0              pyhd8ed1ab_0    conda-forge
jupyterlab_pygments       0.1.2              pyh9f0ad1d_0    conda-forge
jupyterlab_server         2.0.0              pyhd8ed1ab_0    conda-forge
jupyterlab_widgets        1.0.0              pyhd8ed1ab_1    conda-forge
kiwisolver                1.3.1            py39heea7e63_0    conda-forge
lcms2                     2.11                 h11f7e16_1    conda-forge
libblas                   3.9.0                6_openblas    conda-forge
libcblas                  3.9.0                6_openblas    conda-forge
libcxx                    11.0.0               h4c3b8ed_1    conda-forge
libffi                    3.3                  h046ec9c_2    conda-forge
libgfortran               5.0.0               h6c81a4c_15    conda-forge
libgfortran5              9.3.0               h6c81a4c_15    conda-forge
liblapack                 3.9.0                6_openblas    conda-forge
libopenblas               0.3.12          openmp_h54245bb_1    conda-forge
libpng                    1.6.37               h7cec526_2    conda-forge
libsodium                 1.0.18               hbcb3906_1    conda-forge
libtiff                   4.2.0                h355d032_0    conda-forge
libuv                     1.40.0               h35c211d_0    conda-forge
libwebp-base              1.1.0                hbcb3906_3    conda-forge
llvm-openmp               11.0.0               h73239a0_1    conda-forge
lz4-c                     1.9.3                h046ec9c_0    conda-forge
markupsafe                1.1.1            py39h66d5b7b_2    conda-forge
matplotlib                3.3.3            py39h6e9494a_0    conda-forge
matplotlib-base           3.3.3            py39h5674cf4_0    conda-forge
mistune                   0.8.4           py39h5a22ff9_1002    conda-forge
nbclassic                 0.2.5              pyhd8ed1ab_0    conda-forge
nbclient                  0.5.1                      py_0    conda-forge
nbconvert                 6.0.7            py39h6e9494a_3    conda-forge
nbformat                  5.0.8                      py_0    conda-forge
ncurses                   6.2                  h2e338ed_4    conda-forge
nest-asyncio              1.4.3              pyhd8ed1ab_0    conda-forge
networkx                  2.5                        py_0    conda-forge
nodejs                    15.3.0               heaeed3d_0    conda-forge
notebook                  6.1.6            py39h6e9494a_0    conda-forge
numpy                     1.19.4           py39he588a01_2    conda-forge
olefile                   0.46               pyh9f0ad1d_1    conda-forge
openssl                   1.1.1i               h35c211d_0    conda-forge
packaging                 20.8               pyhd3deb0d_0    conda-forge
pandas                    1.2.0            py39h089d6f7_0    conda-forge
pandoc                    2.11.3.2             h35c211d_0    conda-forge
pandocfilters             1.4.3            py39hecd8cb5_1  
parso                     0.8.1              pyhd8ed1ab_0    conda-forge
patsy                     0.5.1                      py_0    conda-forge
pexpect                   4.8.0              pyh9f0ad1d_2    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    8.1.0            py39hc06e9b5_0    conda-forge
pip                       20.3.3             pyhd8ed1ab_0    conda-forge
plotly                    4.14.1             pyhd3deb0d_0    conda-forge
prometheus_client         0.9.0              pyhd3deb0d_0    conda-forge
prompt-toolkit            3.0.8              pyha770c72_0    conda-forge
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pycparser                 2.20               pyh9f0ad1d_2    conda-forge
pygments                  2.7.3              pyhd8ed1ab_0    conda-forge
pyopenssl                 20.0.1             pyhd8ed1ab_0    conda-forge
pyparsing                 2.4.7              pyh9f0ad1d_0    conda-forge
pyrsistent                0.17.3           py39h5a22ff9_1    conda-forge
pysocks                   1.7.1            py39h2c36a5b_2    conda-forge
python                    3.9.1           h1d169a7_2_cpython    conda-forge
python-dateutil           2.8.1                      py_0    conda-forge
python_abi                3.9                      1_cp39    conda-forge
pytz                      2020.5             pyhd8ed1ab_0    conda-forge
pyvis                     0.1.8.2            pyh9f0ad1d_0    conda-forge
pyzmq                     20.0.0           py39h07c395c_1    conda-forge
readline                  8.0                  h0678c8f_2    conda-forge
requests                  2.25.1             pyhd3deb0d_0    conda-forge
retrying                  1.3.3                      py_2    conda-forge
scipy                     1.6.0            py39hca3e1c3_0    conda-forge
seaborn                   0.11.1               h694c41f_0    conda-forge
seaborn-base              0.11.1             pyhd8ed1ab_0    conda-forge
send2trash                1.5.0                      py_0    conda-forge
setuptools                51.0.0           py39hecd8cb5_2  
six                       1.15.0             pyh9f0ad1d_0    conda-forge
sniffio                   1.2.0            py39h6e9494a_0    conda-forge
sqlite                    3.34.0               h17101e1_0    conda-forge
statsmodels               0.12.1           py39hf24ec92_1    conda-forge
terminado                 0.9.1            py39h6e9494a_1    conda-forge
testpath                  0.4.4                      py_0    conda-forge
tk                        8.6.10               h0419947_1    conda-forge
tornado                   6.1              py39h5a22ff9_0    conda-forge
traitlets                 5.0.5                      py_0    conda-forge
tzdata                    2020f                he74cb21_0    conda-forge
urllib3                   1.26.2             pyhd8ed1ab_0    conda-forge
wcwidth                   0.2.5              pyh9f0ad1d_2    conda-forge
webencodings              0.5.1                      py_1    conda-forge
wheel                     0.36.2             pyhd3deb0d_0    conda-forge
widgetsnbextension        3.5.1            py39h6e9494a_4    conda-forge
xz                        5.2.5                haf1e3a3_1    conda-forge
zeromq                    4.3.3                h74dc148_3    conda-forge
zipp                      3.4.0                      py_0    conda-forge
zlib                      1.2.11            h7795811_1010    conda-forge
zstd                      1.4.8                hf387650_1    conda-forge

非常感谢任何帮助/想法! Stackoverflow ticket: I am trying to open an HTML file using python - pyviz

解决方法

更新: 原来 Pyvis(和 IFrame)需要在同一文件夹中的 HTML 文件和 Jupyter Notebook 才能正确显示(否则笔记本会抛出 404 错误)。因此,只需将 HTML 与笔记本放在同一个工作目录中,一切都会顺利。