jupyter notebook

快捷键

Ctrl+Enter 执行单元格

Shift-Enter 执行单元格 进一格

DD 删除

B(Below)键,在单元格下方新建单元格。可以按 ESC 退出编辑

A 在上方插入

M 转化为 Markdown

Y 转化为代码

Tab 代码补全或缩进

Shift-Tab 代码提示

魔法命令

magic 函数主要包含两大类

行魔法(Line magic)前缀为 % 单元魔法(Cell magic)前缀为 %%;

Jupyter

%lsmagic # 查看所有魔法
%lsmagic? # 查看魔法的帮助
%matplotlib inline #使用matplotlib画图时,图片嵌入在jupyter notebook里面,不以单独窗口显示
%pwd # 和linux一样,查找当前目录
%cd ../
%cp test_peace.py test_load.py
%whos # 查看当前变量,类型,信息
%reset # 清除变量
%load test_peace.py # 加载一个文件里面的内容

%timeit %%timeit #为代码执行计时
import numpy as np
%timeit np.sin(24)

%%timeit
x=np.sin(20)
np.cos(-x)

%%writefile # 后面紧接着一个 file_name.py,表示在 jupyter notebook 里面创建一个 py 文件,后面 cell 里面的内容为 py 文件内容

%%writefile test_peace.py
import numpy as np
print(np.random.randint(1,5))

%run #后面紧接着一个相对地址的 file_name.py,表示运行一个 py 文件

%run test_peace.py

Jupyter 修改主题

# 安装
pip install --upgrade jupyterthemes
# 加载可用主题列表
jt -l
# selecting a particular theme
jt -t <name of the theme>
# 恢复到最初主题
jt -r
# 其中 -f(字体) -fs(字体大小) -cellw(占屏比或宽度) -ofs(输出段的字号) -T(显示工具栏) -N(显示自己主机名)
jt -t grade3 -f fira -fs 13 -cellw 90% -ofs 11 -dfs 11 -T -N

jupyter lab

# 激活已经创建好的虚拟环境
conda activate myenv

# 在虚拟环境中安装 ipykernel
conda install ipykernel
pip install ipywidgets

# 安装 kernel,--name 自定义名称
ipython kernel install --user --name myenv

# 不需要该 kernel 时可以删除
jupyter kernelspec remove myenv