jupyter是一種交互式計算和開發環境的筆記,ipython命令行比原生的python命令行更加友好和高效,還可以運行web版的界面,支持多語言,輸出圖形、音頻、視頻等功能。
安裝
pip install jupyter
使用
- 命令行方式
ipython
2. 運行shell命令
感嘆號!+shell直接運行shell命令,如!pwd
運行python文件
%run test.py(python文件)
魔術函數的使用: %time a = np.arange(1000)
%quickref 顯示IPython的快速參考
%magic 顯示所有魔術命令的詳細文檔
%debug 從最新的異常跟蹤的底部進入交互式調試器
%hist 打印命令的輸入(可選輸出)歷史
%pdb 在異常發生后自動進入調試器
%
paste
執行剪貼板中的Python代碼
%cpaste 打開一個特殊提示符以便手工粘貼待執行的Python代碼
%reset 刪除interactive命名空間中的全部變量/名稱
%page OBJECT 通過分頁器打印輸出OBJECT
%run script.py 在IPython中執行一個Python腳本文件
%prun statement 通過cProfile執行statement,並打印分析器的輸出結果
%
time
statement 報告statement的執行時間
%timeit statement 多次執行statement以計算系綜平均執行時間。對那些執行時 間非常小的代碼很有用
%
who
、%who_ls、%whos 顯示interactive命名空間中定義的變量,信息級別/冗余度可變
%xdel variable 刪除variable,並嘗試清除其在IPython中的對象上的一切引用
3. 運行web ipython環境
jupyter notebooke --ip=127.0.0.1 --port=8888
更多參數:
這將啟動一個基於tornado的HTML筆記本服務器,它提供一個html5/javascript筆記本客戶端。
Subcommands
-----------
Subcommands are launched as `jupyter-notebook cmd [args]`. For information on
using subcommand 'cmd', do: `jupyter-notebook cmd -h`.
list
列出當前運行的Notebook服務.
stop
Stop currently running notebook server for a given port
password
Set a password for the notebook server.
Options
-------
Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.
--debug
set log level to logging.DEBUG (maximize logging output)
--generate-config
generate default config file
-y
Answer yes to any questions instead of prompting.
--no-browser
在啟動服務以后不在瀏覽器中打開一個窗口.
--pylab
DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
--no-mathjax
Disable MathJax
MathJax is the javascript library Jupyter uses to render math/LaTeX. It is
very large, so you may want to disable it if you have a slow internet
connection, or for offline use of the notebook.
When disabled, equations etc. will appear as their untransformed TeX source.
--allow-root
允許notebook在root用戶下運行.
--script
DEPRECATED, IGNORED
--no-script
DEPRECATED, IGNORED
--log-level=<Enum> (Application.log_level)
Default: 30
Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')
Set the log level by value or name.
--config=<Unicode> (JupyterApp.config_file)
Default: ''
Full path of a config file.
--ip=<Unicode> (NotebookApp.ip)
Default: 'localhost'
notebook服務會監聽的IP地址.
--port=<Int> (NotebookApp.port)
Default: 8888
notebook服務會監聽的IP端口.
--port-retries=<Int> (NotebookApp.port_retries)
Default: 50
如果指定的端口不可用,則要嘗試其他端口的數量.
--transport=<CaselessStrEnum> (KernelManager.transport)
Default: 'tcp'
Choices: ['tcp', 'ipc']
--keyfile=<Unicode> (NotebookApp.keyfile)
Default: ''
SSL/TLS 私鑰文件所在全路徑.
--certfile=<Unicode> (NotebookApp.certfile)
Default: ''
SSL/TLS 認證文件所在全路徑.
--client-ca=<Unicode> (NotebookApp.client_ca)
Default: ''
用於ssl/tls客戶端身份驗證的證書頒發證書的完整路徑.
--notebook-dir=<Unicode> (NotebookApp.notebook_dir)
Default: ''
用於筆記本和內核的目錄。
--browser=<Unicode> (NotebookApp.browser)
Default: ''
Specify what command to use to invoke a web browser when opening the
notebook. If not specified, the default browser will be determined by the
`webbrowser` standard library module, which allows setting of the BROWSER
environment variable to override it.
--pylab=<Unicode> (NotebookApp.pylab)
Default: 'disabled'
DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
--gateway-url=<Unicode> (GatewayClient.url)
Default: None
The url of the Kernel or Enterprise Gateway server where kernel
specifications are defined and kernel management takes place. If defined,
this Notebook server acts as a proxy for all kernel management and kernel
specification retrieval. (JUPYTER_GATEWAY_URL env var)
To see all available configurables, use `--help-all`
Examples
--------
jupyter notebook # start the notebook
jupyter notebook --certfile=mycert.pem # use SSL/TLS certificate
jupyter notebook password # enter a password to protect the server
常用命令:
1. 查看配置文件
jupyter notebook --generate-config
如果配置文件不存在, 創建
ipython profile create
常用快捷鍵:
執行當前cell,並自動跳到下一個cell:Shift Enter
執行當前cell,執行后不自動調轉到下一個cell:Ctrl-Enter
為一行或者多行添加/取消注釋:Crtl /<br>打開幫助,Ctrl+Shirt+P
1. 添加代碼自動補全
pip install jupyter_contrib_nbextensions 安裝完成,需要配置 jupyter contrib nbextension install --user --skip-running-check 啟動jupyter notebook 頁面上會出現 Nbextensions 勾選 Hinterland即可 具體使用的時候, 使用tab進行代碼補全操作
2. 顯示行號
查看 > 切換行號
3. 以守護進程方式運行
nohup jupyter notebook --ip=0.0.0.0 --port=8888 --allow-root > /dev/null 2>&1 &
4. 設置登陸密碼
Jupiter notebook password
常見錯誤:
1. python - Jupyter error: "No module named jupyter_core.paths"
重新安裝jupyter
2. ImportError: No module named '_sqlite3'
Traceback (most recent call last): File "decode_conv.py", line 6, in <module> import sqlite3 File "/usr/local/lib/python3.5/sqlite3/__init__.py", line 23, in <module> from sqlite3.dbapi2 import * File "/usr/local/lib/python3.5/sqlite3/dbapi2.py", line 27, in <module> from _sqlite3 import * ImportError: No module named '_sqlite3'
解決:
# 先安裝 sqlite-devel yum install sqlite-devel # 之后需要將Python基於代碼的安裝包重新編譯一下,並重新安裝之后 ./configure –prefix=python-install-folder make & make install