Jupyter Notebook, previously known as iPython Notebook, is a web based application for interactive computing. Kernels have been developed for many popular programming languages including Julia, Python and R which have been cleverly combined to form the new name Jupyter. These notebooks are a great way to record any data analysis work flow and are therefore great for reproducibility. Notebooks also allow you to keep all of your code in one place instead of having a directory full of random scripts and output. The integration of Markdown cells makes documentation clean and simple so notebooks are a great way to share results with colleagues. In this post I will be collecting helpful usage tips as I find them.

Make an Alias to Run the Notebook

Typing the command jupyter notebook quickly becomes tedious so this is a good candidate for an alias.

alias nb="jupyter notebook"

Using Shell Commands

In my opinion, the ability to use shell commands inside the notebook is the best part of using notebooks. Using these tools allows you to record and reproduce all of the analysis steps exactly.

You can run any shell command within the notebook by using the ! prefix.

In[1]: !ls
Out[1]: file1.txt file2.txt file3.txt

The output of the command can be stored as a list

In[2]: filenames = !ls
In[3]: filenames
Out[3]: ['file1.txt', 'file2.txt', 'file3.txt']

Magics

%load
This magic loads a Python file from a filepath or URL and replaces the contents of the cell with the contents of the file. %matplotlib inline
This magic places matplotlib plots inline instead of opening a new window. %%writefile
This magic writes the contents of a cell to a file. %pprint
This magic toggles pretty print on/off %reset
This magic resets the namespace by removing all names defined by the user, if called without arguments. %timeit
This magic times the execution of a Python statement or expression. The CPU and clock times are printed. %%html
Render the cell as a block of HTML %%latex
Render the cell as a block of latex

Adding Line Numbers

One of the most annoying things about the Jupyter Notebook is the lack of line numbers. This makes it very difficult to track down lines mentioned in error messages.

Typing CTRL-M L toggles line numbers in the focus cell.

To make line numbers default you must edit the ~/.ipython/profile_default/static/custom/custom.js file by adding these lines:

define([
    'base/js/namespace',
    'base/js/events'
    ],
    function(IPython, events) {
        events.on("app_initialized.NotebookApp",
            function () {
                IPython.Cell.options_default.cm_config.lineNumbers = true;
            }
        );
    }
);

From stackoverflow

Keyboard Shortcuts

Shift-Enter Run current cell and move to the cell below
Ctrl-Enter Run current cell and stay in that cell
Alt-Enter Run the current cell then create and move to a new cell below.