ubuntu16.04安裝jupyternotebook使用與進階

一、Jupyter Notebook簡介

Jupyter Notebook(此前被稱為 IPython notebook)是一個交互式筆記本,支持運行 40 多種編程語言。
Jupyter Notebook 的本質(zhì)是一個 Web 應(yīng)用程序,便于創(chuàng)建和共享文學(xué)化程序文檔,支持實時代碼,數(shù)學(xué)方程,可視化和 markdown。 用途包括:數(shù)據(jù)清理和轉(zhuǎn)換,數(shù)值模擬,統(tǒng)計建模,機器學(xué)習(xí)等等;
是一款非常好用的基于web方式使用python的工具;
支持windows 與linux系統(tǒng),本次以ubuntu 16.04 python3.5.2為例,安裝演示;
相關(guān)配置及更高級的玩法請參考官方文檔

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比彌渡網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式彌渡網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋彌渡地區(qū)。費用合理售后完善,十年實體公司更值得信賴。

以上的操作是針對本機的python3操作,如果想在虛擬的python環(huán)境中操作請參考搭建python虛擬環(huán)境
其他的步驟是 一樣的;

二、安裝

1、pip安裝

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

2、簡單使用
默認安裝好后直接在命令行里輸入
$ jupyter notebook
類似如下圖:
ubuntu 16.04安裝jupyter notebook使用與進階
如上圖,新建-->Python3 創(chuàng)建如下圖交互式帶提示的python命令行
ubuntu 16.04安裝jupyter notebook使用與進階
輸入print("Hellow world") 點運行即可執(zhí)行看到效果;

新建終端時,直接以登錄系統(tǒng)身份登錄系統(tǒng)終端如圖:
ubuntu 16.04安裝jupyter notebook使用與進階

至次jupyter notebook的簡單安裝使用就完成了,沒錯就是這么簡單~
然而這么好的工具,這么方便的操作只能運行在本地?如果在局域網(wǎng)或端口映射后能在外面操作,通過密碼登錄;豈不是很完美?查看了官方網(wǎng)站教程,別說還真有;下面就配置下把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"
  }
}

些時結(jié)束之前的jupyter notebook實例再次運行,會提示輸入密碼,如圖:
ubuntu 16.04安裝jupyter notebook使用與進階
此時只有輸入正確的密碼后才能正常登錄使用jupyter 啦!

2、偵聽非本地接口
默認出于安全jupyter只運行偵聽在本地,想把這么好的功能放在網(wǎng)絡(luò)上使用需要生成添加配置文件

$ jupyter notebook --generate-config

運行后會在家目錄.jupyter/下生成jupyter_notebook_config.py文件
修改內(nèi)容如下:

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
創(chuàng)建私鑰并自簽:

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

運行以上命令如下需要填寫相關(guān)信息如圖:
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停止此服務(wù)器并關(guān)閉所有內(nèi)核(兩次跳過確認).

注意:證書文件需要絕對路徑;
此時可以使用https://10.8.11.65:8888 或https://localhost:8888進行訪問啦 如圖:
ubuntu 16.04安裝jupyter notebook使用與進階

到這里jupyter notebook基本配置完成了,但有一個問題,每次啟動jupyter都要切換到一個指定目錄(就是上圖中文件列出來的內(nèi)容所在目錄);而且還要占用一個終端;
還是搞一個后臺服務(wù),開機自動運行吧,以下的內(nèi)容是官方?jīng)]有的,本人自己搗鼓的;

4、jupyter服務(wù)

#!/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庫 你的系統(tǒng)自行找,如找不到請聯(lián)系我
prog=jupyter
pidfile=${PIDFILE-/tmp/jupyter.pid}
lockfile=${LOCKFILE-/tmp/jupyter}
jupyter="/usr/local/bin/jupyter"     # 如果是虛擬python環(huán)境請?zhí)顚懻_的路徑
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

添加可執(zhí)行權(quán)限

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

測試腳本 如圖:
ubuntu 16.04安裝jupyter notebook使用與進階
可以發(fā)現(xiàn)腳本可以執(zhí)行;會自動讀取當(dāng)前登錄系統(tǒng)用戶的家目錄下.jupyter下的配置文件;官方說把密鑰和簽名證書配置到配置文件中,但我測試下來找不到證書,所以把證書當(dāng)啟動參數(shù)放到,服務(wù)腳本中了,這是正常的;
系統(tǒng)重啟會自動啟動jupyter notebook 偵聽在8888上,系統(tǒng)關(guān)閉時,會自動關(guān)閉jupyter服務(wù); upyter notebook可以運行多實例;即默認隨機啟動偵聽在8888 再次在終端上執(zhí)行時會偵聽在8889端口 如圖:
ubuntu 16.04安裝jupyter notebook使用與進階

此時你會發(fā)現(xiàn) 一臺服務(wù)器可以開多個實例,給多個人測試使用~是不是很方便很好用?趕緊試試吧~

本文標題:ubuntu16.04安裝jupyternotebook使用與進階
標題網(wǎng)址:http://bm7419.com/article4/goshoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、微信小程序、軟件開發(fā)域名注冊、品牌網(wǎng)站制作云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)