Ever tried.

Ever failed.

No matter.

Try again.

Fail again.

Fail better.


-Samuel Beckett


Welcome to the Jupyter Notebook!

First, a quick tour:

  • The Jupyter Notebook is an HTML-based wrapper around a Python (specificially IPython) kernel.
  • It works inside of your web browser much like a regular web application, though it's actually "hosted" on your computer.
  • It is comprised of a series of cells, which allows you to tackle problems in an iterative manner.



These cells can be used to display Markdown:

  • Bulleted Markdown element 1.
  • Bulleted Markdown element 1.


or HTML like this:

`
HTML table cell 1. HTML table cell 2.


or LaTeX like this:

$\LaTeX$ $\not=$ $\pi$ $\cdot$ $\left(\frac{1}{9} \right)^2$





In [ ]:
# Most importantly, these cells can contain runnable Python code.
for item in ['This', 'cell', 'was', 'just', 'executed.']:
    print(item)



Cells will default to 'Code', but can be changed back and forth via the 'Code' or 'Markdown' dropdown box in the toolbar up top.



Cell to tinker with.

  • Unordered list to mess with
  • Unordered list continued

Modes

Generally:

Jupyter is fairly intuitive, but you should be aware there are two modes through which you can interact with the notebook. These modes are the "Command Mode", and the "Edit Mode".

TL;DR:

  • Escape takes you to Command Mode, which is used for the notebook as a whole.
  • Return takes you to Edit Mode, which is used for editing individuals cells.
  • Shift + Enter or Control + Enter runs a cell.
  • Pressing the H key in Command Mode brings up the Help Screen.
  • Control + Shift + P key brings up command palette.
  • For this presentation, you really only need your mouse and Shift + Enter.

Command Mode:

Click ONCE on the "cell to tinker with" above or use your arrow keys to navigate to that cell. A vertical blue bar should appear to the left of the cell because you have now selected the cell. This bar indicates that you are in Command Mode.

Command Mode is used to run cells or interact with the notebook as a whole. While the cell is selected and in Command Mode, press B. This will insert a cell underneath it (you are not expected to memorize this ... know that pressing H in command mode brings up a Help Screen). You can get into Command Mode from Edit Mode by pressing Enter.

Edit Mode:

Click TWICE on the "cell to tinker with" above, or move or select the cell in Command Mode and press Enter. A vertical green bar should appear to the left of the cell. This bar indicates that you are in Edit Mode. You can now change the contents of this cell, and when you have completed your edits press Shift + Enter to input them and switch back to Command Mode. Try changing the "cell to tinker with" cell and changing its contents. Feel free to change it back and forth from markdown to code. If you're feeling dangerous, you can try running a code cell with Shift + Enter.


Toolbar

Basics

  • The toolbar up top is much like the toolbars you've used with other applications.
  • If you click on the title area that says, "02_jupyter", you can change the name of this notebook.
  • If you press the disk icon, you can save any changes you've made to this notebook.
  • There are also buttons for cutting, copying, stopping/restarting the kernel if it crashes, the previously mentioned markdown or code box, and icons that allow you to go into "Presentation Mode", which is outside the scope of our converesation today.
  • You still have access to your browser for things like zoom, print, etc.
  • File: allows for saving, exporting in HTML/ipynb/etc, copying, the creation of new notebook, and associated file operations.
  • Edit: allows for cutting, pasting, find/replace, and cell splitting.
  • View: allows you to tweak your view, but should be used sparingly.
  • Insert: allows for the insertion of cells.
  • Cell: allows you to run an individual cell, run a series of cells, change a cell type, and clear/show cell output.
  • Kernel: allows you to interact with the iPython kernel (think of it as the machine that does all the calculations). You can stop, restart, or create new kernels.
  • Help: has a user interface tour, provides guidance on commands, keyboard shortcuts, markdown, and other information.

Basics

  • If you go to the Start Menu, and open the Anaconda3 folder, you will see a number of options.
  • Click the Jupyter Notebook. A new browser tab and kernel output kernel will appear. It will say "Home" at the top.
  • This is the Jupyter Home window, and it allows you to do things like stop kernels, find iPython notebooks, or create notebooks.
  • It defaults to your Documents folder.
  • For security reasons, the notebook cannot go to a higher parent directory than which it is started. If you want to start it in a different directory, you will have to do so by opening a Jupyter Notebook file in a different folder or use the command line.
  • Note: to use double-clicks to open Jupyter Notebook (ipynb) files, you can right click on a file, select "Open With", and browse for the location of the notebook executable (typically: C:\Anaconda3\Scripts\jupyter-notebook.exe).

So what?

  • ### It's a web-app-ish thing that's not even connected to the web. How does Jupyter benefit us?

1) Jupyter allows for iterative development; this pipeline approach is a great way to approach data discovery and analysis.


In [ ]:
# We can type in one cell, and press Shift + Enter to run it.
a = 'The quick brown fox jumped over the lazy brown dog.'

In [ ]:
# We can then transform the result from that cell and run it.
b = a.upper()

In [ ]:
# And use the result. Note: comments start with # and aren't executed.
b

In [ ]:
# And if we don't like the result, we can go back and do something else
c = a.title()

In [ ]:
# Which we can do with a single cell without compiling or processing.
c

While this is a trivial example, this pipeline approach allows for massive productivity increases when operating on larger data sets.

Side note: the layout of the cells doesn't really matter. It's all about the order in which they are run. You can see the order of the inputs and the outputs in the boxes to the left of the cells. Anything on the last line of a cell will be inspected/printed, which helps you track your progress as you go along.


2) The notebook is flexible and customizable. It can be used to gather information from any number of sources, process it, and export that information in a variety of formats. It can also be customized in any number of ways.


In [ ]:
from IPython.core.display import HTML
from IPython.core.display import display

# You can do useful things
HTML(
    """<script>
            var code_show=true;
            function code_toggle() {
                $('div.prompt').hide();
                if (code_show) {
                    $('div.input').show();
                } else {
                    $('div.input').hide();
                }
                code_show = !code_show;
            }
            $( document ).ready(code_toggle);
        </script>
        <a href="javascript:code_toggle()">[Toggle Code]</a>
    """)

In [ ]:
# You can do useful things ... or less than useful things.
def myspace_ify():
    '''Do not run this function.'''
    return HTML('<style type="text/css">'
                '    div#notebook {'
                '        background-image: url("./static/haters.gif");'
                '        background-repeat: repeat;'
                '    }'
                '</style>')

myspace_ify()

In [ ]:
def de_myspace_ify():
    return HTML('<style type="text/css">'
                '    div#notebook {'
                '        background-image: none;'
                '        background-repeat: none;'
                '    }'
                '</style>')

de_myspace_ify()

3) It has built in support for plotting and images, and ties in well with Matplotlib and IPython.



In [ ]:
# Plotting is generally a lot simplier than this.

# First we import pyplot under an alias for brevity's sake
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('fivethirtyeight')
%matplotlib inline

import numpy as np

t = np.arange(0.0, 2.0, 0.05)
s = 1 + np.sin(2*np.pi*t)
fig = plt.plot(t, s, '--')
plt.xlabel('Time')
plt.ylabel('WAT')
plt.title('Important Information')
fig[0].axes.set_ylim([-1, 3])
plt.show()

4) It is directly tied into Python and gives you access to the entire Python ecosystem. Which takes us to our next topic.



Next Up: Python