两三个月前,有幸拿到了云筏的一个 4 核 16G,1TB硬盘,300M带宽位于欧洲的云服务器,自带的开箱即用的 RStudio Server 也非常给力,但最近这两天在升级 R 的时候遇上了不少问题,也懒得去折腾了,于是想把 RStudio Server 替换成自己比较熟悉的 JupyterLab Server,这是一些折腾的笔记记录。
JupyterLab is a next-generation web-based user interface for Project Jupyter. JupyterLab enables you to work with documents and activities such as Jupyter notebooks, text editors, terminals, and custom components in a flexible, integrated, and extensible manner.
JupyterLab 可以通过conda
,pip
,pipenv
ordocker
的方式安装。
# condaconda install -c conda-forge jupyterlab# pippip install jupyterlab
nb_conda_kernels 是一个 Jupyter 的扩展功能(extension),可以管理多个 Conda 环境下的 Jupyter 核心(kernels),整合多 Conda 环境中各种不同版本的 Python 或 R 等语言。
nb_conda_kernels 必须安装在在 Jupyter(Jupyter Notebook 或 JupyterLab)所在的 Conda 环境中,可以直接放在 base
环境中,或是另外建立独立的 Conda 环境亦可。
这里我们建立一哥新的 Conda 环境,专门用于放置 JupyterLab:
# 建立 Conda 环境,并安裝 JupyterLabconda create --name jupyterlab python=3.7 jupyterlab
在此 Conda 环境加装 nb_conda_kernels 扩展功能套件:
# 安装 nb_conda_kernels 扩展功能套件conda install --name jupyterlab nb_conda_kernels
所有需要在 Jupyter 中透过nb_conda_kernels
来使用 Conda 环境,都需要安装对应语言的的 Jupyter 核心(kernels)。
建立一个 Python 2.7 的 Conda 环境:
# 建立 Python 2.7 的 Conda 环境conda create --name Python2 python=2.7.18# 安装 Python 的 Jupyter 核心conda install --name Python ipykernel
建立一个 R-4.0.2 的 Conda 环境:
# 建立 R 4.0.2 的 Conda 环境conda create --name R-4.0.2 --channel r r-essentials=4.0.2 r-base=4.0.2# 安装 R 的 Jupyter 核心conda install --name R-4.0.2 --channel r r-irkernel
安装好所有的 Conda 环境以及对应的 Jupyter 核心之后,就可以依照一般的方式启动 JupyterLab:
# 载入 Jupyter 的 Conda 环境(下面两种方式都可以激活)$ conda activate jupyterlab $ source activate jupyterlab# 启动 JupyterLab$ jupyter lab --ip 0.0.0.0 --port 9090
JupyterLab 各种 Conda 环境与语言版本
这样就可以在单一一个 JupyterLab 界面中同时使用不同 Conda 环境下的各种语言与版本了。
假设你已经进入了 ipython 环境,参考下面的操作生成秘钥。
In [1]: from notebook.auth import passwdIn [2]: passwd()Enter password: #填写你的密码(用于访问jupyterlab的密码)Verify password: #再次填写你的密码Out[2]: 'argon2:$argon2id$v=19$m=10240,t=10,p=8$ShdM5...vHBHA$EUCjIE1L8joDbugmWIDqLw'#这是你的密码生成的密钥
使用--generate-config
参数,生成 jupyterlab 配置文件。
$ jupyter lab --generate-configWriting default config to: /home/shenweiyan/.jupyter/jupyter_notebook_config.py
JupyterLab 默认配置文件位于~/.jupyter/jupyter_notebook_config.py
。
更改内容如下:
# 将ip设置为*,允许任何IP访问c.NotebookApp.ip = '*'# 这里的密码填写上面生成的密钥c.NotebookApp.password = 'argon2:$argon2id$v=19$m=10240,t=10,p=8$ShdM5...vHBHA$EUCjIE1L8joDbugmWIDqLw'# 禁止用host的浏览器打开jupyterc.NotebookApp.open_browser = False # 监听端口设置为8888或其他c.NotebookApp.port = 8888# 允许远程访问 c.NotebookApp.allow_remote_access = True
接下来输入jupyter lab
就可以启动 jupyter-lab 服务啦!
下面是 JupyterLab 通过 nginx 转发的一个示例。
upstream jupyter { server 127.0.0.1:9090;}server { listen 80; server_name jupyter.bioitee.com; rewrite ^/(.*) https://jupyter.bioitee.com/$1 permanent;}server{ listen 443 ssl; index index.html index.htm index.php default.html default.htm default.php; server_name jupyter.bioitee.com; root /data/apps/jupyterlab/notebooks; ssl_certificate ../certs/4564529_jupyter.bioitee.com.pem; ssl_certificate_key ../certs/4564529_jupyter.bioitee.com.key; ssl_ciphers "EECDH CHACHA20:EECDH CHACHA20-draft:EECDH AES128:RSA AES128:EECDH AES256:RSA AES256:EECDH 3DES:RSA 3DES:!MD5"; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; location / { proxy_pass http://jupyter; proxy_set_header Host $host; } location ~ /api/kernels/ { proxy_pass http://jupyter; proxy_set_header Host $host; # websocket support proxy_http_version 1.1; proxy_set_header Upgrade "websocket"; proxy_set_header Connection "Upgrade"; proxy_read_timeout 86400; } location ~ /terminals/ { proxy_pass http://jupyter; proxy_set_header Host $host; # websocket support proxy_http_version 1.1; proxy_set_header Upgrade "websocket"; proxy_set_header Connection "Upgrade"; proxy_read_timeout 86400; } access_log logs/jupyterlab_acc.log; error_log logs/jupyterlab_err.log;}
jupyter lab 启动的过程中,如果使用--config=~/.jupyter/jupyter_notebook_config.py
作为启动参数之一,会导致其他的 Conda 环境在 web 端消失,只剩下一个 Python 3 的 Notebook 和 Console,具体原因目前没搞明白。
jupyter lab --generate-config
默认生成的配置文件位于~/.jupyter/jupyter_notebook_config.py
,目前不太清楚如何更改这个默认配置文件的路径。
往期文章回顾
本文分享自微信公众号 - 生信科技爱好者(bioitee)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章