首页 > 解决方案 > 无法将在 Jupyterhub 上运行的 Jupyterlab 嵌入 iframe

问题描述

我正在尝试将与 jupyterhub 一起运行的 jupyterlab 集成到 iframe 中。我在配置文件中进行了所有必要的更改。在 jupyter_notebook_config.py 中,我做了以下更改。

c.NotebookApp.tornado_settings = {'headers': {
    'Access-Control-Allow-Origin': '*',
    'Content-Security-Policy': 'frame-ancestors http://localhost:9005'
  }}

在 jupyterhub_config.py 中,我添加了以下内容

c.JupyterHub.tornado_settings = {'headers': {
    'Access-Control-Allow-Origin': '*',
    'Content-Security-Policy': 'frame-ancestors http://localhost:9005'
  }}

但是,当我尝试在 iframe 中打开http://localhost:8002/user/admin/lab URL 时,我收到以下错误

Refused to display 'http://localhost:8002/user/admin/lab' in a frame because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'self'".

如果我遗漏了什么或者我的配置有问题,有人可以告诉我吗?

标签: jupyter-notebookjupyterjupyter-labjupyterhub

解决方案


这是一个解决方法,

  • 为所有登录 Jupyterhub 的用户提供一个通用的笔记本配置文件。
  • 将配置文件作为 spawner args 传递。

请按照以下步骤操作:

  1. 在特定位置创建一个名为 jupyter_notebook_config.py 的文件,例如 /home/shared_config/

您可以使用 vim 等文本编辑器手动创建文件,也可以使用 jupyter notebook 使用以下命令生成默认的 coinfig 文件

jupyter notebook --generate-config

注意上面的命令需要安装jupyter notebook(pip3 install jupyterhub notebook)

安装后,如果在加载 tljh-config 时遇到与 ruamel.yaml 版本相关的错误,请执行以下命令:pip3 install ruamel.yaml==0.15.*

  1. 打开您在上面创建的 jupyter_notebook_config.py 文件并添加以下代码:

    c.NotebookApp.tornado_settings={'headers': {'Content-Security-Policy': "frame-ancestors * 'self'"}}

使用以下代码更改文件的权限:

chmod -R 755 /home/shared_config/jupyter_notebook_config.py
  1. 打开 jupyterhub 配置文件(默认位于 /opt/tljh/config/jupyterhub_config.d/jupyterhub_config.py)并添加以下代码:

注意:您可以使用以下命令生成 jupyterhub 配置文件:jupyterhub --generate-config

c.Spawner.args = [ '--config=/home/shared_config/jupyter_notebook_config.py']
  1. 使用以下命令重新加载 tljf-config:

    sudo tljf-config 重新加载

快乐编码!

这是我的配置的样子

-- /opt/tljh/config/jupyterhub_config.d/jupyterhub_config.py

c.JupyterHub.tornado_settings = {'headers': {'Content-Security-Policy': "frame-ancestors * 'self' "}}
c.Spawner.args = [ '--config=/home/ubuntu/jupyter_notebook_config.py']

-- /home/shared_config/jupyter_notebook_config.py

c.NotebookApp.tornado_settings={'headers': {'Content-Security-Policy': "frame-ancestors * 'self' "}}

一些有用的参考资料和相关问题:


推荐阅读