jupyter & ipython - duxiaoyao/pdp GitHub Wiki

现在应该用jupyterlab

TL;DR: ipython, jupyterlab, python debugger in jupyterlab and matplotlib & plotly in jupyterlab

pip install ipython xeus-python jupyterlab matplotlib ipympl kaleido plotly
python debugger: switch kernel from ipython to xpython

jupyterlab-lsp: Coding assistance for JupyterLab

code navigation + hover suggestions + linters + autocompletion + rename, using Language Server Protocol

conda install jupyter-lsp 
conda install python-language-server[all]

useful 3rd-party extensions

line_profiler, RISE (Slideshow)

ipython console in Pycharm

For example, instruct Pycharm to automatically reload modules upon changing by adding the following lines to settings->Build,Excecution,Deployment->Console->Python Console in the Starting Script:

%load_ext autoreload
%autoreload 2

git

install jupyterlab-git

ipython

shell + kernel
autocompletion: Tab
? ?? help %quickref %lsmagic
history: %history, %recall, %rerun
shell command: !
常用魔法命令(% or %%):%autoreload, %time, %timeit, %script, %prun, %lprun, %debug, %pdb, %xmode, %memit, %mprun, %store, %xdel, %reset, %cls, %who, %whos, %run, %hist

ipywidgets

Prerequisite: pip install jupyterlab ipywidgets

from ipywidgets import interact
@interact(x=1.0)
def g(x):
    return x**2

Matplotlib JupyterLab Integration

conda install ipympl
Usage: %matplotlib widget

Plotly JupyterLab Integration

Just works and nothing required now

jupyterlab

start: nohup jupyter lab &> ~/tmp/nohup.out &
notebook: kernel, cell

ctrl-b: 左边栏收起或展开;ctrl-shift-d:single document mode;鼠标右键:上下文提示;method?或?method后ctrl-enter:显示帮助文档;method??或??method后ctrl-enter:显示帮助文档和源码;np.*sum*?或?np.*sum*后ctrl-enter:显示匹配的名字;tab:补全;ctrl/shift/alt-enter:运行cell/select below/insert below;ESC或ctrl-m:command mode Enter:edit mode;Y/M/R:code/markdown/raw cell type;a/b:insert a cell above/below;c/x/v/shift-v/dd:copy/cut/paste-below/paste-up/delete;down/j/k/up:move cursor down/down/up/up cell;z/shift-z:undo/redo command;ctrl-z/ctrl-shift-z:undo/redo editing;ctrl-Shift--/shift-m:split-cell/merge-cells;Space/Shift-Space:Scroll notebook;ESC-00:Restart notebook;

Skip a cell from running: %%script false

Multiple outputs for a cell:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'

%debug

Try introducing a bug in the code (e.g., use an incorrect variable name: points.append((x, y2, True)))
run the cell
after the exception occurs, run the %debug magic in a new cell to enter an interactive debugger
type h for a help menu, and help <keyword> for help on keyword
type p x to print the value of x
exit the debugger by typing q

cell

code cell: 左侧会有一个标签 In [1] ,这里的数字表示该代码块运行的次序,即在该 notebook 中,如果没有执行,显示的是 In [ ]
markdown cell: 左侧没有标签 In [ ]
对于一个单元,如果正在编辑,其边界框显示的是绿色,而运行时候则显示蓝色。这里展示的就是两种模式,即绿色表示编辑模式,蓝色表示命令模式。
Markdown 的使用方法可以参考其发明者 John Gruber 的官方教程:https://daringfireball.net/projects/markdown/syntax

快捷键

可以通过菜单中的 Help->Keyboard Shortcuts 查看,也可以直接用快捷键 Ctrl+Shift+P 查看。
编辑模式和命令模式可以通过 Esc 和 Enter 进行转换,一般是按 Esc 进入命令模式,Enter 进入编辑模式。
Ctrl + Enter: run the cell, Shift + Enter: run the cell and insert a new cell below
Ctrl + Shift + -, in edit mode, will split the active cell at the cursor.
You can also click and Shift + Click in the margin to the left of your cells to select them.

在命令模式下:

  • 在 cell 之间上下浏览采用上下箭头,或者 Up 和 Down 键
  • A 表示在当前 cell 上方插入一个新的 cell ,而 B 则是下方插入新的cell
  • M 表示变为 Markdown cell ,而 Y 是表示变为 code cell
  • 连续按两次 D 是删除当前 cell
  • Z 是撤销操作
  • Shift 加上 Up 或者 Down 可以一次选择多个 cells ,接着采用 Shift + M 可以合并多个 cells

Jupyter Notebook 有两种键盘输入模式。编辑模式,允许你往单元中键入代码或文本;这时的单元框线是绿色的。命令模式,键盘输入运行程序命令;这时的单元框线是灰色。

命令模式 (按键 Esc 开启):
Enter : 转入编辑模式
Shift-Enter : 运行本单元,选中下个单元
Ctrl-Enter : 运行本单元
Alt-Enter : 运行本单元,在其下插入新单元
Y : 单元转入代码状态
M :单元转入markdown状态
R : 单元转入raw状态
1 : 设定 1 级标题
2 : 设定 2 级标题
3 : 设定 3 级标题
4 : 设定 4 级标题
5 : 设定 5 级标题
6 : 设定 6 级标题
Up : 选中上方单元
K : 选中上方单元
Down : 选中下方单元
J : 选中下方单元
Shift-K : 扩大选中上方单元
Shift-J : 扩大选中下方单元
A : 在上方插入新单元
B : 在下方插入新单元
X : 剪切选中的单元
C : 复制选中的单元
Shift-V : 粘贴到上方单元
V : 粘贴到下方单元
Z : 恢复删除的最后一个单元
D,D : 删除选中的单元
Shift-M : 合并选中的单元
Ctrl-S : 文件存盘
S : 文件存盘
L : 转换行号
O : 转换输出
Shift-O : 转换输出滚动
Esc : 关闭页面
Q : 关闭页面
H : 显示快捷键帮助
I,I : 中断Notebook内核
0,0 : 重启Notebook内核
Shift : 忽略
Shift-Space : 向上滚动
Space : 向下滚动

编辑模式 ( Enter 键启动):
Tab : 代码补全或缩进
Shift-Tab : 提示
Ctrl-] : 缩进
Ctrl-[ : 解除缩进
Ctrl-A : 全选
Ctrl-Z : 复原
Ctrl-Shift-Z : 再做
Ctrl-Y : 再做
Ctrl-Home : 跳到单元开头
Ctrl-Up : 跳到单元开头
Ctrl-End : 跳到单元末尾
Ctrl-Down : 跳到单元末尾
Ctrl-Left : 跳到左边一个字首
Ctrl-Right : 跳到右边一个字首
Ctrl-Backspace : 删除前面一个字
Ctrl-Delete : 删除后面一个字
Esc : 进入命令模式
Ctrl-M : 进入命令模式
Shift-Enter : 运行本单元,选中下一单元
Ctrl-Enter : 运行本单元
Alt-Enter : 运行本单元,在下面插入一单元
Ctrl-Shift-- : 分割单元
Ctrl-Shift-Subtract : 分割单元
Ctrl-S : 文件存盘
Shift : 忽略
Up : 光标上移或转入上一单元
Down :光标下移或转入下一单元

Magics

the shell in which ! commands are executed is discarded after execution completes.
line magics %
cell magics %%
Timing Execution: %time, %timeit, %%time, %%timeit
Execute external scripts: %load and %run

Top 5 magic commands

  • Know how much time your code needs to run: %time, %timeit, %%time, %%timeit
  • Ensure that Jupyter Notebooks show your plots: %matplotlib inline
  • Load the most important extension autoreload: %load_ext autoreload and %autoreload 2
  • Use the shell: %system date or !!date
  • Show the list of variables in your environment: %who_ls or %who_ls function

1st cell

%autosave 10
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="darkgrid")

更多

Read: 28 Jupyter Notebook Tips, Tricks, and Shortcuts for Data Science

References

  1. Jupyter Notebook for Beginners: A Tutorial
  2. Advanced Jupyter Notebook Tutorial
  3. 28 Jupyter Notebook Tips, Tricks, and Shortcuts for Data Science
⚠️ **GitHub.com Fallback** ⚠️