1. 简介

Jupyter Notebook是一款开源的web应用,支持markdown编辑,可以使用Jupyter Notebook来创建一个技术分享的文档。Jupyter Notebook最大的特点是:可以插入Python代码,并且还能运行代码,直接将运行结果展示在文档中。下面是一个使用Jupyter Notebook创建文档的示例,文档中包含三部分:markdown编写的描述、python代码和运行结果。

2. 安装 & 启动
Jupyter Notebook安装有两种方式,我选择的是第一种方式。
-
通过Anaconda安装
Anaconda默认会安装Jupyter Notebook,只要安装了Anaconda,就可以直接使用Jupyter Notebook,关于Anaconda的使用可以参考:Windows下通过Anaconda安装TensorFlow -
通过pip命令安装
如果你安装的是Python3,使用如下命令:python3 -m pip install --upgrade pip python3 -m pip install jupyter
如果你安装的是Python2,使用如下命令:
python -m pip install --upgrade pip python -m pip install jupyter
安装完成以后,在命令行输入命令jupyter notebook,就可以启动Jupyter Notebook了。

我这边是通过Anaconda Prompt输入命令来启动的,先切到D盘的Jupyter Notebook目录,启动成功后,会自动打开浏览器,展示的内容就是D盘Jupyter Notebook目录下存放的文件。


点击文件进入编辑页面,如果有一个No Connection to Kernel的错误提示,表示不能运行Python代码,解决方法:在启动时,指定一个非默认的端口号(默认端口号8888)
jupyter notebook --port 8899


指定端口后重新打开,Kernel状态变成了Kernel Idel,说明已经能够正常运行代码了。

3. 使用
Jupyter Notebook的内容由一个个的cell组成。接下来,我们来看下如何进行编辑、新增和删除等操作

- 双击单个Cell进行编辑
编辑cell很简单,双击你需要编辑的cell,然后进行修改就好了。

- 点击Run Cells运行
编辑完成后,选择Cell->Run Cells进行运行,展示修改以后的效果。我们也可以直接使用快捷键Ctrl-Enter 来运行,表示运行选中的Cell


- 插入新的Cell
我们可以选择在当前Cell的上方或下方进行插入,对应的快捷键:
- A : insert cell above
- B : insert cell below

- 选择Cell类型
默认新插入的Cell类型为code,可以修改为Markdown


-
删除Cell
删除Cell可以通过Edit->Delete Cells进行操作,同样支持快捷键(单击2次D): -
D,D : delete selected cells

-
快捷键查询
上面提到的所有快捷键,都可以通过Help->Keyboard Shortcuts进行查询。


快捷键查询也有对应的快捷键:Ctrl-Shift-F : open the command palette,通过Ctrl-Shift-F查询更加便捷。

网友评论