jupyter

A Tutorial

package management

pip只能管理python package,而conda可以管理非python语言的package。

So Conda is a packaging tool and installer that aims to do more than what pip does; handle library dependencies outside of the Python packages as well as the Python packages themselves.

曾读到过,对于python package,conda基本就是用的pip。简单讲,如果安装一个包的命令是pip install wanted_package,在conda中conda install wanted_package同样可以安装。

package for jupyter

Since jupyter is belong to anaconda, package shall be installed by conda if you want use it in jupyter!

pylab help info

%pylab?
This sentence will give out the ‘help information’ of pylab.
Note that %pylab will be imported!

magics

Magics come in two kinds:【ct】可以简单理解为单行/多行

Line magics

these are commands prepended by one % character and whose arguments only extend to the end of the current line.

  • %run可以从.py.ipynb文件

Cell magics:

  • Some simple cell magics
1
2
3
%%writefile foo.py
print('foo')
print('Equinox')
  • Magics for running code under other interpreters
    IPython has a %%script cell magic, which lets you run a cell in a subprocess of any interpreter on your system, such as: bash, ruby, perl, zsh, R, etc.
    To use it, simply pass a path or shell command to the program you want to run on the %%script line, and the rest of the cell will be run by that script, and stdout/err from the subprocess are captured and displayed.
1
2
3
%%script python2
import sys
print 'hello from Python %s' % sys.version
  • Background Scripts
    These scripts can be run in the background, by adding the --bg flag.
    When you do this, output is discarded unless you use the --out/err flags to store output as above.
1
2
3
4
5
6
%%ruby --bg --out ruby_lines
for n in 1...10
sleep 1
puts "line #{n}"
STDOUT.flush
end

shortcut key

Jupyter Notebook 的快捷键
Keyboard shortcuts

command mode

  • ESC: command mode

  • h: 显示快捷键帮助

  • Enter: edit mode

  • ctrl+entershitf+enter: 执行、执行后选中下个单元

  • a/b: 在上/下方插入新单元

  • dd: 删除当前cell

  • m: 设为markdown cell

  • y: 单元转入代码状态

  • 3: 设定 3 级标题

  • x/c/v: 剪切/复制/粘贴

  • space/shift+spcae: 向下/上滚动

  • Shift-K/J : 扩大选中上/下方单元

  • 合并/分割
    shfit + m / shift + ctrl + -

  • cmd + s : safe and Checkpoint

  • restart kernel
    00 即在command mode下,

edit mode

  • alt + 左键: 列模式

  • shift+tab: 补全提示

  • ctrl + shift + -: 分割cell

  • cmd+[ 减小缩进

Errors

  • c.NotebookApp.allow_origin and c.NotebookApp.ip :
    They don’t work. Except certain ip, it supposes to be unaccessible when these parameters is set.

Tips

  • change default browser
    $ jupyter notebook --generate-config
    And then in ~/.jupyter/jupyter_notebook_config.py file, c.NotebookApp.browser = ' should be modified to
    c.NotebookApp.browser = 'open -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome %s'.

2018.07.06: In OS X, the easiest way is modifying the system default browser to Chrome and then jupyter-notebook will use Chrome as default browser.

  • about proxy
    **NOTE: proxy extension (such as shadowsocks) will induce error – ERR_EMPTY_RESPONSE **

In Chrome, the use of privoxy will lead to a forever loading page. (Maybe it’s because the local http request is transferred by proxy and hence the port is changed to 12333.)
Solution: You can change SS to “自动代理模式” and start jupyter, and then change SS back to “全局代理模式”.

  • Pretty Display of Varibles

    1
    2
    from IPython.core.interactiveshell import InteractiveShell
    InteractiveShell.ast_node_interactivity = "all"
  • 不让变量输出
    结尾加个分号!variable;

  • 添加目录功能
    conda install -c conda-forge jupyter_contrib_nbextensions

  • convert to .py file
    jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb

  • jupyter notebook list
    Show running notebook

  • add kernel & edit kernel
    Add kernel:
    How do I add python3 kernel to jupyter (IPython)
    NOTE: Don’t forget to point to a specific python.

    1
    2
    python3.7-m pip install ipykernel
    python3.7 -m ipykernel install --user

Edit kernel:
Jupyter notebook not pointing to Anaconda


source 1
source 2

27 个Jupyter Notebook的小提示与技巧

reference

谢谢~