TranslateProject/sources/tech/20210329 Why I love using the IPython shell and Jupyter notebooks.md
2021-04-06 08:59:23 +08:00

6.7 KiB

Why I love using the IPython shell and Jupyter notebooks

Jupyter notebooks take the IPython shell to the next level. Computer laptop in space

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

(Ben Nuttall, CC BY-SA 4.0)

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

(Ben Nuttall, CC BY-SA 4.0)

It has autocompletion and provides context-aware suggestions:

iPython offers suggestions

(Ben Nuttall, CC BY-SA 4.0)

It pretty-prints by default:

iPython pretty prints

(Ben Nuttall, CC BY-SA 4.0)

It even allows you to run shell commands:

IPython shell commands

(Ben Nuttall, CC BY-SA 4.0)

It also provides helpful features like adding ? to an object as a shortcut for running help() without breaking your flow:

IPython help

(Ben Nuttall, CC BY-SA 4.0)

If you're using a virtual environment (see my post on virtualenvwrapper, 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

(Ben Nuttall, CC BY-SA 4.0)

You can create a new Python 3 notebook using the New dropdown:

Python 3 in Jupyter Notebook

(Ben Nuttall, CC BY-SA 4.0)

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

(Ben Nuttall, CC BY-SA 4.0)

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

(Ben Nuttall, CC BY-SA 4.0)

You can even refer to In and Out as indexable objects:

Jupyter output

(Ben Nuttall, CC BY-SA 4.0)

All the IPython features are available and are often presented a little nicer, too:

Jupyter supports IPython features

(Ben Nuttall, CC BY-SA 4.0)

You can even do inline plots using Matplotlib:

Graphing in Jupyter Notebook

(Ben Nuttall, CC BY-SA 4.0)

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):

Saving Notebook to GitHub

(Ben Nuttall, CC BY-SA 4.0)


This article originally appeared on Ben Nuttall's Tooling Tuesday blog and is reused with permission.


via: https://opensource.com/article/21/3/ipython-shell-jupyter-notebooks

作者:Ben Nuttall 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出