CentOS 线上搭建 jupyter_server 笔记

时间:2019-11-17
本文章向大家介绍CentOS 线上搭建 jupyter_server 笔记,主要包括CentOS 线上搭建 jupyter_server 笔记使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、背景


为公司负责 Data Science 的同事配置线上 jupyter_server (jupyter + jupyter_kernel_gateway)环境。

二、环境


CentOS 7.6

三、安装


从最基础的 python 安装开始介绍。

1、python

采用编译安装,版本为 3.7.0,教程:

https://www.centos.bz/2018/01/%E5%9C%A8centos%E4%B8%8A%E5%AE%89%E8%A3%85python3%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E6%B3%95/

2、anaconda

下载地址:

https://www.anaconda.com/distribution/

教程:

https://linuxize.com/post/how-to-install-anaconda-on-centos-7/

坑:安装完 anaconda,发现 conda 并没有自动配到环境变量中,去anaconda找,没找到,其实在这里:/mnt/ds/anaconda3/etc/profile.d/conda.sh

3、jupyter

Anaconda 中自带 jupyter,无需安装。(但记得配环境变量)

4、jupyter_kernel_gateway

pip3 install jupyter_kernel_gateway

坑:因为我的 ssh 账号不是 root,所以用 pip 时,不能安装到没有权限的系统目录(如/usr/local/lib/python3.7),所以需要加上--user,这样会在我的主目录中创建 pip 安装包了。

四、配置与启动


1、jupyter

(1)创建配置文件

jupyter notebook --generate-config

配置文件会默认创建在 ~\.jupyter\jupyter_notebook_config.py

(2)修改配置文件

修改~\.jupyter\jupyter_notebook_config.py的这几个地方:


## 相当于启动命令时的当前目录
c.NotebookApp.notebook_dir = '/mnt/ds/ds_Independent_product/jurpyter_server'

## 相当于命令参数:--ip=0.0.0.0 
c.NotebookApp.ip = '0.0.0.0'

## 相当于命令参数:--port=8890
c.NotebookApp.port = 8890

# 下面两种登录方式可以共存:
## 设置登录token(默认每次启动时token都会变,设置后就不会变了)
c.NotebookApp.token = 'd77e703b-c26f-4dbc-9e1c-5187a36619bb:bfb66fbfe0864f7b869ed3a50467c03c'
## 设置登录密码
c.NotebookApp.password = u'sha1:08017771105d:5cd7cd486867427fee56a50b3217338986e42813'

## 设置 jupyter public 资源可以被前端随意引用
c.NotebookApp.allow_origin = '*'
# 支持 <iframe> 引用
c.NotebookApp.tornado_settings = {
    'headers': {
        'Content-Security-Policy': "frame-ancestors * 'self' "
    }
}

坑1:c.NotebookApp.ip需要指定为0.0.0.0,否则外网访问不了(报错 Socket Error 99)

参考资料:https://github.com/ipython/ipython/issues/6193

坑2:c.NotebookApp.password的值需要加上前缀u,是指定字符串是 UTF-8 的意思,但是字符串也不是中文呀!具体原因未知。

参考资料:https://cloud.tencent.com/developer/ask/59082

(3)启动

jupyter notebook

2、jupyter_kernel_gateway

jupyter kernelgateway --KernelGatewayApp.api='kernel_gateway.notebook_http' --KernelGatewayApp.seed_uri='/mnt/ds/ds_Independent_product/jurpyter_server/main.ipynb' --port=8888

--KernelGatewayApp.seed_uri指定启动server的文件

默认端口为8888

坑:启动时报错,原因是没有装 jupyter 的一个插件ipywidgets,安装方式如下:

pip3 install ipywidgets
jupyter nbextension enable --py >widgetsnbextension

五、注意点


1、python 括号很重要

# 错
 'R2_threshold' in b  == True

# 对
( 'R2_threshold' in b ) == True

原文地址:https://www.cnblogs.com/xjnotxj/p/11876610.html