Jupyter Notebooks 入门
Jupyter IPython 笔记

> 
> 通过 Jupyter 使用实时代码、方程式和可视化及文本创建交互式的共享笔记本。
> 
> 
> 


![](/data/attachment/album/201805/21/082420s8r3azrm38op8b1n.png)


自从有了纸莎草纸以来，出版人们一直在努力以吸引读者的方式来格式化数据。尤其是在数学、科学、和编程领域，设计良好的图表、插图和方程式可以成为帮助人们理解技术信息的关键。


[Jupyter Notebook](http://jupyter.org/) 通过重新构想我们如何制作教学文本来解决这个问题。Jupyter （我在 2017 年 10 月在 [All Things Open](https://allthingsopen.org/) 上首次了解到）是一款开源应用程序，它使用户能够创建包含实时代码、方程式、可视化和文本的交互式共享笔记本。


Jupyter 从 [IPython 项目](http://ipython.org/)发展而来，它是个具有交互式 shell 和基于浏览器的笔记本，支持代码、文本和数学表达式。Jupyter 支持超过 40 种编程语言，包括 Python、R 和 Julia，其代码可以导出为 HTML、LaTeX、PDF、图像和视频，或者作为 [IPyhton](https://en.wikipedia.org/wiki/IPython) 笔记本与其他用户共享。



> 
> 一个有趣的事实是：“Jupyter” 是 “Julia、Python 和 R” 的缩写。
> 
> 
> 


根据 Jupyter 项目网站介绍，它的一些用途包括“数据清理和转换，数值模拟，统计建模，数据可视化，机器学习等等”。科学机构正在使用 Jupyter Notebooks 来解释研究结果。代码可以来自实际数据，可以调整和重新调整以可视化成不同的结果和情景。通过这种方式，Jupyter Notebooks 变成了生动的文本和报告。


### 安装并开始 Jupyter


Jupyter 软件是开源的，其授权于[修改过的 BSD 许可证](https://opensource.org/licenses/BSD-3-Clause)，它可以[安装在 Linux、MacOS 或 Windows 上](http://jupyter.org/install.html)。有很多种方法可以安装 Jupyter；我在 Linux 和 MacOS 上试过 PIP 和 [Anaconda](https://www.anaconda.com/download/#linux) 安装方式。PIP 安装要求你的计算机上已经安装了 Python；Jupyter 推荐 Python 3。


由于 Python 3 已经安装在我的电脑上，我通过在终端（在 Linux 或 Mac 上）运行以下命令来安装 Jupyter：



```
$ python3 -m pip install --upgrade pip
$ python3 -m pip install jupyter

```

在终端提示符输入以下命令立即启动应用程序：



```
$ jupyter notebook

```

很快，我的浏览器打开并显示了我在 `http://localhost:8888` 的 Jupyter Notebook 服务器。（支持的浏览器有 Google Chrome、Firefox 和 Safari）


![](/data/attachment/album/201805/21/082421dnx6fqqawddisdxq.png)


在右上角有一个标有 “New” 的下拉菜单，它使我能够根据自己的指示和代码快速创建新的笔记本。请注意，我的新笔记本默认为 Python 3，这是我目前的环境。


![](/data/attachment/album/201805/21/082421kmzvrfqtb9vvnvn6.png)


一个带有一些默认值的新笔记本，它可以被改变（包括笔记本的名字），已打开。


![](/data/attachment/album/201805/21/082421nqp66yepjym62vgx.png)


笔记本有两种不同的模式：“命令模式”和“编辑模式”。命令模式允许你添加或删除单元格。你可以通过按下 `Escape` 键进入命令模式，按 `Enter` 键或单击单元格进入编辑模式。


单元格周围的绿色高亮显示你处于编辑模式，蓝色高亮显示你处于命令模式。以下笔记本处于命令模式并准备好执行单元中的 Python 代码。注意，我已将笔记本的名称更改为 “First Notebook”。


![](/data/attachment/album/201805/21/082422vj8ane2zu85op753.png)


### 使用 Jupyter


Jupyter Notebooks 的强大之处在于除了能够输入代码之外，你还可以用 Markdown 添加叙述性和解释性文本。我想添加一个标题，所以我在代码上面添加了一个单元格，并以 Markdown 输入了一个标题。当我按下 `Ctrl+Enter` 时，我的标题转换为 HTML。（LCTT 译注：或者可以按下 Run 按钮。）


![](/data/attachment/album/201805/21/082422j434avlvey0fx7va.png)


我可以通过在命令前追加 `!` 来添加 Bash 命令或脚本的输出。


![](/data/attachment/album/201805/21/082423enzn3w8snluzl66r.png)


我也可以利用 IPython 的 [line magic 和 cell magic](http://ipython.readthedocs.io/en/stable/interactive/magics.html) 命令。你可以通过在代码单元内附加 `%` 或 `%%` 符号来列出魔术命令。例如，`%lsmagic` 将输出所有可用于 Jupyter notebooks 的魔法命令。


![](/data/attachment/album/201805/21/082423lx5f31vj9mzzf1u1.png)


这些魔术命令的例子包括 `%pwd`——它输出当前工作目录（例如 `/Users/YourName`）和 `%ls`——它列出当前工作目录中的所有文件和子目录。另一个神奇命令显示从笔记本中的 `matplotlib` 生成的图表。`%%html` 将该单元格中的任何内容呈现为 HTML，这对嵌入视频和链接很有用，还有 JavaScript 和 Bash 的单元魔术命令。


如果你需要更多关于使用 Jupyter Notebooks 和它的特性的信息，它的帮助部分是非常完整的。


人们用许多有趣的方式使用 Jupyter Notebooks；你可以在这个[展示栏目](https://github.com/jupyter/jupyter/wiki/a-gallery-of-interesting-jupyter-notebooks#mathematics)里找到一些很好的例子。你如何使用 Jupyter 笔记本？请在下面的评论中分享你的想法。




---


via: <https://opensource.com/article/18/3/getting-started-jupyter-notebooks>


作者：[Don Watkins](https://opensource.com/users/don-watkins) 选题：[lujun9972](https://github.com/lujun9972) 译者：[MjSeven](https://github.com/MjSeven) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Since the days of papyrus, publishers have struggled with formatting data in ways that engage readers. This is a particular issue in the areas of mathematics, science, and programming, where well-designed charts, illustrations, and equations can be key to helping people understand technical information.

[The Jupyter Notebook](http://jupyter.org/) is addressing this problem by reimagining how we produce instructional texts. Jupyter (which I first learned about at [All Things Open](https://allthingsopen.org/) in October 2017) is an open source application that enables users to create interactive, shareable notebooks that contain live code, equations, visualizations, and text.

Jupyter evolved from the [IPython Project](http://ipython.org/), which features an interactive shell and a browser-based notebook with support for code, text, and mathematical expressions. Jupyter offers support for over 40 programming languages, including Python, R, and Julia, and its code can be exported to HTML, LaTeX, PDF, images, and videos or as [IPython](https://en.wikipedia.org/wiki/IPython) notebooks to be shared with other users.

*Fun fact: "Jupyter" is an acronym for "Julia, Python, **and** R."*

Some of its uses, according to Project Jupyter's website, include "data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more." Scientific institutions are using Jupyter Notebooks to explain research results. The code, which can come from actual data, can be tweaked and re-tweaked to visualize different results and scenarios. In this way, Jupyter Notebooks have become living texts and reports.

## Installing and starting Jupyter

The Jupyter software is open source, licensed under the [modified BSD license](https://opensource.org/licenses/BSD-3-Clause), and it can be [installed on Linux, MacOS, or Windows](http://jupyter.org/install.html). There are a number of ways to install Jupyter; I tried the PIP and the [Anaconda](https://www.anaconda.com/download/#linux) installs on Linux and MacOS. A PIP install requires that Python is already installed on your computer; Jupyter recommends Python 3.

Since Python 3 was already installed on my computers, I installed Jupyter by running the following commands in the terminal (on Linux or Mac):

```
``````
$ python3 -m pip install --upgrade pip
$ python3 -m pip install jupyter
```


Entering the following command at the terminal prompt started the application right away:

```
````$ jupyter notebook`


Soon my browser opened and displayed my Jupyter Notebook server at `http://localhost:8888`

. (Supported browsers are Google Chrome, Firefox, and Safari.)

![Jupyter Notebook server](https://opensource.com/sites/default/files/u128651/jupyter_1.png)

opensource.com

There's a drop-down menu labeled "New" in the upper-right corner. It enabled me to quickly create a new notebook for my own instructions and code. Note that my new notebook defaults to Python 3, which is my current environment.

![Creating a new notebook](https://opensource.com/sites/default/files/u128651/jupyter_2.png)

opensource.com

A new notebook with some default values, which can be changed (including the name of the notebook), opened.

![New notebook](https://opensource.com/sites/default/files/u128651/jupyter_3.png)

opensource.com

Notebooks have two different modes: Command and Edit. Command mode allows you to add or delete cells. You can enter Command mode by pressing the Escape key, and you can get into Edit mode by pressing the Enter key or by clicking in a cell.

A green highlight around a cell indicates you are in Edit mode, and a blue highlight means you're in Command mode. The following notebook is in Command mode and ready for me to execute the Python code in the cell. Note that I've changed the name of the notebook to First Notebook.

![First Notebook in Command mode](https://opensource.com/sites/default/files/u128651/jupyter_4.png)

opensource.com

## Using Jupyter

A strength of Jupyter Notebooks is that besides being able to enter code, you can also add Markdown with narrative and explanatory text. I wanted to add a heading, so I added a cell above my code and typed a heading in Markdown. When I pressed `Ctrl+Enter`

, my heading converted to HTML.

![Adding a heading](https://opensource.com/sites/default/files/u128651/jupyter_5.png)

opensource.com

I can add output from a Bash command or script by appending an `!`

to a command.

![Bash in Jupyter](https://opensource.com/sites/default/files/u128651/jupyter_6.png)

opensource.com

I can also take advantage of IPython's [line magic and cell magic](http://ipython.readthedocs.io/en/stable/interactive/magics.html) commands. You can list the magic commands by appending a `%`

or `%%`

sign to commands inside a code cell. For example, `%lsmagic`

produces all the magic commands that can be used in Jupyter notebooks.

![Magic commands](https://opensource.com/sites/default/files/u128651/jupyter_7.png)

opensource.com

Examples of these magic commands include `%pwd`

, which outputs the present working directory (e.g., `/Users/YourName`

) and `%ls`

, which lists all the files and subdirectories in the current working directory. Another magic command displays charts generated from `matplotlib`

in the notebook. `%%html`

renders anything in that cell as HTML, which is useful for embedding video and links. There are cell magic commands for JavaScript and Bash, also.

If you need more information about using Jupyter Notebooks and its features, the Help section is incredibly complete.

People are using Jupyter Notebooks in many interesting ways; you can find some great examples in this [gallery](https://github.com/jupyter/jupyter/wiki/a-gallery-of-interesting-jupyter-notebooks#mathematics). How are you using Jupyter notebooks? Please share your ideas in the comments below.

## 1 Comment