Jupyter Notebook基本使用方法

①基本介绍

Jupyter Notebook是基于网页的用于交互计算的应用程序。其可被应用于开发、文档编写、运行代码和展示结果。优势在于可以在网页中按单元格分块编写Python代码和 Markdown文字

②安装Jupyter Notebook

在有Python环境的命令行中输入以下命令即可

pip install jupyter

jupyter notebook python案例 python中jupyter notebook_jupyter

③打开Jupyter Notebook

在已安装Jupyter Notebook的环境中输入以下命令

jupyter notebook

jupyter notebook python案例 python中jupyter notebook_Python_02


之后将在浏览器中自动弹出以下页面,页面显示的是当前用户C盘下所有的文件夹

如在使用jupyter notebook命令后未弹出,可在浏览器中手动输入以下网址

http://localhost:8888

jupyter notebook python案例 python中jupyter notebook_搜索_03

④错误情况

在访问http://localhost:8888如果后如显示以下情况

Token authentication is enabled
If no password has been configured, you need to open the notebook server with its login token in the URL, or paste it above. This requirement will be lifted if you enable a password.
The command:jupyter notebook list will show you the URLs of running servers with their tokens, which you can copy and paste into your browser. For example:
Currently running servers:
http://localhost:8888/?token=c8de56fa… :: /Users/you/notebooks
or you can paste just the token value into the password field on this page.
See the documentation on how to enable a password in place of token authentication, if you would like to avoid dealing with random tokens.
Cookies are required for authenticated access to notebooks.

jupyter notebook python案例 python中jupyter notebook_搜索_04


在命令行中搜索ctrl + f 搜索token

jupyter notebook python案例 python中jupyter notebook_命令模式_05


复制搜索到的token粘贴在输入框

jupyter notebook python案例 python中jupyter notebook_命令模式_06


或为自己设置一个密码,之后即可使用密码访问

jupyter notebook python案例 python中jupyter notebook_Python_07

⑤使用Jupyter Notebook

在显示上图中位置后,进入自己想存储或打开文件的路径

点击右上角的New 选择Python

jupyter notebook python案例 python中jupyter notebook_搜索_08

Jupyter Notebook很类似Linux系统下的vim编辑器,分为命令模式和编辑模式。
按Esc进入命令模式,单元格中没有光标,单元格左侧变为蓝色

jupyter notebook python案例 python中jupyter notebook_搜索_09


鼠标点击一个单元格,出现光标或按 Enter 回车键,进入编辑模式。编辑模式可以将代码或文本输入到一个单元格中,进入编辑模式后,单元格中有光标,单元格左侧变为绿色

jupyter notebook python案例 python中jupyter notebook_jupyter_10


在命令模式下(按Esc进入),按H可以按到所有快捷键的合集

命令模式快捷键

jupyter notebook python案例 python中jupyter notebook_python_11

编辑模式快捷键

jupyter notebook python案例 python中jupyter notebook_Python_12

⑥常用操作

命令模式下,按Y可以输入Python代码 按M可以输入MarkDown语法的文字,鼠标点击也可以进行切换

jupyter notebook python案例 python中jupyter notebook_搜索_13

shift+enter:运行当前单元格,且光标进入下面的单元格
ctrl+enter: 运行当前单元格,并且光标停留在当前单元格
enter:换行
dd: 删除当前单元格
b:在下方添加一个空单元格
a: 在上方添加一个空单元格

在命令模式下按Y 输入Python代码,输入完成后使用ctrl + enter执行命令,即可输出运行结果

jupyter notebook python案例 python中jupyter notebook_命令模式_14

在命令模式下按M ,按MarkDown语法编辑文字

插入超链接:使用方括号和圆括号 [链接描述] (链接) 注意](之间没有空格

jupyter notebook python案例 python中jupyter notebook_python_15

执行结果如下

jupyter notebook python案例 python中jupyter notebook_Python_16

一级标题 一个#号加一个空格

二级标题 两个#号加一个空格 依次类推

四个星号 加粗

两个星号 斜体

jupyter notebook python案例 python中jupyter notebook_Python_17

执行效果如下

jupyter notebook python案例 python中jupyter notebook_jupyter_18

居中显示 四个$符

无序序列 -加空格

有序序列 阿拉伯数字加点.加空格

代码块 用六个点包围 ```

jupyter notebook python案例 python中jupyter notebook_python_19

运行结果如下

jupyter notebook python案例 python中jupyter notebook_搜索_20

写好的jupyter笔记可以被保存成以下格式并继续看

jupyter notebook python案例 python中jupyter notebook_jupyter_21