Jupyter Notebook

Jupyter Notebook is the evolution of IPython Notebook. It is an open source web application that allows you to create and share documents that contain live code, equations, visualizations, and markdown.

The original IPython Notebook was constrained to Python as the only language. Jupyter Notebook has evolved to allow many programming languages to be used including Python, R, Julia, Scala, and F#.

If you want to take a deeper look at Jupyter Notebook, head to http://jupyter.org/, where you will be presented with a page similar to the following:

Jupyter Notebook can be downloaded and used independently of Python. Anaconda installs it by default. To start a Jupyter Notebook, issue the following command at the command line or Terminal:

$ jupyter notebook

To demonstrate, let's look at how to run the example code that comes with the text. Download the code from the Packt website and and unzip the file to a directory of your choosing. In the directory, you will see the following contents similar to the following:

Now issue the jupyter notebook command. You should see something similar to the following:

A browser page will open displaying the Jupyter Notebook homepage, which is http://localhost:8888/tree. This will open a web browser window showing this page, which will be a directory listing similar to the following:

Clicking on a .ipynb link opens a notebook page. If you open the notebook for this chapter you will see content similar to the following:

The notebook that is displayed is HTML that was generated by Jupyter and IPython. It consists of a number of cells that can be one of four types: code, markdown, raw nbconvert, or heading. All of the examples in this book use either code or markdown cells.

Jupyter runs an IPython kernel for each notebook. Cells that contain Python code are executed within that kernel and the results added to the notebook as HTML.

Double-clicking on any of the cells will make the cell editable. When you're done editing the contents of a cell, press Shift+Enter, at which point Jupyter/IPython will evaluate the contents and display the results.

If you wan to learn more about the notebook format that underlies the pages, see https://ipython.org/ipython-doc/3/notebook/nbformat.html.

The toolbar at the top of a notebook gives you a number of abilities to manipulate the notebook. These include adding, removing, and moving cells up and down in the notebook. Also available are commands to run cells, rerun cells, and restart the underlying IPython kernel.

To create a new notebook, go to File | New Notebook | Python 3:

A new notebook page will be created in a new browser tab. Its name will be Untitled:

The notebook consists of a single code cell that is ready to have Python entered. Enter 1+1 in the cell and press Shift + Enter to execute.

The cell has been executed and the result shown as Out [1]:. Jupyter also opened a new cell for you to enter more code or markdown.

Jupyter Notebook automatically saves your changes every minute, but it's still a good thing to save manually every once and a while.