translated

This commit is contained in:
geekpi 2021-04-08 08:45:45 +08:00
parent 15eafd1677
commit ae2d18b6f7
2 changed files with 191 additions and 190 deletions

View File

@ -1,190 +0,0 @@
[#]: subject: (Why I love using the IPython shell and Jupyter notebooks)
[#]: via: (https://opensource.com/article/21/3/ipython-shell-jupyter-notebooks)
[#]: author: (Ben Nuttall https://opensource.com/users/bennuttall)
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
Why I love using the IPython shell and Jupyter notebooks
======
Jupyter notebooks take the IPython shell to the next level.
![Computer laptop in space][1]
The Jupyter project started out as IPython and the IPython Notebook. It was originally a Python-specific interactive shell and notebook environment, which later branched out to become language-agnostic, supporting Julia, Python, and R—and potentially anything else.
![Jupyter][2]
(Ben Nuttall, [CC BY-SA 4.0][3])
IPython is a Python shell—similar to what you get when you type `python` or `python3` at the command line—but it's more clever and more helpful. If you've ever typed a multi-line command into the Python shell and wanted to repeat it, you'll understand the frustration of having to scroll through your history one line at a time. With IPython, you can scroll back through whole blocks at a time while still being able to navigate line-by-line and edit parts of those blocks.
![iPython][4]
(Ben Nuttall, [CC BY-SA 4.0][3])
It has autocompletion and provides context-aware suggestions:
![iPython offers suggestions][5]
(Ben Nuttall, [CC BY-SA 4.0][3])
It pretty-prints by default:
![iPython pretty prints][6]
(Ben Nuttall, [CC BY-SA 4.0][3])
It even allows you to run shell commands:
![IPython shell commands][7]
(Ben Nuttall, [CC BY-SA 4.0][3])
It also provides helpful features like adding `?` to an object as a shortcut for running `help()` without breaking your flow:
![IPython help][8]
(Ben Nuttall, [CC BY-SA 4.0][3])
If you're using a virtual environment (see my post on [virtualenvwrapper][9], install it with pip in the environment):
```
`pip install ipython`
```
To install it system-wide, you can use apt on Debian, Ubuntu, or Raspberry Pi:
```
`sudo apt install ipython3`
```
or with pip:
```
`sudo pip3 install ipython`
```
### Jupyter notebooks
Jupyter notebooks take the IPython shell to the next level. First of all, they're browser-based, not terminal-based. To get started, install `jupyter`.
If you're using a virtual environment, install it with pip in the environment:
```
`pip install jupyter`
```
To install it system-wide, you can use apt on Debian, Ubuntu, or Raspberry Pi:
```
`sudo apt install jupyter-notebook`
```
or with pip:
```
`sudo pip3 install jupyter`
```
Launch the notebook with:
```
`jupyter notebook`
```
This will open in your browser:
![Jupyter Notebook][10]
(Ben Nuttall, [CC BY-SA 4.0][3])
You can create a new Python 3 notebook using the **New** dropdown:
![Python 3 in Jupyter Notebook][11]
(Ben Nuttall, [CC BY-SA 4.0][3])
Now you can write and execute commands in the `In[ ]` fields. Use **Enter** for a newline within the block and **Shift+Enter** to execute:
![Executing commands in Jupyter][12]
(Ben Nuttall, [CC BY-SA 4.0][3])
You can edit and rerun blocks. You can reorder them, delete them, copy/paste, and so on. You can run blocks in any order—but be aware that any variables created will be in scope according to the time of execution, rather than the order they appear within the notebook. You can restart and clear output or restart and run all blocks from within the **Kernel** menu.
Using the `print` function will output every time. But if you only have a single statement that's not assigned or your last statement is unassigned, it will be output anyway:
![Jupyter output][13]
(Ben Nuttall, [CC BY-SA 4.0][3])
You can even refer to `In` and `Out` as indexable objects:
![Jupyter output][14]
(Ben Nuttall, [CC BY-SA 4.0][3])
All the IPython features are available and are often presented a little nicer, too:
![Jupyter supports IPython features][15]
(Ben Nuttall, [CC BY-SA 4.0][3])
You can even do inline plots using [Matplotlib][16]:
![Graphing in Jupyter Notebook][17]
(Ben Nuttall, [CC BY-SA 4.0][3])
Finally, you can save your notebooks and include them in Git repositories, and if you push to GitHub, they will render as completed notebooks—outputs, graphs, and all (as in [this example][18]):
![Saving Notebook to GitHub][19]
(Ben Nuttall, [CC BY-SA 4.0][3])
* * *
_This article originally appeared on Ben Nuttall's [Tooling Tuesday blog][20] and is reused with permission._
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/3/ipython-shell-jupyter-notebooks
作者:[Ben Nuttall][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/bennuttall
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_space_graphic_cosmic.png?itok=wu493YbB (Computer laptop in space)
[2]: https://opensource.com/sites/default/files/uploads/jupyterpreview.png (Jupyter)
[3]: https://creativecommons.org/licenses/by-sa/4.0/
[4]: https://opensource.com/sites/default/files/uploads/ipython-loop.png (iPython)
[5]: https://opensource.com/sites/default/files/uploads/ipython-suggest.png (iPython offers suggestions)
[6]: https://opensource.com/sites/default/files/uploads/ipython-pprint.png (iPython pretty prints)
[7]: https://opensource.com/sites/default/files/uploads/ipython-ls.png (IPython shell commands)
[8]: https://opensource.com/sites/default/files/uploads/ipython-help.png (IPython help)
[9]: https://opensource.com/article/21/2/python-virtualenvwrapper
[10]: https://opensource.com/sites/default/files/uploads/jupyter-notebook-1.png (Jupyter Notebook)
[11]: https://opensource.com/sites/default/files/uploads/jupyter-python-notebook.png (Python 3 in Jupyter Notebook)
[12]: https://opensource.com/sites/default/files/uploads/jupyter-loop.png (Executing commands in Jupyter)
[13]: https://opensource.com/sites/default/files/uploads/jupyter-cells.png (Jupyter output)
[14]: https://opensource.com/sites/default/files/uploads/jupyter-cells-2.png (Jupyter output)
[15]: https://opensource.com/sites/default/files/uploads/jupyter-help.png (Jupyter supports IPython features)
[16]: https://matplotlib.org/
[17]: https://opensource.com/sites/default/files/uploads/jupyter-graph.png (Graphing in Jupyter Notebook)
[18]: https://github.com/piwheels/stats/blob/master/2020.ipynb
[19]: https://opensource.com/sites/default/files/uploads/savenotebooks.png (Saving Notebook to GitHub)
[20]: https://tooling.bennuttall.com/the-ipython-shell-and-jupyter-notebooks/

View File

@ -0,0 +1,191 @@
[#]: subject: (Why I love using the IPython shell and Jupyter notebooks)
[#]: via: (https://opensource.com/article/21/3/ipython-shell-jupyter-notebooks)
[#]: author: (Ben Nuttall https://opensource.com/users/bennuttall)
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
为什么我喜欢使用 IPython shell 和 Jupyter Notebook
======
Jupyter Notebook 将IPython shell 提升到一个新的高度。
![Computer laptop in space][1]
Jupyter 项目最初是以 IPython 和 IPython Notebook 的形式出现的。它最初是一个专门针对 Python 的交互式 shell 和笔记本环境,后来扩展为不分语言的环境,支持 Julia、Python 和 R,以及其他任何语言。
![Jupyter][2]
Ben Nuttall, [CC BY-SA 4.0][3]
IPython 是一个 Python shell类似于你在命令行输入 `python` 或者 `python3` 时看到的,但它更聪明,更有用。如果你曾经在 Python shell 中输入过多行命令,并且想重复它,你就会理解每次都要一行一行地滚动浏览历史记录的挫败感。有了 IPython你可以一次滚动浏览整个块同时还可以逐行浏览和编辑这些块的部分内容。
![iPython][4]
Ben Nuttall, [CC BY-SA 4.0][3]
它具有自动补全,并提供上下文感知的建议:
![iPython offers suggestions][5]
Ben Nuttall, [CC BY-SA 4.0][3]
它默认漂亮输出:
![iPython pretty prints][6]
Ben Nuttall, [CC BY-SA 4.0][3]
它甚至允许你运行 shell 命令:
![IPython shell commands][7]
Ben Nuttall, [CC BY-SA 4.0][3]
它还提供了一些有用的功能,比如将 `?` 添加到对象中,作为运行 `help()` 的快捷方式,而不会破坏你的流程:
![IPython help][8]
Ben Nuttall, [CC BY-SA 4.0][3]
如果你使用的是虚拟环境(参见我关于 [virtualenvwrapper][9] 的帖子),在环境中用 pip 安装:
```
`pip install ipython`
```
要在全系统范围内安装,你可以在 Debian、Ubuntu 或树莓派上使用apt
```
`sudo apt install ipython3`
```
或使用 pip
```
`sudo pip3 install ipython`
```
### Jupyter Notebook
Jupyter Notebook 将 IPython shell 提升到了一个新的高度。首先,它们是基于浏览器的,而不是基于终端的。要开始使用,请安装 `jupyter`
如果你使用的是虚拟环境,请在环境中使用 pip 进行安装:
```
`pip install jupyter`
```
要在全系统范围内安装,你可以在 Debian、Ubuntu 或树莓派上使用 apt
```
`sudo apt install jupyter-notebook`
```
或使用 pip
```
`sudo pip3 install jupyter`
```
启动 Notebook
```
`jupyter notebook`
```
这将在你的浏览器中打开:
![Jupyter Notebook][10]
Ben Nuttall, [CC BY-SA 4.0][3]
你可以使用 **New** 下拉菜单创建一个新的 Python 3 Notebook
![Python 3 in Jupyter Notebook][11]
Ben Nuttall, [CC BY-SA 4.0][3]
现在你可以在 `In[ ]` 字段中编写和执行命令。使用**回车**在代码块中换行,使用 **Shift+回车**来执行:
![Executing commands in Jupyter][12]
Ben Nuttall, [CC BY-SA 4.0][3]
你可以编辑和重新运行代码块,你可以重新排序、删除,复制/粘贴,等等。你可以以任何顺序运行代码块,但是要注意的是,任何创建的变量的作用域都将根据执行的时间而不是它们在 Notebook 中出现的顺序。你可以在 **Kernel** 菜单中重启并清除输出或重启并运行所有的代码块。
使用 `print` 函数每次都会输出。但是如果你有一条没有分配的语句,或者最后一条语句没有分配,那么它总是会输出:
![Jupyter output][13]
Ben Nuttall, [CC BY-SA 4.0][3]
你甚至可以把 `In ``Out` 作为可索引对象:
![Jupyter output][14]
Ben Nuttall, [CC BY-SA 4.0][3]
All the IPython features are available and are often presented a little nicer, too:
所有的 IPython 功能都可以使用,而且通常也会表现得更漂亮一些:
![Jupyter supports IPython features][15]
Ben Nuttall, [CC BY-SA 4.0][3]
你甚至可以使用 [Matplotlib][16] 进行内联绘图:
![Graphing in Jupyter Notebook][17]
Ben Nuttall, [CC BY-SA 4.0][3]
最后,你可以保存您的笔记本,并将其包含在 Git 仓库中,如果你将其推送到 GitHub它们将作为已完成的 Notebook 被渲染:输出、图形和所有一切(如 [本例][18]
![Saving Notebook to GitHub][19]
Ben Nuttall, [CC BY-SA 4.0][3]
* * *
_本文原载于 Ben Nuttall 的 [Tooling Tuesday 博客][20]经许可后重用。_
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/3/ipython-shell-jupyter-notebooks
作者:[Ben Nuttall][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/bennuttall
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_space_graphic_cosmic.png?itok=wu493YbB (Computer laptop in space)
[2]: https://opensource.com/sites/default/files/uploads/jupyterpreview.png (Jupyter)
[3]: https://creativecommons.org/licenses/by-sa/4.0/
[4]: https://opensource.com/sites/default/files/uploads/ipython-loop.png (iPython)
[5]: https://opensource.com/sites/default/files/uploads/ipython-suggest.png (iPython offers suggestions)
[6]: https://opensource.com/sites/default/files/uploads/ipython-pprint.png (iPython pretty prints)
[7]: https://opensource.com/sites/default/files/uploads/ipython-ls.png (IPython shell commands)
[8]: https://opensource.com/sites/default/files/uploads/ipython-help.png (IPython help)
[9]: https://opensource.com/article/21/2/python-virtualenvwrapper
[10]: https://opensource.com/sites/default/files/uploads/jupyter-notebook-1.png (Jupyter Notebook)
[11]: https://opensource.com/sites/default/files/uploads/jupyter-python-notebook.png (Python 3 in Jupyter Notebook)
[12]: https://opensource.com/sites/default/files/uploads/jupyter-loop.png (Executing commands in Jupyter)
[13]: https://opensource.com/sites/default/files/uploads/jupyter-cells.png (Jupyter output)
[14]: https://opensource.com/sites/default/files/uploads/jupyter-cells-2.png (Jupyter output)
[15]: https://opensource.com/sites/default/files/uploads/jupyter-help.png (Jupyter supports IPython features)
[16]: https://matplotlib.org/
[17]: https://opensource.com/sites/default/files/uploads/jupyter-graph.png (Graphing in Jupyter Notebook)
[18]: https://github.com/piwheels/stats/blob/master/2020.ipynb
[19]: https://opensource.com/sites/default/files/uploads/savenotebooks.png (Saving Notebook to GitHub)
[20]: https://tooling.bennuttall.com/the-ipython-shell-and-jupyter-notebooks/