Jupyter Notebook基本操作介绍

工欲善其事,必先利其器。

Jupyter Notebook来源于Ipython Notebook。根据Ipython官网的介绍:

Jupyter and the future of IPython
IPython is a growing project, with increasingly language-agnostic components. IPython 3.x was the last monolithic release of IPython, containing the notebook server, qtconsole, etc. As of IPython 4.0, the language-agnostic parts of the project: the notebook format, message protocol, qtconsole, notebook web application, etc. have moved to new projects under the name Jupyter. IPython itself is focused on interactive Python, part of which is providing a Python kernel for Jupyter.

Ipython未来会专注在喝Python的交互上,把与语言无关的部分,包括笔记的记录、消息协议、web应用等内容全部转移到Jupyter上,并且会为Jupyter提供Python的内核。可以说Ipython是基于Python内核提供了更好的交互特性的Shell,而Ipython Notebook是基于Ipython的基础上提供了更多面向用户的友好功能,Jupyter是Ipython Notebook的升级版,并且以后Ipython Notebook会逐渐取消名称,转而用Jupyter Notebook代替。

对于初学者来说,Jupyter Notebook提供了良好的体验,可视化效果更好。任何事物都有两面性,Why not use Jupyter Notebook?提到了一些不适合的场景。不过对于新手或者并不需要大量的编程,或者主要是用于简单的科学计算、展示的人,Jupyter还是非常好的选择。

原理

How IPython and Jupyter Notebook work中给出了执行过程。

The Notebook frontend does something extra. In addition to running your code, it stores code and output, together with markdown notes, in an editable document called a notebook. When you save it, this is sent from your browser to the notebook server, which saves it on disk as a JSON file with a .ipynb extension.
The notebook server, not the kernel, is responsible for saving and loading notebooks, so you can edit notebooks even if you don’t have the kernel for that language—you just won’t be able to run code. The kernel doesn’t know anything about the notebook document: it just gets sent cells of code to execute when the user runs them.

关于Jupyter的项目组成架构如下图:

具体内容参见官方文档:A Visual Overview of Projects

Jupyter Notebook的文档有一些庞杂,对新手入门略有难度,建议保持最小行动,从The Jupyter notebook User Documentation 开始。有一定基础之后看ipywidgets文档,了解交互控件的应用。

安装

在Jupyter Notebook安装说明中强烈建议新手安装Anaconda。Anaconda是一个集成环境,其中内置了包括在Jupyter Notebook的其他一些方便科学计算的环境,配置极其简单。除了Linux可能需要少量配置外,其他系统直接安装即可。

强烈建议不要使用Windows系统,在后续应用中会出现各种奇怪的问题,如果一定要使用,建议全路径为英文,并且尽量的少用中文,规避可能出现的问题。

安装后建议查看Installation, Configuration, and Usage文档了解基于Jupyter的功能说明。

启动和关闭

Jupyter Notebook需要启动服务后在本地的浏览器运行客户端的服务:

1
2
3
cd your_dir
ipython notebook
jupyter notebook

首先转到你希望的工作目录下,然后启动服务。ipython notebook或者jupyter notebook都可以,二选一即可。然后在浏览器输入http://localhost:8888/tree就进入了客户端,可以开始干活啦,这时展示的目录就是你的工作目录,如果想切换到其他目录,停止服务,转到对应的目录即可。

关闭服务也很方便,在对应的终端服务点击【Ctrl】+【C】即可。

关于启动项的一些特殊选项介绍参见Introducing the Notebook Server’s Command Line Options,代码如下:

1
2
3
jupyter notebook --port 9999
jupyter notebook --no-browser
jupyter notebook --help

界面说明

首先要了解界面中各个功能的名称,方便以后英文搜索。UI Components有详细说明,这里挑出关键名词用图片说明:

Notebook Dashboard

  • File Tree:你的服务所在的目录就是File Tree的顶级目录。

Notebook Editor


在你的Notebook打开的文件上点击”Help”->”User Interface Tour”可以了解上图中具体的按钮和对应位置说明。

编辑模式和命令模式


在编辑模式(Edit Mode)下,右上角出现一只铅笔的图标,点Esc键切换回命令模式。
在命令模式(Command Mode)下,铅笔图标消失,按Enter键(或者双击cell)变为编辑状态。

界面上的每一个可以录入内容的地方叫做Cell,Cell有四种功能:Code、Markdown、Row NBConvert、Heading。配合是否进入功能,有两种模式。关于四种功能,在下面介绍。

Markdown

关于Markdown在Jupyter Notebook的使用,这里有具体的语法说明。采用了GitHub flavored markdown的渲染方式,书写语法问题可以参考Github的Help。LaTeX可以直接使用,包含在$或者SS中,其他用法和Q&A:科学写作中的基本工具和用法一致。更多案例说明参见Motivating Examples

Code

点击Shift-Enter可以运行代码
Alt-Enter runs the current cell and inserts a new one below.
Ctrl-Enter run the current cell and enters command mode.
更多特性参见Running Code

快捷键

快捷键的使用需要区分命令模式和编辑模式:

命令模式下常用快捷键

  • a: 在当前 cell 前添加 cell;
  • b: 在当前 cell 后添加 cell
  • 按两下d: 删除当前 cell
  • 查看快捷键帮助:h
  • 保存: s
  • cell间移动: j, k
  • 添加cell: a, b
  • 删除cell: dd
  • cell编辑: x, c, v, z
  • 中断kernel: ii
  • 重启kernel: 00
  • 先用shift+jshift+k选中想合并的单元,然后用shift+m合并

编辑模式下快捷键

  • Control + Enter: 显示当前 cell 运行结果
  • Shift + Enter: 显示当前 cell 运行结果, 并将光标跳转至下一个 cell
  • alt + Enter: 运行当前 cell, 并自动在后面新建一个cell
    编辑模式下按control+shift+-可拆分cell
  • 改变Cell的状态: y, m, 1-6, t
  • 执行Cell: Shift+Enter并转到下一个Cell
  • 在面板的”Help”->”keyboard”->”Short Cut”可以看到更多快捷键

导出

Jupyter Notebook可以方便的把笔记内容导出成其他格式的内容,具体信息参见Q:Ipython notebook文章如何直接发布博客?

Ipython特性

内置魔法函数(Built-in magic commands)

  • %:行魔法函数,只对本行代码生效。
  • %%:Cell魔法函数,在Cell中生效。
    一些魔法函数的示例
    %magic
    %lsmagic:列出所有的魔法函数
    %%writefile hello.py:调用外部python脚本
    %run hello.py:调用外部python脚本

Tips

Jupyter Notebook的使用有很多小技巧,根据自己的经验添加:

Reference

Ipython-quick-ref-sheets
IPython Notebook Shortcuts

声明: 本文转载需标明出处,禁止用于商业目的。

ChangeLog

161129 新建
161130 发布