JupyterLab Server 搭建与使用笔记

Python中文社区

共 4752字,需浏览 10分钟

 · 2020-12-08

两三个月前,有幸拿到了云筏的一个 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.


1. 安装

JupyterLab 可以通过condapippipenvordocker的方式安装。

# conda
conda install -c conda-forge jupyterlab

#
 pip
pip install jupyterlab

nb_conda_kernels 是一个 Jupyter 的扩展功能(extension),可以管理多个 Conda 环境下的 Jupyter 核心(kernels),整合多 Conda 环境中各种不同版本的 Python 或 R 等语言。

1.1 安裝 nb_conda_kernels

nb_conda_kernels 必须安装在在 Jupyter(Jupyter Notebook 或 JupyterLab)所在的 Conda 环境中,可以直接放在 base 环境中,或是另外建立独立的 Conda 环境亦可。

这里我们建立一哥新的 Conda 环境,专门用于放置 JupyterLab:

# 建立 Conda 环境,并安裝 JupyterLab
conda create --name jupyterlab python=3.7 jupyterlab

在此 Conda 环境加装 nb_conda_kernels 扩展功能套件:

# 安装 nb_conda_kernels 扩展功能套件
conda install --name jupyterlab nb_conda_kernels

1.2 安装 Jupyter 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

2. 启动

安装好所有的 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 环境下的各种语言与版本了。

3. 配置远程访问

3.1 进入 ipython 环境,生成秘钥

假设你已经进入了 ipython 环境,参考下面的操作生成秘钥。

In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:        #填写你的密码(用于访问jupyterlab的密码)
Verify password:    #再次填写你的密码
Out[2]: 'argon2:$argon2id$v=19$m=10240,t=10,p=8$ShdM5...vHBHA$EUCjIE1L8joDbugmWIDqLw'
#这是你的密码生成的密钥

3.2 生成 jupyterlab 配置文件

使用--generate-config参数,生成 jupyterlab 配置文件。

$ jupyter lab --generate-config
Writing default config to: /home/shenweiyan/.jupyter/jupyter_notebook_config.py

3.3 修改配置文件

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的浏览器打开jupyter
c.NotebookApp.open_browser = False 
# 监听端口设置为8888或其他
c.NotebookApp.port = 8888
# 允许远程访问 
c.NotebookApp.allow_remote_access = True

接下来输入jupyter lab 就可以启动 jupyter-lab 服务啦!

4. 域名配置

下面是 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;
}

5. 遇到的问题

  1. jupyter lab 启动的过程中,如果使用--config=~/.jupyter/jupyter_notebook_config.py作为启动参数之一,会导致其他的 Conda 环境在 web 端消失,只剩下一个 Python 3 的 Notebook 和 Console,具体原因目前没搞明白。

  2. jupyter lab --generate-config默认生成的配置文件位于~/.jupyter/jupyter_notebook_config.py,目前不太清楚如何更改这个默认配置文件的路径。

更多阅读



有人在代码里下毒!慎用 pip install 命令


pip 20.3 新版本发布!即将抛弃 Python 2.x


5分钟快速掌握 scrapy 爬虫框架


特别推荐


程序员摸鱼指南


为你精选的硅谷极客资讯,
来自FLAG巨头开发者、技术、创投一手消息




点击下方阅读原文加入社区会员

浏览 7
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报