【问题标题】:Not able to embed Jupyterlab running on Jupyterhub into an iframe无法将在 Jupyterhub 上运行的 Jupyterlab 嵌入 iframe
【发布时间】:2020-02-17 06:44:30
【问题描述】:

我正在尝试将与 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'".

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

【问题讨论】:

  • 不同浏览器的 iframe 行为可能不同,例如Firefox vs chrome。

标签: jupyter-notebook jupyter jupyter-lab jupyterhub


【解决方案1】:

这是一个解决方法,

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

按照以下步骤进行:

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

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

jupyter notebook --generate-config

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

如果您在安装后遇到与 ruamel.yaml 相关的错误 在加载 tljh-config 时执行以下命令: 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' "}}

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

【讨论】:

  • 我尝试了上述方法,但我仍然面临同样的错误。我正在为 iframe 使用一个简单的 HTML 文件。框架祖先值应该是什么?
  • 试试这个:
  • 我在单独的机器上运行 jupyterhub。我试过 。但我仍然面临同样的问题。拒绝在框架中显示“10.201.11.44:8002/hub/login?next=%2Fhub%2Fuser%2Fadmin%2Flab”,因为祖先违反了以下内容安全策略指令:“frame-ancestors * 'self'”。
  • 我得到了 iframe 的工作,我现在得到了登录页面。但是,一旦我尝试登录,就会收到以下错误:- 阻止自动对焦于跨域子框架中的表单控件。
  • 这就是我能够实现 iframe 嵌入的方式。我按照这里的安装教程:tljh.jupyter.org/en/latest,然后做了上述工作。确保添加 letencrypt 免费 ssl 证书 - tljh.jupyter.org/en/latest/howto/admin/https.html – Amal Vijayan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 2016-01-15
  • 1970-01-01
  • 2012-04-05
  • 2019-12-08
  • 2020-04-12
  • 1970-01-01
相关资源
最近更新 更多