温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ubuntu 16.04安装jupyter notebook使用与进阶

发布时间:2020-06-10 18:21:49 来源:网络 阅读:5042 作者:dyc2005 栏目:编程语言

一、Jupyter Notebook简介

Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言。
Jupyter Notebook 的本质是一个 Web 应用程序,便于创建和共享文学化程序文档,支持实时代码,数学方程,可视化和 markdown。 用途包括:数据清理和转换,数值模拟,统计建模,机器学习等等;
是一款非常好用的基于web方式使用python的工具;
支持windows 与linux系统,本次以ubuntu 16.04 python3.5.2为例,安装演示;
相关配置及更高级的玩法请参考官方文档

以上的操作是针对本机的python3操作,如果想在虚拟的python环境中操作请参考搭建python虚拟环境
其他的步骤是 一样的;

二、安装

1、pip安装

sudo python3 -m pip install --upgrade pip
sudo python3 -m pip install jupyter

2、简单使用
默认安装好后直接在命令行里输入
$ jupyter notebook
类似如下图:
ubuntu 16.04安装jupyter notebook使用与进阶
如上图,新建-->Python3 创建如下图交互式带提示的python命令行
ubuntu 16.04安装jupyter notebook使用与进阶
输入print("Hellow world") 点运行即可执行看到效果;

新建终端时,直接以登录系统身份登录系统终端如图:
ubuntu 16.04安装jupyter notebook使用与进阶

至次jupyter notebook的简单安装使用就完成了,没错就是这么简单~
然而这么好的工具,这么方便的操作只能运行在本地?如果在局域网或端口映射后能在外面操作,通过密码登录;岂不是很完美?查看了官方网站教程,别说还真有;下面就配置下把jupyter notebook开放在本地的任何接口上,并且配置https形式,这样传输就安全啦~

三、jupyter notebook使用进阶

1、配置密码

$ jupyter notebook password
Enter password:  ****
Verify password: ****

以上操作会在我的家目录下生成 /home/san/.jupyter/jupyter_notebook_config.json
文件,密码以hash保存如下:

$  /home/san/.jupyter/jupyter_notebook_config.json
{
  "NotebookApp": {
    "password": "sha1:74432c61ae9b:26c35e70239jfe7e3dc4b177d140d4ac7f560e1f"
  }
}

些时结束之前的jupyter notebook实例再次运行,会提示输入密码,如图:
ubuntu 16.04安装jupyter notebook使用与进阶
此时只有输入正确的密码后才能正常登录使用jupyter 啦!

2、侦听非本地接口
默认出于安全jupyter只运行侦听在本地,想把这么好的功能放在网络上使用需要生成添加配置文件

$ jupyter notebook --generate-config

运行后会在家目录.jupyter/下生成jupyter_notebook_config.py文件
修改内容如下:

c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.open_browser = False 
c.NotebookApp.password = "sha1:74432c61ae9b:26c35e70239jfe7e3dc4b177d140d4ac7f560e1f"
c.NotebookApp.port = 8888

注意:按官方的c.NotebookApp.ip = '*' 报错,换成0.0.0.0则可以
可以看到不仅可以自定义侦听地址,还可以修改侦听端口;

$ netstat -ntpul |grep python3
tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      2403/python3

此时就可以通过本地的ip或loclahost :8888访问啦

3、jupyter配置https
创建私钥并自签:

$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mykey.key -out mycert.pem

运行以上命令如下需要填写相关信息如图:
ubuntu 16.04安装jupyter notebook使用与进阶

以https方式启动:

$ jupyter notebook --certfile=mycert.pem --keyfile mykey.key
[I 13:30:22.165 NotebookApp] 启动notebooks 在本地路径: /home/dongyc/ipython
[I 13:30:22.165 NotebookApp] 本程序运行在: https://(san-Vostro-3660 or 127.0.0.1):8888/
[I 13:30:22.165 NotebookApp] 使用control-c停止此服务器并关闭所有内核(两次跳过确认).

注意:证书文件需要绝对路径;
此时可以使用https://10.8.11.65:8888 或https://localhost:8888进行访问啦 如图:
ubuntu 16.04安装jupyter notebook使用与进阶

到这里jupyter notebook基本配置完成了,但有一个问题,每次启动jupyter都要切换到一个指定目录(就是上图中文件列出来的内容所在目录);而且还要占用一个终端;
还是搞一个后台服务,开机自动运行吧,以下的内容是官方没有的,本人自己捣鼓的;

4、jupyter服务

#!/bin/bash
# author: by san at 20180929
### BEGIN INIT INFO
# Provides:             jupyter 
# Required-Start:       $syslog $remote_fs
# Required-Stop:        $syslog $remote_fs
# Should-Start:         $local_fs
# Should-Stop:          $local_fs
# Default-Start:        2 3 4 5 
# Default-Stop:         0 1 6
# Short-Description:    jupyter notebook deamon 
# Description:          jupyter notebook with https deamon
### END INIT INFO

. /home/san/functions      # 这个类似redhat系列上的/etc/init.d/functions shell库 你的系统自行找,如找不到请联系我
prog=jupyter
pidfile=${PIDFILE-/tmp/jupyter.pid}
lockfile=${LOCKFILE-/tmp/jupyter}
jupyter="/usr/local/bin/jupyter"     # 如果是虚拟python环境请填写正确的路径
RETVAL=0

START(){
if [ ! -f ${pidfile} ]; then
cd /home/san/ipython 
echo -n $"Stopping jupyter notebook:"
nohup ${jupyter}  notebook  --certfile=/home/san/mycert.pem --keyfile /home/san/mykey.key >/tmp/jupyter.log 2>&1 &
 [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
 jupyter_pid=$(ps aux |grep jupyter |grep -v grep|awk '{print $2}')
 echo $jupyter_pid >$pidfile
  RETVAL=$?
    echo
 [ $RETVAL = 0 ] && touch ${lockfile}
     return $RETVAL
else
  status -p ${pidfile}
  exit 0
 fi

}

STOP(){
        echo -n $"Stopping jupyter notebook:"
        nohup kill -15 $(ps aux |grep jupyter |grep -v grep|awk '{print $2}')  >/dev/null 2>&1 &
        [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
        echo
        [ -f ${pidfile} ] && rm -rf ${pidfile}
        [ -f ${lockfile} ] && rm -rf ${lockfile}
}

case $1 in
    start)
    START
    ;;
    stop)
    STOP
    ;;
    restart)
    STOP
    START
    ;;
    status)
    status -p ${pidfile} $prog
    ${jupyter} notebook list
    RETVAL=$?
    ;;
    *)
   echo "USAGE:start|stop|restart"
    ;;
esac

添加可执行权限

$ chmod +x /etc/init.d/jupyter

测试脚本 如图:
ubuntu 16.04安装jupyter notebook使用与进阶
可以发现脚本可以执行;会自动读取当前登录系统用户的家目录下.jupyter下的配置文件;官方说把密钥和签名证书配置到配置文件中,但我测试下来找不到证书,所以把证书当启动参数放到,服务脚本中了,这是正常的;
系统重启会自动启动jupyter notebook 侦听在8888上,系统关闭时,会自动关闭jupyter服务; upyter notebook可以运行多实例;即默认随机启动侦听在8888 再次在终端上执行时会侦听在8889端口 如图:
ubuntu 16.04安装jupyter notebook使用与进阶

此时你会发现 一台服务器可以开多个实例,给多个人测试使用~是不是很方便很好用?赶紧试试吧~

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI