如何让Juypter小部件通过Sphinx和nbconvert正确渲染?

2024-04-27 07:52:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在笔记本中使用Jupyter小部件。然后通过Sphinx+nbsphinx处理我的笔记本,生成HTML。我注意到,即使下面的简单示例在我的笔记本(Jupyter Lab上)中也显示了一个滑块,但是生成的相应HTML页面没有显示滑块

import ipywidgets as widgets

widgets.IntSlider()

我知道有一种方法可以通过观察Juypter Widgets的已发布HTML来获取要呈现的小部件。他们的书是使用Sphinx+nbsphinx构建的。当我查看它们的Sphinx configuration时,它们启用了以下扩展

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.autosummary',
    'sphinx.ext.intersphinx',
    'sphinx.ext.mathjax',
    'nbsphinx',
    'jupyter_sphinx.execute',
    'IPython.sphinxext.ipython_console_highlighting',
]

我相信将小部件从笔记本呈现到发布的HTML的关键是jupyter_sphinx.execute扩展。我的项目的扩展如下。请注意,当发出make html时,会出现一个警告,将jupyter_sphinx.execute更改为jupyter_sphinx(但是,这并不重要,因为使用其中一个仍然不会呈现小部件)

extensions = [
    'sphinxcontrib.bibtex',
    'nbsphinx',
    'sphinx.ext.mathjax',
    'sphinx_sitemap',
    'jupyter_sphinx'
]

发出命令pip list我看到了以下包

jupyter-client                     6.1.3
jupyter-console                    6.1.0
jupyter-core                       4.6.3
jupyter-server-proxy               1.5.0
jupyter-sphinx                     0.3.2
jupyterlab                         2.1.4
jupyterlab-commenting-service      0.2
jupyterlab-server                  1.1.5
nbconvert                          6.0.7
nbsphinx                           0.8.0
sphinx-autobuild                   2020.9.1
sphinx-rtd-theme                   0.5.0
sphinx-sitemap                     2.2.0
sphinxcontrib-applehelp            1.0.2
sphinxcontrib-bibtex               1.0.0
sphinxcontrib-blockdiag            2.0.0
sphinxcontrib-devhelp              1.0.2
sphinxcontrib-htmlhelp             1.0.3
sphinxcontrib-jsmath               1.0.1
sphinxcontrib-qthelp               1.0.3
sphinxcontrib-serializinghtml      1.1.4
sphinxcontrib-websupport           1.2.2

如果你看一下它们的ipynb{a5},你所看到的只是其中一个单元格中的widgets.IntSlider()(没有什么特别的事情发生)。根据nbconvert的说法,live rendering of widgets从4.3版开始就可以使用了

关于我缺少的其他配置或步骤有什么想法吗


Tags: execute部件htmlsphinxsphinxcontrib笔记本jupyterwidgets
1条回答
网友
1楼 · 发布于 2024-04-27 07:52:31

好的,解决办法比我想象的简单。在Jupyter实验室中,转到Settings > Save Widget State Automatically,并确保选中该选项

或者,您可以转到~/.jupyter/lab/user-settings/@jupyter-widgets/jupyterlab-manager/plugin.jupyterlab-settings并按如下方式配置设置

{
    // Jupyter Widgets
    // @jupyter-widgets/jupyterlab-manager:plugin
    // Jupyter widgets settings.
    // ******************************************

    // Save Jupyter widget state in notebooks
    // Automatically save Jupyter widget state when a notebook is saved.
    "saveState": true
}

解决方案不是很清楚,但这个page给了我一些提示

相关问题 更多 >