jupyter-notebook - 无法将在 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'".
如果我遗漏了什么或者我的配置有问题,有人可以告诉我吗?
解决方案
这是一个解决方法,
- 为所有登录 Jupyterhub 的用户提供一个通用的笔记本配置文件。
- 将配置文件作为 spawner args 传递。
请按照以下步骤操作:
- 在特定位置创建一个名为 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.*
打开您在上面创建的 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
- 打开 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']
使用以下命令重新加载 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' "}}
一些有用的参考资料和相关问题:
- https://jupyterhub.readthedocs.io/en/stable/getting-started/config-basics.html
- https://minrk-notebook.readthedocs.io/en/latest/config.html
- https://jupyterhub.readthedocs.io/en/stable/getting-started/spawners-basics.html
- https://github.com/jupyterhub/jupyterhub/issues/335
- https://github.com/jupyterhub/the-littlest-jupyterhub/issues/312
- https://github.com/jupyter/notebook/issues/3904
- https://github.com/jupyterhub/jupyterhub/issues/379
推荐阅读
- javascript - 2 函数异步结果 2.get,post 函数
- php - phalcon phql 在 where 中查找带有数组元素的查询
- postgresql - 公共架构上新用户的默认权限?
- sql - 如何在Oracle SQL中将excel序列日期号转换为日期
- rethinkdb-python - Rethinkdb:在整个表中搜索一个字符串(全文搜索)
- cuda - NV 工具包扩展包括文件位置 - Linux 与 Windows
- kubernetes - K8s ingress:nginx入口控制器未处于运行模式
- python - 如何根据内容裁剪图像(Python 和 OpenCV)?
- javascript - Interrupt a drag&drop in progress
- c++ - SDL 2.0.4 on Ubuntu 16.04 - Multiple displays fullscreen problem