基本介紹
這是一個筆記本軟件/文本編輯器,其次它有一系列的功能,使得它非常適合寫作科學類的文章。
約等於 Python 交互式命令行 + markdown,結合 numpy、plotly.py 等科學計算相關的庫,實現類似 matlab 的效果。
安裝
方法一:通過 anaconda 安裝
這是一個開源的 Python 發行版本,其包含了conda、Python等180多個科學包及其依賴項,當然也包含了 Jupyter notebook。
在其 官網 上提供了 Mac、Linux、Windows 在內的多個系統的安裝包。
方法二:通過 pip 安裝
如果 Jupyter 部署服務器上,所以安裝 anaconda 並不見得方便。那麼可以使用 pip 來安裝
pip install jupyter
安裝驗證
命令行輸入
jupyter notebook
你可能會看到類似這樣的提示
[I 09:38:52.280 NotebookApp] The Jupyter Notebook is running at:
[I 09:38:52.280 NotebookApp] http://localhost:8888/
[I 09:38:52.280 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
根據這裏的提示,訪問對應的網址即可對 Jupyter 進行簡單的使用。
Jupyter 的配置
完成了安裝後,你可能需要根據自己的需求進行一些配置,首先你需要生成配置文件
jupyter notebook --generate-config
# 這條指令會在 ~/.jupyter 下生成一個配置文件 jupyter_notebook_config.py
幾個重要的配置項包括:
# TCP/IP 相關配置
## 授權
c.NotebookApp.allow_origin = '*' # 設置訪問的允許來源,* 表示接受所有來源
c.NotebookApp.allow_remote_access = False # 是否支持遠程訪問
## HTTPS,但是建議通過 Nginx 或 Apache 進行代理,而不通過 Jupyter 直接處理 HTTPS 請求
c.NotebookApp.certfile = '' # 證書文件路徑
c.NotebookApp.client_ca = '' # 公鑰證書路徑
c.NotebookApp.keyfile = '' # 私鑰文件路徑
## 地址設置
c.NotebookApp.ip = 'localhost' # 監聽的 IP 地址
c.NotebookApp.port = 8888 # 監聽的地址
c.NotebookApp.custom_display_url = '' # 顯示的域名
# 本地文件配置
c.ContentsManager.untitled_notebook = '未命名' # 新建筆記本的默認名稱
c.NotebookApp.notebook_dir = '/Users/antony/Notes' # 設置筆記本文件保存的路徑
# 密碼配置
c.NotebookApp.password = 'sha1:c05fe7fe7152:12cf0fe1c689112cc3451d1b13700af3fafadc1f'
# 設置訪問密碼,格式爲 type:salt:hashed-password,可以通過 from notebook.auth import passwd; passwd() 來生成
c.NotebookApp.token = '<generated>' # 用於首次登陸 Jupyter 服務時進行校驗的密碼,默認是一個自動生成的字符串,在 Jupyter 運行時打印的信息中可以看到
配置 Nginx 代理
我的做法是 Jupyter Notebook 在本地運行,可以通過 localhost 來進行訪問,再通過 Nginx 將 https 請求進行代理轉發。
在配置 Nginx 之前,請在 Jupyer Notebook 的配置文件中,將 allow_origin 做相應調整,最簡單的就是改爲 *
。
upstream notebook {
server localhost:8888;
}
server {
# 監聽 7123 端口,支持 SSL,當然也可以不支持 SSL
listen 7123 ssl;
# 監聽 7124 端口,使用 http 協議
listen 7124;
# 如果要支持 SSL,需要配置相關的證書、密鑰文件地址
ssl_certificate /etc/nginx/cert/certificate.crt;
ssl_certificate_key /etc/nginx/cert/private.key;
# 設置 Header
add_header Strict-Transport-Security max-age=15768000;
# 設置基礎地址訪問
location / {
proxy_pass http://notebook;
proxy_set_header Host $host;
}
# 設置相關接口請求,需要增加對 websocket 的支持,增加相關 header
location ~ /api/kernels/ {
proxy_pass http://notebook;
proxy_set_header Host $host;
# websocket 支持
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
# 設置終端接口請求,需要增加對 websocket 的支持
location ~ /terminals/ {
proxy_pass http://notebook;
proxy_set_header Host $host;
# websocket 支持
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
}
設置 Jupyter Notebook 的開機自啓動
這裏以 Ubuntu 系統爲例,新增 /etc/systemd/system/jupyter.service
文件
[Unit]
Description=Jupyter Notebook
[Service]
Type=simple
ExecStart=/path/to/jupyter notebook --no-browser
# 需要將 /path/to 替換成 jupyter 的安裝路徑
# 建議在 Jupyter 的配置文件中,將 NotebookApp.token 修改爲一個固定值
User=你的用戶名
Group=用戶羣組
WorkingDirectory=你的 home 目錄
[Install]
WantedBy=multi-user.target
啓用這個服務
sudo systemctl start jupyter
sudo systemctl enable jupyter