jupyter-notebook-sen-usage

安装jupyter-notebook

1
$ pip install jupyter

配置jupyter-notebook

配置可以同时使用python2和python3内核

1
2
3
4
5
$ ipython kernel install --user
$ python3 -m ipykernel install --user
$ pip2 install -U ipykernel
$ python2 -m ipykernel install --user
$ jupyter-notebook //运行,会自动web界面,可以同时运行python2,python3,ctrl+c结束

生成配置文件

1
2
3
$ cd 
$ jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py

生成密码

运行jupyter

1
2
3
4
5
6
7
In [1]: from notebook.auth import passwd
passwd()

Enter password: ········
Verify password: ········

Out[1]: 'sha1:c3a52264ad87:f6a2c3503ee3370c67da1f723ae1e8e79477f5f7'

设置密码

将前面生成的一串密码替换到配置文件中

1
2
$ vim /root/.jupyter/jupyter_notebook_config.py 
c.NotebookApp.password=u'sha1:c3a52264ad87:f6a2c3503ee3370c67da1f723ae1e8e79477f5f7' //前面u表示转换成unicode字符,python2的时候需要带

其它设置

1
2
3
4
c.NotebookApp.ip = '*'  //访问ip限制
c.NotebookApp.notebook_dir = '/home/knmax/Desktop/Python/jupyter-project' //工作目录,路径不能出现中文
c.NotebookApp.open_browser = False //不自动打开浏览器
c.NotebookApp.port = 88 //运行监听的端口

以服务方式运行

每次运行打开都是终端交互的界面,关闭会话终端也结束了jupyter,很不方便,这里做成以systemctl方式启动,适用于Debain、CentOS 7、Ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ vim /lib/systemd/system/jupyter.service  //这个目录不同发行版可能也不同
[Unit]
Description=jupyter
After=network.target
[Service]
Tpye=forking
EnvironmentFile=/usr/local/bin/jupyter-notebook
ExecStart=/usr/local/bin/jupyter-notebook
ExecStop=/usr/bin/pkill jupyter-notebook
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target

创建好之后再操作下就行了

1
2
3
$ systemctl daemon-reload
$ systemctl start jupyter
$ systemctl enable jupyter

局域网访问jupyter notebook

用电脑开wifi,手机连上电脑的wifi,手机可以直接从电脑的局域网ip访问到电脑上的jupyter notebook

安装额外插件或kernel

显示稿插件

1
2
3
4
$ pip install RISE 
$ pip3 install RISE
$ jupyter-nbextension install rise --py --sys-prefix
$ jupyter nbextension enable rise --py --sys-prefix

安装Jupyter Notebook extensions–>Jupyter拓展插件!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ conda install -c conda-forge jupyter_contrib_nbextensions

或者

$ pip install jupyter_contrib_nbextensions

还不行就把下面的都试一遍...
$ pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master
$ pip3 install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master
$ pip install jupyter_nbextensions_configurator
$ pip3 install jupyter_nbextensions_configurator
$ jupyter contrib nbextension install --user
$ jupyter nbextensions_configurator enable --user
$ systemctl restart jupyter

还有......再试一试又不会怀孕...

$ pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master
$ pip install jupyter_nbextensions_configurator
$ jupyter contrib nbextension install –user
$ jupyter nbextensions_configurator enable –user

开启toc2插件
命令行输入jupyter notebook运行,在浏览器打开notebook的Home页面,多了一个Nbextensions,点进去就懂了
这个用起来很爽,可以增加许多功能,尤其是里面可以增加侧边栏,这个用起来对代码管理就看起来层次分明多了,找代码也更方便了~~

顺便note一下,代码折叠,生成目录,自动格式化很有用~~~

安装主题、字体

1
2
3
4
$ pip install --upgrade jupyterthemes
$ pip3 install --upgrade jupyterthemes
$ jt -l 查看可用主题
$ jt -t grade3 -f ptmono -fs 115

参考地址

jupyter支持的内核

参考地址

多指针

Jupyter支持多个指针同步编辑,类似Sublime Text编辑器。按下Alt键并拖拽鼠标即可实现。====这个我用着很不顺手,按住ctrl后用移动鼠标可实现一样的多次选中,我还是喜欢用这个。。

隐藏代码只显示代码输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from IPython.display import HTML

HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')

也可以这样:

1
2
3
4
5
6
7
8
9
10
11
12
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}

$([IPython.events]).on("app_initialized.NotebookApp", function () {
$("#view_menu").append("<li id=\"toggle_toolbar\" title=\"Show/Hide code cells\"><a href=\"javascript:code_toggle()\">Toggle Code Cells</a></li>")

或者这样:

1
2
3
4
5
6
7
8
9
from IPython.display import display
from IPython.display import HTML
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)

# This line will hide code by default when the notebook is exported as HTML
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)

# This line will add a button to toggle visibility of code blocks, for use with the HTML export version
di.display_html('''<button onclick="jQuery('.input_area').toggle(); jQuery('.prompt').toggle();">Toggle code</button>''', raw=True)

这样:

1
2
3
4
5
6
7
8
9
$([IPython.events]).on('notebook_loaded.Notebook', function(){
IPython.toolbar.add_buttons_group([
{
'label' : 'toggle input cells',
'icon' : 'icon-refresh',
'callback': function(){$('.input').slideToggle()}
}
]);
});

甚至可以这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# This is a cell to hide code snippets from displaying
# This must be at first cell!

from IPython.display import HTML

hide_me = ''
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show) {
$('div.input').each(function(id) {
el = $(this).find('.cm-variable:first');
if (id == 0 || el.text() == 'hide_me') {
$(this).hide();
}
});
$('div.output_prompt').css('opacity', 0);
} else {
$('div.input').each(function(id) {
$(this).show();
});
$('div.output_prompt').css('opacity', 1);
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input style="opacity:0" type="submit" value="Click here to toggle on/off the raw code."></form>''')

惊不惊喜?刺不刺激?
[参考]:(http://stackoverflow.com/questions/27934885/how-to-hide-code-from-cells-in-ipython-notebook-visualized-with-nbviewer)

全部显示

有一点已经众所周知。把变量名称或没有定义输出结果的语句放在cell的最后一行,无需print语句,Jupyter也会显示变量值。当使用Pandas DataFrames时这一点尤其有用,因为输出结果为整齐的表格。

鲜为人知的是,你可以通过修改内核选项ast_node_interactivity,使得Jupyter对独占一行的所有变量或者语句都自动显示,这样你就可以马上看到多个语句的运行结果了。

1
2
3
In [1]: from IPython.core.interactiveshell import InteractiveShell

InteractiveShell.ast_node_interactivity = "all"

如果你想在各种情形下(Notebook和Console)Jupyter都同样处理,用下面的几行简单的命令创建文件~/.ipython/profile_default/ipython_config.py即可实现:

1
2
3
4
5
c = get_config()

# Run all nodes interactively

c.InteractiveShell.ast_node_interactivity = "all"

这个刚了解时用起来很开心,不过当用到matplotlib时会输出很多信息,看起来比较丑,我就弃用了…

末句函数不输出

有时候不让末句的函数输出结果比较方便,比如在作图的时候,此时,只需在该函数末尾加上一个分号即可===这个用起来作的图看起来就清爽多了;

嘿嘿嘿…

在notebook中作图

如果不想每次用matplotlib 作图后都要输入plt.show()来弹出显示图,可以如下:

Jupyter notebook的magic操作

上面介绍的%matplotlib inline就是其中的一个魔术操作,作图时用起来流畅极了;

  • %run ====用来运行代码脚本
  • %store ====命令可以在两个notebook文件之间传递变量,没用过。。
  • %who ====不加任何参数,命令可以列出所有的全局变量。加上参数 str 将只列出字符串型的全局变量

有两种用于计时的jupyter magic命令:
当你有一些很耗时的代码,想要查清楚问题出在哪时,这两个命令非常给力。

  • %%time 会告诉你cell内代码的单次运行时间信息。
  • %%timeit 使用了Python的 timeit 模块,该模块运行某语句100,000次(默认值),然后提供最快的3次的平均值作为结果。
  • %prun+函数声明 会给你一个按顺序排列的表格,显示每个内部函数的耗时情况,每次调用函数的耗时情况,以及累计耗时。

Jupyter 有自己的调试界面The Python Debugger (pdb)

===这个貌似很强大的样子,暂时还没用过,有机会我要试试~
https://docs.python.org/3.5/library/pdb.html),使得进入函数内部检查错误成为可能。
Pdb中可使用的命令见链接(https://docs.python.org/3.5/library/pdb.html#debugger-commands

运行Shell命令

在notebook中可以用cd 来切换目录;
ls用来显示当前目录内容;
$ pip install或者$ conda install用来使用cmd下的命令操作;

Jupyter-Notebook 的快捷键

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

命令模式 (按键 Esc 开启)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 键启动)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 :光标下移或转入下一单元

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器