无法将 Jupyterhub 上的 Jupyterlab 运行 嵌入到 iframe 中

Not able to embed Jupyterlab running on Jupyterhub into an iframe

我正在尝试将 jupyterlab 运行 jupyterhub 集成到 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'".

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

这里有一个解决方法,

  • 为所有使用的用户提供一个通用的笔记本配置文件 登录 Jupyterhub。
  • 将配置文件作为生成器参数传递。

按照以下步骤操作:

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

You can either create the file manually using a text editor like vim or generate a default coinfig file using jupyter notebook with the following command

jupyter notebook --generate-config

Note that the above command requires jupyter notebook to be installed (pip3 install jupyterhub notebook)

After the installation if you face an error related to ruamel.yaml version on loading the tljh-config, execute the following command: pip3 install ruamel.yaml==0.15.*

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

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

change the permission of the file using the following code:

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

Note: you can generate jupyterhub configuration file using the following command: 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' "}}

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