0

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

import ipywidgets as widgets

widgets.IntSlider()

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

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.executejupyter_sphinx(但这并不重要,因为使用其中任何一个仍然不会呈现小部件)。

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

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

jupyter 客户端 6.1.3
jupyter 控制台 6.1.0
jupyter 核心 4.6.3
jupyter 服务器代理 1.5.0
jupyter-sphinx 0.3.2
jupyterlab 2.1.4
jupyterlab 评论服务 0.2
jupyterlab 服务器 1.1.5
nbconvert 6.0.7
nbsphinx 0.8.0
狮身人面像自动构建 2020.9.1
狮身人面像 rtd 主题 0.5.0
狮身人面像站点地图 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 源代码,您所看到的只是widgets.IntSlider()其中一个单元格(没有什么特别的事情发生)。根据 nbconvert 的说法,从 4.3 版开始可以实时渲染小部件。

关于我缺少哪些其他配置或步骤的任何想法?

4

1 回答 1

0

好的,解决方案比我想象的要容易。在 Jupyter Lab 中,转到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
}

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

于 2020-12-01T02:09:57.983 回答