jupyter安装系列
需要说明:所有操作需要在root权限下进行!
因为我需要安装jupyterhub,目前该组件只支持linux系统,因此我选择了64位的ubuntu18.04系统。
jupyter系列有很多安装路径,我选择使用Anaconda3.
一、安装Anaconda
1、下载
清华镜像:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
2、安装
(base) root@jupyter:~# bash Anaconda3-5.3.1-Linux-x86_64.sh
按照安装提示,接受安装许可,各种输入“Yes”即可
3、测试
(base) root@jupyter:~# conda --version conda 4.6.8
二、测试jupyter notebook
Anaconda安装完毕后,jupyter notebook已经安装成功了!
1、测试启动
(base) root@jupyter:~#jupyter notebook --allow-root
2、登录
此时可以通过本地IP和默认端口号登录jupyter,比如http://10.1.20.64:8888
三、安装jupyter lab
jupyter lab是jupyter notebook的升级版,解决了jupyter notebook的很多bug,为用户提供更具生产力的jupyter开发环境,于是我就决定尝试一下jupyterLab。
1、安装
(1)通过conda命令安装
(base) root@jupyter:~# conda install jupyter lab
(2)通过pip命令安装
(base) root@jupyter:~# python Python 3.7.2 (default, Dec 29 2018, 06:19:36) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> pip install jupyter lab
2、启动测试
1 jupyter lab --allow-root
输出中指定了通过什么端口登录jupyter lab,如:
The Jupyter Lab is running at: http://localhost:8889/
四、jupyter远程登录
截止到之前的步骤,jupyter服务都只能在安装本机上访问,无法进行远程访问。
1、指定配置文件
(base) root@jupyter:~# jupyter lab --generate-config
Writing default config to: ~/.jupyter/jupyter_notebook_config.py
2、生成登录密码及密文密码
(base) root@jupyter:~# python Python 3.7.2 (default, Dec 29 2018, 06:19:36) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> exit() (base) root@jupyter:~# ipython3 Python 3.7.2 (default, Dec 29 2018, 06:19:36) Type 'copyright', 'credits' or 'license' for more information IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: from notebook.auth import passwd In [2]: passwd() Enter password: Verify password:
Out [2] :'sha1:ce23d945972f:34769685a7ccd3d08c84a18c63968a41f1140274'
请记住password,登录jupyterLab要使用;
请记住sha1:ce23d945972f:34769685a7ccd3d08c84a18c63968a41f1140274,需要设置在配置文件中。
该密文密码保存在文件“jupyter_notebook_config.json”中。
3、修改配置文件
编辑文件jupyter_notebook_config.py。
1 c.NotebookApp.allow_origin = '*' 2 c.NotebookApp.allow_remote_access = True #允许远程登录 3 c.NotebookApp.ip = '*' #监听所有IP 4 c.NotebookApp.notebook_dir = '/home/hnc/jupyter/jupyter_run' #jupyter运行空间路径 5 c.NotebookApp.open_browser = False 6 c.NotebookApp.password = u'sha1:ce23d945972f:34769685a7ccd3d08c84a18c63968a41f1140274'
7 c.NotebookApp.port = 8891
4、远程登录
jupyter lab --config-file /root/.jupyter/jupyter_notebook_config.py --allow-root
登录地址:http://10.1.20.64:8891/
输入设置的登录密码:
登录成功:
五、安装jupyterhub
截止以上步骤,jupyter服务器只能提供一个统一的工作空间,无法实现多用户管理和多用户访问隔离,因此需要安装jupyterhub。
1、安装依赖包
(1)安装
(base) root@jupyter:~# apt-get install npm nodejs
(2)测试
(base) root@jupyter:~# node -v v11.11.0 (base) root@jupyter:~# npm -v 6.7.0
2、安装服务代理
(base) root@jupyter:~# npm install -g configurable-http-proxy
测试:
(base) root@jupyter:~# configurable-http-proxy --version 4.0.1
3、安装jupyterhub
(1)通过conda命令安装
(base) root@jupyter:~# conda install jupyterhub
(2)通过pip命令安装
(base) root@jupyter:~# python Python 3.7.2 (default, Dec 29 2018, 06:19:36) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> pip install jupyterhub
(3)测试
(base) root@jupyter:~# jupyterhub --version 0.9.4
4、配置文件
(base) root@jupyter:~# jupyterhub --generate-config
默认还是在~/.jupyter/upyterhub_config.py,更改配置:
1 c.Authenticator.delete_invalid_users = True 2 c.JupyterHub.authenticator_class = 'sqliteauthenticator.SQLiteAuthenticator' 3 c.JupyterHub.bind_url = 'http://:8000' 4 c.JupyterHub.hub_bind_url = 'http://127.0.0.1:8082' 5 c.JupyterHub.hub_port = 8082 6 c.JupyterHub.statsd_prefix = 'jupyterhub' 7 c.Spawner.cmd = ['jupyter-labhub'] 8 c.Spawner.notebook_dir = '/home/{username}' 9 c.Authenticator.admin_users = {'hnc'} 10 c.LocalAuthenticator.add_user_cmd = ['adduser', '-q', '-ingroup', 'fengbingyan', '--gecos', '""', '--home', '/home/USERNAME', '--disabled-password', '--force-badname'] 11 c.LocalAuthenticator.create_system_users = True
有几个参数需要说明一下:
(1)c.JupyterHub.authenticator_class,默认值是 'jupyterhub.auth.PAMAuthenticator',一般采用默认值寄即可,笔者实现了自己的用户验证类。
(2)c.Spawner.cmd,默认值是c.Spawner.cmd = ['jupyterhub-singleuser'],jupyterhub认证成功后,进入jupyter notebook的界面;
笔者将其设置为jupyterhub,指定jupyterhub认证成功后,进入jupyter lab的界面。
(3)c.LocalAuthenticator.add_user_cmd,该值一般为默认值即可,笔者是因为有时有以“手机号”创建用户的需求,所以改写了该命令;
(4)笔者没有设置列表白名单/组白名单、黑名单,表示所有通过认证的用户都可以登录。
5、启动服务
(base) root@jupyter:~# jupyterhub -f /root/.jupyter/jupyterhub_config.py --no-ssl
六、iframe集成
在集成过程中遇到跨域问题:
an ancestor violates the following Content Security Policy directive: “frame-ancestors ‘self’
我的解决方法(请根据具体应用修改端口号):
1、修改jupyterhub_config.py文件
1 c.Spawner.args = ['--NotebookApp.tornado_settings={"headers":{"Content-Security-Policy": "frame-ancestors * self *:8080;"}}'] 2 c.JupyterHub.tornado_settings = { 'headers': { 'Content-Security-Policy': "frame-ancestors * self *:8080; "} }
2、修改jupyter_notebook_config.py文件
1 c.NotebookApp.disable_check_xsrf = True 2 c.NotebookApp.tornado_settings = {"headers":{"Content-Security-Policy": "frame-ancestors * self *:8080;"}} 3 c.NotebookApp.trust_xheaders = True
3、在jupyter_notebook_config.py同级目录创建文件custom/custom.js文件,输入以下内容:
1 define(['base/js/namespace'], function(Jupyter){ 2 Jupyter._target = '_self'; 3 });
权限修改:
(base) root@jupyter:~# chmod 777 ~/.jupyter/custom