2

我想在我自己的网页中嵌入 Jupyter 的 HTML 输出。这样做的主要原因是,我可以从我自己的 web 应用程序中使用 Jupyter——还可以通过互联网从世界任何地方访问我的研究笔记本。

一个典型的用例场景是我点击页面上的一个按钮,一个 iframe 将插入到我的页面中;然后 Jupyter 将在后端启动(如果尚未运行),Jupyter 的输出将通过管道传输到 iframe - 这样我就可以在我的页面中使用 Jupyter。

它出现的幼稚解决方案是使用<iframe>,但有两个问题:

  1. iframe 跨域策略问题
  2. Jupyter 首次启动时生成一次性身份验证令牌

无论如何我可以克服这些问题,所以我可以将 Jupyter 的输出嵌入到我自己的网页中吗?

4

3 回答 3

3

您可以使用html_embed 预处理器直接执行此操作:

$ jupyter nbconvert  --to html_embed  Annex.ipynb
[NbConvertApp] Converting notebook Annex.ipynb to html_embed
/usr/local/lib/python3.6/site-packages/nbconvert/filters/datatypefilter.py:41: UserWarning: Your element with mimetype(s) dict_keys(['image/pdf']) is not able to be represented.
  mimetypes=output.keys())
[NbConvertApp] Writing 2624499 bytes to Annex.html

奇怪的是,我在nbconvert的手册中找不到直接参考。

于 2018-11-27T14:35:56.890 回答
3

你需要检查 nbconvert - https://github.com/jupyter/nbconvert

你有2个选择。

  1. 使用命令行运行笔记本,然后让一些 Web 服务器到服务器 .html
  2. 使用 python 和 nbconvert 库

这是短代码:如果您想显示已生成:

from nbconvert.preprocessors import ExecutePreprocessor import nbformat from nbconvert import HTMLExporter from nbconvert.preprocessors.execute import CellExecutionError src_notebook = nbformat.reads(ff.read(), as_version=4) #where ff is file opened with some open("path to notebook file")
html_exporter = HTMLExporter() html_exporter.template_file = 'basic' #basic will skip generating body and html tags.... use "all" to gen all.. (body, resources) = html_exporter.from_notebook_node(src_notebook) print(body) #body have html output

如果你还想运行笔记本,那么:

from nbconvert.preprocessors import ExecutePreprocessor import nbformat from nbconvert import HTMLExporter from nbconvert.preprocessors.execute import CellExecutionError src_notebook = nbformat.reads(ff.read(), as_version=4) #where ff is file opened with some open("path to notebook file")
ep = ExecutePreprocessor(timeout=50, kernel_name='python3') ep.preprocess(src_notebook, {}) html_exporter = HTMLExporter() html_exporter.template_file = 'basic' #basic will skip generating body and html tags.... use "all" to gen all.. (body, resources) = html_exporter.from_notebook_node(src_notebook) print(body) #body have html output

于 2017-02-01T13:28:19.877 回答
1

您可以使用 ipython nbconvert - -to html notebook.ipynb 来获取相同的 html 代码。这是有关如何使用 IPython 笔记本写博客的指南 -请参见此处

如果您的网站是用 python 编写的,请使用 python embed docs 还有这个教程 -见这里

或使用 kyso.io 以下是如何使用 Kyso 平台嵌入 Jupyter -请参见此处

(免责声明-我是kyso的创始人)

于 2018-02-01T02:21:59.910 回答