【问题标题】:Embed Jupyter HTML output in a web page在网页中嵌入 Jupyter HTML 输出
【发布时间】:2017-01-07 09:42:34
【问题描述】:

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

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

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

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

我是否可以克服这些问题,以便将 Jupyter 的输出嵌入到我自己的网页中?

【问题讨论】:

标签: html iframe jupyter-notebook jupyter jupyterhub


【解决方案1】:

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

你有两个选择。

  1. 使用命令行运行笔记本,然后让一些网络服务器 到服务器 .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

【讨论】:

  • +1 供您输入。这听起来像我正在尝试做的。我会试试看,如果有效,我会接受你的回答。顺便说一句,看起来您正在导入一些非标准模块(以访问 nbformat 和 HTMLExporter)-您从哪个模块导入?谢谢
  • 我添加了导入......它实际上是从应用程序中捕捉的......请记住,笔记本的执行速度并不快......我通常在后台运行一些 cron 来运行笔记本,而脚本只是转换而不执行...
  • 抱歉,我完全不知道你的陈述是什么意思:“我添加了导入......它实际上是从应用程序中获取的。” .请澄清。
【解决方案2】:

您可以使用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 的 manual 中找不到直接引用。

【讨论】:

    【解决方案3】:

    您可以使用 ipython nbconvert --to html notebook.ipynb 来获取相同的 html 代码。 以下是如何使用 IPython 笔记本写博客的指南 - see here

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

    或使用 kyso.io 这是使用 Kyso 平台嵌入 Jupyter 的方法 - see here

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 2022-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 2015-03-13
      相关资源
      最近更新 更多