安装jupyter lab
过程不再赘述。
测试
设置所有本机所有IP均可访问,服务端口为8080,设置启动目录为/root/ipynbs
。
由于当前用户为root
,出于安全考虑,jupyter lab
需要设置--allow-root
才能运行。
(base) [root@ecs-9e76 ~]# jupyter lab --ip=0.0.0.0 --port=8080 --notebook-dir='/root/ipynbs' --allow-root
[I 22:49:45.533 LabApp] JupyterLab extension loaded from /root/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 22:49:45.533 LabApp] JupyterLab application directory is /root/anaconda3/share/jupyter/lab
[I 22:49:45.535 LabApp] Serving notebooks from local directory: /root/ipynbs
[I 22:49:45.535 LabApp] The Jupyter Notebook is running at:
[I 22:49:45.535 LabApp] http://ecs-9e76:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d
[I 22:49:45.535 LabApp] or http://127.0.0.1:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d
[I 22:49:45.535 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 22:49:45.538 LabApp] No web browser found: could not locate runnable browser.
[C 22:49:45.538 LabApp]
To access the notebook, open this file in a browser:
file:///root/.local/share/jupyter/runtime/nbserver-13713-open.html
Or copy and paste one of these URLs:
http://ecs-9e76:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d
or http://127.0.0.1:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d
访问http://xx.xx.xx.xx:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d
即可访问Jupyter Lab
。
设置密码访问
jupyter lab
默认采用token访问,不够方便,设置采用密码访问。
输入jupyter notebook password
,按提示输入密码。
密码文件保存在配置文件/root/.jupyter/jupyter_notebook_config.json
中。
(base) [root@ecs-9e76 ~]# jupyter notebook password
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json
(base) [root@ecs-9e76 ~]# vi /root/.jupyter/jupyter_notebook_config.json
{
"NotebookApp": {
"password": "sha1:d933b9053eb0:55de430361c7686fdc3ce5462af9804637ead074"
}
}
修改配置文件
前面测试中直接用参数配置IP、端口等信息,为了便于使用,可在配置文件中做相关配置。
jupyter lab
的配置文件有两种:jupyter_notebook_config.py
和jupyter_notebook_config.json
。
两个配置文件选择其一即可。
1. 通过jupyter_notebook_config.py
文件
- 生成配置文件
jupyter_notebook_config.py
输入jupyter notebook --generate-config
命令,生成jupyter_notebook_config.py
文件。
(base) [root@ecs-9e76 ~]# jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py
- 修改配置文件
(base) [root@ecs-9e76 ~]# vi /root/.jupyter/jupyter_notebook_config.py
修改配置文件,确保以下选项为未注释状态:
c.NotebookApp.ip = '*'
c.NotebookApp.port = 8080
c.NotebookApp.open_browser = False
c.NotebookApp.notebook_dir = '/root/ipynbs'
c.NotebookApp.allow_root = True
2. 通过jupyter_notebook_config.json
文件
注意,jupyter_notebook_config.json
文件原始内容为:
{
"NotebookApp": {
"password": "sha1:d933b9053eb0:55de430361c7686fdc3ce5462af9804637ead074"
}
}
修改上述配置后,对应的jupyter_notebook_config.json
文件内容如下:
(base) [root@ecs-9e76 ~]# vi /root/.jupyter/jupyter_notebook_config.json
{
"NotebookApp": {
"password": "sha1:d933b9053eb0:55de430361c7686fdc3ce5462af9804637ead074",
"ip" : "*" ,
"port" : 8080,
"open_browser" : false ,
"notebook_dir" : "/root/ipynbs" ,
"allow_root": true
}
}
运行效果如下:
(base) [root@ecs-9e76 ~]# jupyter lab
[W 00:47:39.022 LabApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 00:47:39.028 LabApp] JupyterLab extension loaded from /root/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 00:47:39.028 LabApp] JupyterLab application directory is /root/anaconda3/share/jupyter/lab
[I 00:47:39.030 LabApp] Serving notebooks from local directory: /root/ipynbs
[I 00:47:39.030 LabApp] The Jupyter Notebook is running at:
[I 00:47:39.030 LabApp] http://ecs-9e76:8080/
[I 00:47:39.030 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
以系统服务形式运行jupyter lab
创建/etc/systemd/system/jupyter.service
文件,文件内容如下:
[root@ecs-9e76 ~]# vi /etc/systemd/system/jupyter.service
[Unit]
Description=jupyterlab
After=network.service
[Service]
ExecStart=/root/anaconda3/bin/jupyter lab
[Install]
WantedBy=default.target
启动服务:systemctl start jupyter
[root@ecs-9e76 ~]# systemctl start jupyter
开机启动:systemctl enable jupyter
[root@ecs-9e76 ~]# systemctl enable jupyter
Created symlink from /etc/systemd/system/default.target.wants/jupyter.service to /etc/systemd/system/jupyter.service.