1- Jupyter簡介
- 可用於創建和共享代碼與文檔網絡應用;
- 可在其中編寫並運行代碼、查看輸出、可視化數據並查看結果;
- 不僅支持 Python, 還包括用於Julia、 R和其他編程語言;
- Jupyter是從IPython中遷移出來的獨立應用;
- 從IPython4.0(包含)之后分離成IPython和Jupyter,IPython只具有終端接口(安裝的依賴包少很多);
- 分離之后的Jupyter可以被更加廣泛的使用,支持多種語言,並不斷加入新特性;
2- 安裝Jupyter
方式一:安裝Anaconda(包含Jupyter notebook)--- 推薦
方式二:通過pip安裝(同時也會安裝包括Ipython等大量依賴庫)
$ pip install jupyter -i https://mirrors.ustc.edu.cn/pypi/web/simple/
3- 幫助信息
jupyter --help jupyter console --help jupyter notebook --help
- 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
- 交互上使用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,將出現交互式界面:

- In[數字]:表示已經執行的代碼段,其中數字代表順序編號
- In[*]:表示正在執行的代碼段,括號里顯示星號*
- In[]:表示未執行的代碼段,括號里為空
點擊Kernel中的“Restart & Run all”來執行全部代碼。從上向下檢查每一個框,找到錯誤代碼並修改。
特別注意:如果下面已經運行出了執行結果,但是上面又修改了,並且出錯了,那么重新執行的時候下面的執行結果還會保留,因為沒有執行到這里來,程序在上面就已經出錯退出了。
Cell的類型
- Code cells:用於寫代碼;
- Markdown cells:寫markdown文檔,用來寫說明,在Markdown單元中可以直接使用Html和Javascript;
- Raw NBConvert:代碼或文檔的輸出信息,可將筆記本轉換成另一種格式(比如 HTML)的命令行工具;
- Heading:添加標題,遵循Markdown格式;

- 在函數、模塊、類后面輸入‘?’,按住Ctrl-Entry顯示幫助文檔,如果是兩個‘??’則顯示源代碼;
- 以%開始的魔術命令只對單行有效;
- 以%%開頭的魔術命令放在單元的第一行,對整個單元有效;
- %prun用於代碼的執行性能分析,可以作為行命令和單元命令使用;
如果想關閉已經打開的終端和“ipynb”格式的筆記本,僅僅關閉其頁面是無法徹底退出程序的,必須點擊Running標簽中對應的關閉按鈕。

6-設置Jupyter Notebook遠程訪問
官網參考信息(Running a notebook server) :https://jupyter-notebook.readthedocs.io/en/stable/public_server.html
6.1- 生成配置文件
$ 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- 修改默認配置文件
c.NotebookApp.ip='*' c.NotebookApp.password = u'sha1:e16f8ee37c2d:32b310c2505bd268a408a703ada17b7c0181c5f7' c.NotebookApp.open_browser = False c.NotebookApp.port = 12345
6.4- 啟動Jupyter Notebook
日志信息包括筆記文件的存放目錄、啟動的IP地址和端口、關閉方法等;
$ 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- 遠程訪問
7- 一些設置
7.1- 更改Jupyter notebook的工作空間
默認顯示用戶目錄下的非隱藏文件和目錄,例如“C:\Users\guowli\”;
- 執行“jupyter notebook --generate-config”命令獲取配置文件的位置;
- 修改jupyter_notebook_config.py中的c.NotebookApp.notebook_dir參數,將值更改為實際的工作空間目錄即可;
例如:

注意:如果不想在配置文件中指定默認目錄,可以嘗試在Windows系統CMD窗口中進入到指定目錄,執行“jupyter notebook”即可。
7.2- 實現同時支持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內核

7.3- 修改默認瀏覽器
在配置文件jupyter_notebook_config.py的c.NotebookApp.browser參數下,添加如下內容並保存。
import webbrowser
webbrowser.register("chrome", None, webbrowser.GenericBrowser(u"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"))
c.NotebookApp.browser = 'chrome'
注意:與chrome.exe實際所在路徑一致,並且使用“\\”符號來分割路徑地址。

7.4- 管理插件
使用Jupyter NbExtensions Configurator可以簡易地在Jupyter notebook中添加、設置、啟用、禁用插件。
安裝NbExtensions之后重啟Jupyter Notebook,導航欄將出現“Nbextensions”的類目,點擊“Nbextensions”,將看到所有的插件列表。
# 通過conda安裝Nbextensions: conda install -c conda-forge jupyter_nbextensions_configurator # 通過pip安裝Nbextensions: pip install jupyter_nbextensions_configurator jupyter nbextensions_configurator enable --user
- Codefolding:折疊代碼
- Code prettify:它能重新調整代碼塊內容的格式並進行美化
- Collapsible Headings:使得各級標題可縮進
- Notify:For long running task, the notify extension sends a notification when the notebook becomes idle.
- Table of Contents (2):生成Markdown內容的目錄結構

7.5- 安裝nb_conda
nb_conda可以關聯Jupyter Notebook與conda的環境和包
8- 通過nbviewer查看“.ipynb”文件
nbviewer:https://nbviewer.jupyter.org/
A simple way to share Jupyter Notebooks。
通過nbviewer可以方便地查看網絡上的“.ipynb”格式文件。
操作十分簡潔,只需要根據提示輸入“URL”、“GitHub username”、“GitHub username/repo”或“Gist ID”,然后回車即可。

示例:
- 如果GitHub上的.ipynb文件鏈接為:https://github.com/anliven/Hello-Data/blob/master/test.ipynb
- 使用nbviewer打開此文件的地址為:https://nbviewer.jupyter.org/github/anliven/Hello-Data/tree/master/test.ipynb
9- 參考信息
- 必須會的Jupyter Notebook:http://python.jobbole.com/87527/
- 始於Jupyter Notebooks:一份全面的初學者實用指南 https://mp.weixin.qq.com/s/G5_vQQp2qIQZ7Qp_AF-f9g
- Jupyter Notebook介紹、安裝及使用教程:https://zhuanlan.zhihu.com/p/33105153
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?
