Jupyter Notebook是一款開源的交互式Web應用,使用Python語言開發。其官方建議通過Anaconda安裝Python和Jupyter Notebook。本文記錄如何通過Anaconda安裝、配置Jupyter Notebook,並通過Shell腳本實現整個過程。
We are changing versioning in Anaconda Distribution from a
major/minor
version scheme to ayear.month
scheme. We made this change to differentiate between the open source Anaconda Distribution and Anaconda Enterprise, our managed data science platform. Conda, will continue to use amajor/minor
versioning scheme. – Anaconda Distribution 2018.12 Released
簡介
We strongly recommend installing Python and Jupyter using the Anaconda Distribution, which includes Python, the Jupyter Notebook, and other commonly used packages for scientific computing and data science. – Installing Jupyter
Anaconda
Anaconda Distribution is the easiest way to do Python data science and machine learning. It includes 250+ popular data science packages and the conda package and virtual environment manager for Windows, Linux, and MacOS. Conda makes it quick and easy to install, run, and upgrade complex data science and machine learning environments like Scikit-learn, TensorFlow, and SciPy. Anaconda Distribution is the foundation of millions of data science projects as well as Amazon Web Services' Machine Learning AMIs and Anaconda for Microsoft on Azure and Windows. – https://www.anaconda.com/what-is-anaconda/
Jupyter
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more. – https://jupyter.org/
官方文檔
- Conda
- Anaconda https://docs.anaconda.com/anaconda/
- Jupyter
Conda 提供關於conda
相關使用命令的 cheat sheet。
Shell 腳本
整個安裝、配置過程已通過Shell腳本實現,代碼託管在GitLab,通過如下命令執行
|
|
爲方便管理Jupyter Notebook,本人在腳本中設置了一些命令別名,存放在文件~/.bashrc
中,具體如下
|
|
jnl
:查看系統中已運行的server;jnb
:啓動新的server;jne
:關閉所有已啓動的server;jni
:用於安裝包的搜索、安裝和更新,jni a
可對所有已安裝的包進行更新。jnr
:移除安裝包
Anaconda
Anaconda 下載頁面爲 https://www.anaconda.com/download,同時支持Python 3.7 和 2.7,根據需要選擇下載對應版本的Anaconda。
版本信息
Anaconda當前最新釋出版本爲2019.03
。
可通過如下命令提取最新版本信息
|
|
輸出結果
|
|
釋出日期 | 版本 |
---|---|
April 04, 2019 | 2019.03 |
December 21, 2018 | 2018.12 |
November 19, 2018 | 5.3.1 |
May 30, 2018 | 5.2.0 |
February 15, 2018 | 5.1.0 |
校驗
Anaconda 並未直接在下載頁面提供安裝包的hash校驗信息,相關信息存放在文檔頁 Anaconda installer file hashes。其中頁面 Hashes for all files 列出了Anaconda各歷史版本的sha256hash值。
此處以Anaconda3-2019.03-Linux-x86_64.sh
爲例,頁面 Hashes for Anaconda3-2019.03-Linux-x86_64.sh 列出了安裝包的相關信息。
item | details |
---|---|
Last Modified | 2019-04-04 16:00:31 |
size(byte) | 685906562 |
md5 | 43caea3d726779843f130a7fb2d380a2 |
sha256 | 45c851b7497cc14d5ca060064394569f724b67d9b5f98a926ed49b834a6bb73a |
可通過如下命令進行Hash校驗
|
|
演示示例
|
|
安裝
sha256校驗通過後,參照官方文檔 https://docs.anaconda.com/anaconda/install/ 進行安裝。
執行如下命令進行安裝
|
|
Anaconda默認採用的是 交互式 安裝,需要用戶參與,詳細說明見官方文檔 Installing on Linux。
Anaconda同樣支持 無交互式 安裝,根據官方文檔
- https://conda.io/docs/user-guide/install/index.html#installing-in-silent-mode
- https://conda.io/docs/user-guide/install/linux.html#install-linux-silent
- https://conda.io/docs/user-guide/install/macos.html#install-macos-silent
安裝時添加參數-b
即可實現 靜默 安裝,如果需要自定義安裝路徑,添加參數-p
,具體說明如下:
- -b: Batch mode with no PATH modifications to
~/.bashrc
. Assumes that you agree to the license agreement. Does not edit the.bashrc
or.bash_profile
files. - -p: Installation prefix/path.
- -f: Force installation even if prefix -p already exists.
此處以安裝路徑爲/opt/Anaconda
爲例,安裝命令爲
|
|
$PATH
Anaconda安裝完成後,仍無法直接執行命令conda
。原因是可執行文件路徑${installation_dir}/bin/
不在環境變量$PATH
中。需要通過文件${installation_dir}/bin/activate
將其添加到$PATH
中。
Anaconda給出的方案是在文件~/.bashrc
中添加如下設置
|
|
但個人建議將其放置在目錄/etc/profile.d/
中,這樣其他用戶也可以使用conda
。
執行如下命令進行更新
|
|
Jupyter
Anaconda中已集成有Jupyter Notebook,稍作配置即可使用。
Jupyter Notebook默認監聽端口8888
,採用密碼或token方式登入。
通過如下命令生成配置文件
|
|
生成的配置文件路徑爲
|
|
需要設置的主要有以下指令
NotebookApp.allow_root
NotebookApp.base_url
NotebookApp.ip
NotebookApp.port
c.NotebookApp.password
NotebookApp.allow_password_change
NotebookApp.notebook_dir
NotebookApp.certfile
NotebookApp.disable_check_xsrf
如果是遠程訪問,則需要在防火牆中開啓Jupyter端口(默認爲8888
)。
生成密碼
Jupyter Notebook採用的是交互式方式生成Hash密碼。
交互模式
官方文檔 Alternatives to token authentication 提到
New in version 5.0: jupyter notebook password command is added.
生成的Hash密碼存儲路徑爲
|
|
演示過程如下 (密碼[email protected]$(date +'%Y')_Python
)
|
|
靜默模式
但交互式方式不利於Shell腳本的自動化操作, 能否實現非交互式生成密碼呢?
此處以Python 3爲例,Jupyter Notebook生成密碼使用到如下文件,裏面定義了相關函數,如passwd
、passwd_check
、set_password
、persist_config
。
|
|
提取其中部分代碼,寫入文件/tmp/passwd.py
,仍以密碼[email protected]$(date +'%Y')_Python
爲例。
|
|
執行如下命令
|
|
操作過程
|
|
可以看到驗證結果爲True
。
但這樣做會有一個問題:Anaconda同時支持Python 3 和 2,文件security.py
有2份,需要分別處理;如果Jupyter Notebook更新了其中的代碼,則Shell腳本也須作出相應更改,本人無法確保能夠及時作出響應。基於此考慮,未將自定義密碼功能加入Shell腳本。
SSL證書
爲提高數據傳輸安全,可配置SSL證書,官方文檔見 Using SSL for encrypted communication。
通過openssl
創建自簽SSL證書,該過程是一個交互式過程。但可以通過指令-subj
實現免交互操作。
Shell代碼示例
|
|
執行過程如下
|
|
Jupyter 插件
爲增強Jupyter Notebook功能,可選擇安裝插件jupyter_contrib_nbextensions,官方文檔 Unofficial Jupyter Notebook Extensions。
|
|
測試
命令行
|
|
Web 瀏覽器
私有 SSL
參考資料
更新日誌
- 2018.04.19 10:42 Wed America/Boston
- 初稿完成
- 2018.07.11 11:38 Wed America/Boston
- 更新版本至
5.2
- 更新版本至
- 2018.11.29 10:45 Thu America/Boston
- 更新版本至
5.3.1
- 更新版本至
- 2018.12.22 19:27 Sat America/Boston
- Anaconda版本號格式由
major/minor
更改爲year.month
,更新版本至2018.12
,詳情見 Anaconda Distribution 2018.12 Released。
- Anaconda版本號格式由
- 2019.04.09 13:32 Tue America/Boston
- 更新版本至
2019.03
- 更新版本至