gpt4 book ai didi

html - 在网页中嵌入 Jupyter HTML 输出

转载 作者:行者123 更新时间:2023-12-05 09:19:13 29 4
gpt4 key购买 nike

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

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

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

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

有没有办法克服这些问题,以便将 Jupyter 的输出嵌入到我自己的网页中?

最佳答案

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

你有 2 个选项。

  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")<br/>
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")<br/>
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

关于html - 在网页中嵌入 Jupyter HTML 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41520034/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com