在 Flask 中渲染 Jupyter Notebook Iframe

Render a Jupyter Notebook Iframe in Flask

我正在使用 Flask 为单个用户托管 UI。我一直在尝试做的事情是设置一种方法,让用户单击一个按钮,使用标记语言将一些文本和图像插入文档中预先指定的位置。我最初为此使用 Jinja2,但问题是用户需要能够在插入数据后修改文档,以防他们需要对文本进行小的更改或添加一行等......据我所知,这不能'使用烧瓶渲染的模板无法完成。

我知道可以使用 iframe 将 Jupyter Notebook(基于标记语言)引入 UI,但我没有成功。

我试过的:

  1. 将我的 Jupyter notebook 配置为 public
  2. 用我的 public Jupyter 地址放置一个 iframe <iframe width="400" height="400" src="http://10.33.75.112:8888/notebooks/Desktop/Tool/test.ipynb"/> 进入我的 HTML 模板文件...它会弹出一个我无法与之交互的空白 Iframe
  3. 用 NBviewer 和 NBconvert 搞砸了,看看是否有某种方法可以整合它,但没有任何成功。

想法?

我找到了解决办法。

在 Jupyter-notebook-config.py 文件夹中,您需要取消注释和修改几个选项才能进行此操作。

c.NotebookApp.allow_origin = '*' #Basic permission
c.NotebookApp.disable_check_xsrf = True #Otherwise Jupyter restricts you modifying the Iframed Notebook
c.NotebookApp.ip = 'ENTER_YOUR_JUPYTER_IP_ADDRESS' #Appears in the top browser bar when you launch jupyter
c.NotebookApp.port = 8888 #Your choice of port
c.NotebookApp.token = '' #In my case I didn't want to deal with security
c.NotebookApp.trust_xheaders = True #May or may not make a difference to you

c.NotebookApp.tornado_settings = {
'headers': {
'Content-Security-Policy': "frame-ancestors 'http://127.0.0.1:5000/' 'self' "
}
} #assuming your Flask localhost is 127.0.0.1:5000

然后在 HTML:

<iframe width="1000" height="1000"  src="http://ENTER_YOUR_JUPYTER_IP_ADDRESS:8888/notebooks/Desktop/test.ipynb"/>

编辑:此外 Google Chrome 在显示这样的 iframe 时出错,因此请使用 Mozilla 或 IE。