Python - 搭建Jupyter notebook環境
阿新 • • 發佈:2018-11-23
1- Jupyter簡介
- HomePage:https://jupyter.org/
- 安裝指南:https://jupyter.org/install.html
- 官方文件:https://jupyter.org/documentation.html https://jupyter.readthedocs.io/
- 可用於建立和共享程式碼與文件網路應用;
- 可在其中編寫並執行程式碼、檢視輸出、視覺化資料並檢視結果;
- 不僅支援 Python, 還包括用於Julia、 R和其他程式語言;
- Jupyter是從IPython中遷移出來的獨立應用;
- 從IPython4.0(包含)之後分離成IPython和Jupyter,IPython只具有終端介面(安裝的依賴包少很多);
- 分離之後的Jupyter可以被更加廣泛的使用,支援多種語言,並不斷加入新特性;
2- 安裝Jupyter
$ pip install jupyter -i https://mirrors.ustc.edu.cn/pypi/web/simple/
安裝Jupyter的同時也安裝包括Ipython等大量依賴庫;
3- 幫助資訊
jupyter --help jupyter console --help jupyter notebook --helpJupyter中包括console和notebook: console類似ipython; notebook是基於瀏覽器Web技術的開發環境,可以進行寫作文件和執行程式碼,包括web application和notebook documents;
- web application等同於一個編輯器,除了支援普通程式碼,還包括富媒體資訊如HTML,LaTex,Markdown,MathJax和各種影象格式;
- notebook documents以JSON格式儲存為.ipynb檔案,可以通過bfconvert轉成HTML、PDF等格式;
4- 啟動Jupyter console
$ jupyter console Jupyter console 5.2.0 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help. In [1]: In [1]: exit Shutting down kernel $
5- 啟動Jupyter notebook
Jupyter notebook是web based IPython封裝,可以展現富文字,使得整個工作能夠以筆記的形式展現和儲存;- 互動上使用C/S結構,使用瀏覽器作為客戶端訪問頁面,向後臺的Jupyter Notebook伺服器傳送請求,並顯示結果;
- 頁面都被儲存為 .ipynb 的類 JSON 檔案格式;
- 預設啟動的地址為“http://localhost:8888/tree”,可以啟動多個,埠號會自動累加;
$ jupyter notebook # 以預設引數啟動 $ jupyter notebook test.ipynb # 啟動時開啟test.ipynb $ jupyter notebook --port 12345 # 啟動時指定埠 $ jupyter notebook --no-brower # 啟動時不開啟瀏覽器 $ jupyter notebook --ip="*" # 允許任意IP連線
以預設引數啟動:
$ jupyter notebook [I 23:16:00.714 NotebookApp] Writing notebook server cookie secret to C:\Users\anliven\AppData\Roaming\jupyter\runtime\notebook_cookie_secret [I 23:16:01.245 NotebookApp] Serving notebooks from local directory: D:\Anliven [I 23:16:01.246 NotebookApp] 0 active kernels [I 23:16:01.247 NotebookApp] The Jupyter Notebook is running at: [I 23:16:01.247 NotebookApp] http://localhost:8888/?token=34be99926b1d3972f14ccc93bff0be9fa1815cc81674199f [I 23:16:01.247 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 23:16:01.249 NotebookApp] Copy/paste this URL into your browser when you connect for the first time, to login with a token: http://localhost:8888/?token=34be99926b1d3972f14ccc93bff0be9fa1815cc81674199f [I 23:16:01.570 NotebookApp] Accepting one-time-token-authenticated connection from ::1 [W 23:16:01.915 NotebookApp] 404 GET /static/components/moment/locale/zh-cn.js?v=20171207231600 (::1) 7.04ms referer=http://localhost:8888/tree [W 23:16:01.920 NotebookApp] 404 GET /i18n/zh-CN/LC_MESSAGES/nbjs.json?v=20171207231600 (::1) 2.00ms referer=http://localhost:8888/tree
預設瀏覽器將開啟Jupyter的web介面:
在Files標籤中-->New-->選擇對應的NoteBook,例如Python3,將出現互動式介面:
每個Code輸入區域都有一個對應的輸出區域,輸入程式碼後按 Shift-Enter 將執行此程式碼,顯示程式碼中最後一個表示式的值; 如果希望遮蔽輸出,可以在最後一條語句後新增一個分號”;”; 使用單元(Cell)儲存資訊,種類包括表示格式化文字的Markdown單元和表示程式碼的Code單元等;- Code cells:用於寫程式碼;
- Markdown cells:寫markdown文件,用來寫說明,在Markdown單元中可以直接使用Html和Javascript;
- Raw Cells: 程式碼或文件的輸出資訊;
- 在函式、模組、類後面輸入‘?’,按住Ctrl-Entry顯示幫助文件,如果是兩個‘??’則顯示原始碼;
- 以%開始的魔術命令只對單行有效;
- 以%%開頭的魔術命令放在單元的第一行,對整個單元有效;
- %prun用於程式碼的執行效能分析,可以作為行命令和單元命令使用;
6-設定Jupyter Notebook遠端訪問
6.1-生成配置檔案
將在~/.jupyter目錄下生成一個jupyter_notebook_config.py檔案$ jupyter notebook --generate-config Writing default config to: C:\Users\anliven\.jupyter\jupyter_notebook_config.py
6.2-生成一個密文密碼
$ jupyter console Jupyter console 5.2.0 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help. In [1]: from IPython.lib import passwd In [2]: passwd() Enter password: Verify password: Out[2]: 'sha1:e16f8ee37c2d:32b310c2505bd268a408a703ada17b7c0181c5f7' In [3]: exit Shutting down kernel
6.3-修改預設配置檔案
jupyter_notebook_config.py的修改內容如下:c.NotebookApp.ip='*' c.NotebookApp.password = u'sha1:e16f8ee37c2d:32b310c2505bd268a408a703ada17b7c0181c5f7' c.NotebookApp.open_browser = False c.NotebookApp.port = 12345
6.4-啟動Jupyter Notebook
$ jupyter notebook [W 23:34:18.473 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended. [I 23:34:18.643 NotebookApp] Serving notebooks from local directory: D:\Anliven [I 23:34:18.644 NotebookApp] 0 active kernels [I 23:34:18.644 NotebookApp] The Jupyter Notebook is running at: [I 23:34:18.644 NotebookApp] http://[all ip addresses on your system]:12345/ [I 23:34:18.645 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation) .
6.5-遠端訪問
瀏覽器直接訪問 http://<jupyter-server>:<port>可以看到jupyter的登陸介面。6.6-官網參考資訊
Running a notebook server : https://jupyter-notebook.readthedocs.io/en/stable/public_server.html7- 更改jupyter notebook的工作空間
- 執行“jupyter notebook --generate-config”命令獲取配置檔案的位置;
- 修改jupyter_notebook_config.py中的c.NotebookApp.notebook_dir引數,將值更改為實際的工作空間目錄即可;
8- 實現同時支援Python2和Python3
# 假定已安裝Python3(通過Anaconda3安裝) # $ conda env list # # conda environments: # # # base * D:\DownLoadFiles\anaconda3 conda create -n testpy2 python=2 # 建立一個Python2的虛擬環境 source activate testpy2 # 啟用環境 conda install ipykernel # 安裝ipykernel python -m ipykernel install --user source activate # 啟用base環境 jupyter notebook # 啟動notebook,此時可以看到Python2和Python3核心
9- 參考資訊
- 必須會的Jupyter Notebook:http://python.jobbole.com/87527/
- 始於Jupyter Notebooks:一份全面的初學者實用指南 https://mp.weixin.qq.com/s/G5_vQQp2qIQZ7Qp_AF-f9g
10- 問題處理
Jupyter notebook的啟動日誌顯示大量404
[W 23:35:49.743 NotebookApp] 404 GET /static/components/moment/locale/zh-cn.js?v=20171207233418 (127.0.0.1) 6.02ms referer=http://127.0.0.1:12345/tree? [W 23:35:49.756 NotebookApp] 404 GET /i18n/zh-CN/LC_MESSAGES/nbjs.json?v=20171207233418 (127.0.0.1) 1.98ms referer=http://127.0.0.1:12345/tree? [I 23:50:19.421 NotebookApp] 302 POST /login?next=%2F (10.172.192.178) 1.00ms [I 23:50:19.429 NotebookApp] 302 GET / (10.172.192.178) 0.00ms [W 23:50:19.724 NotebookApp] 404 GET /static/components/moment/locale/zh-cn.js?v=20171207233418 (10.172.192.178) 2.01ms referer=http://10.172.192.178:12345/tree? [W 23:50:19.772 NotebookApp] 404 GET /i18n/zh-CN/LC_MESSAGES/nbjs.json?v=20171207233418 (10.172.192.178) 1.98ms referer=http://10.172.192.178:12345/tree?問題分析: 302錯誤疑似notebook不應該放在d盤目錄。 404錯誤疑似語言支援格式問題導致,對中文語言不適配。 問題處理: 對於學習python知識而言,無實質影響,暫不關注。