ubuntu 16.04安装jupyter notebook使用与进阶

1、Jupyter Notebook简介

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

以上的操做是针对本机的python3操做,若是想在虚拟的python环境中操做请参考搭建python虚拟环境
其余的步骤是 同样的;node

2、安装

一、pip安装python

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

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

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

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

3、jupyter notebook使用进阶

一、配置密码编程

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

以上操做会在个人家目录下生成 /home/san/.jupyter/jupyter_notebook_config.json
文件,密码以hash保存以下:json

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

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

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

$ 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访问啦

三、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都要切换到一个指定目录(就是上图中文件列出来的内容所在目录);并且还要占用一个终端;
仍是搞一个后台服务,开机自动运行吧,如下的内容是官方没有的,本人本身捣鼓的;

四、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使用与进阶

此时你会发现 一台服务器能够开多个实例,给多我的测试使用~是否是很方便很好用?赶忙试试吧~

相关文章
相关标签/搜索