The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain
Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.
Pros:
R
). This makes Jupyter notebooks particularly friendly when you're first learning PythonAtom
+ Hydrogen
and Spyder
equally useful.Cons:
.ipynb
is really a JSON
file¶At it's core, an Jupyter notebook is a JSON (JavaScript Object Notation) file. Let's see what the notebook that we are currently using looks like:
There are two primary methods for initializing a notebook.
.ipynb
notebook. cd /Users/me/Desktop/
jupyter notebook
Quit
and Logout
buttons on the page. Quit
== close the local server (i.e. the web application connection)Logout
== shut down the home page of the web application (but keep the server running)Control-C
in the console. Via the Anaconda Navigator (requires you installing an Anaconda distribution)
A kernel is a computational engine that executes the code contained in a notebook document. A cell (or "Chunk") is a container for text to be displayed in the notebook or code to be executed by the notebook's kernel.
Though we can only have one type of kernel running for any given notebook (we can't change between kernels in the middle of a notebook), we can use jupyter beyond just a python kernel. Here is a list of all the kernels that you can use with a jupyter notebook. For example, we can easily employ an R kernel in a jupyter notebook. This was always the notebooks original intent. Actually, "Jupyter" is a loose acronym meaning Julia, Python and R
Code chunks are what we use to execute Python (or whatever kernel we have running) code. In addition, we can write prose in a code chunk by altering the metadata regarding how the code should be run.
There are two states of a code chunk:
Esc
or using the mouse to click outside a cell's editor area.We can switch between Markdown and Code chunks either
y
when on the cell in Command Mode to switch to a code chunk.m
when on the cell in Command Mode to switch to a markdown chunkA code chunk will always reflect the behavior of the kernel that you're using (e.g. a Python code chunk will follow Python coding Syntax).
Best Practices
The Markdown chunks will use the Markdown and will allow for writing mathematical equations using LaTex.
As with most user interfaces, Jupyter Notebooks have developed their own way of doing things. Thus there are a number of useful shortcuts that you can employ to help perform useful tasks.
We can access a full (searchable) list of keyboard shortcuts by pressing p
when in Command Mode, or by clicking the keyboard icon in the tools.
Important ones while in Command Mode:
a
: create a new code chunk above the current one.b
: create a new code chunk below the current one.ii
: interrupt the kernel (really useful when some code is running too long or you've accidentally initiated an infinite loop!y
: code modem
: markdown modeshift
+ m
: merge cells (when more than one cell is highlighted)Important ones while in Edit Mode:
shit
+ ctrl
+ minus
: split cellMagic commands, and are prefixed by the %
character. These magic commands are designed to succinctly solve various common problems in standard data analysis.
Magic commands come in two flavors:
%
prefix and operate on a single line of input, %%
prefix and operate on multiple lines of input. List off all the available magic commands.
%lsmagic
Or consult the quick reference sheet of all available magic
%quickref
Here are some useful magic commands that come in handy as you're working with code.
"Come back here later"
%bookmark Home
See below
%cd ~/Desktop
%pwd
Using the bookmark to return to where we were...
%cd -b Home
%pwd
Extremely useful when we develop some functionality that we'd like to utilize later on.
%%writefile my_fib_func.py
def fib(n):
'''Fibonacci Sequence'''
x = [0]*n
for i in range(n):
if i == 0:
x[i] = 0
elif i == 1:
x[i] = 1
else:
x[i] = x[i-2] + x[i-1]
return x
%ls # list files ( see our function)
# %load my_fib_func.py
def fib(n):
'''Fibonacci Sequence'''
x = [0]*n
for i in range(n):
if i == 0:
x[i] = 0
elif i == 1:
x[i] = 1
else:
x[i] = x[i-2] + x[i-1]
return x
%run my_fib_func.py
How fast does what we wrote run?
%time fib(10)
How long does many runs take (statistical sample)?
%%timeit
fib(10)
main_dat = [1,2,3,4]
main_key = ["a","b"]
x = 5
y = 6
%psearch main*
Whenever you encounter an error or exception, just open a new notebook cell, type %debug
and run the cell. This will open a command line where you can test your code and inspect all variables right up to the line that threw the error. Type n
and hit Enter to run the next line of code (The ->
arrow shows you the current position). Use c
to continue until the next breakpoint. q
quits the debugger and code execution.
%%timeit?
We can expand the functionality of Jupyter notebooks through extensions. Extensions allow for use to create and use new features that better customize the notebook's user experience. For example, there are extensions for spell check, a table of contents to ease navigation, run code in parallel, and for viewing differences in notebooks when using Version control.
Download python module to install notebook extensions: https://github.com/ipython-contrib/jupyter_contrib_nbextensions
Using PyPi
(module manager):
pip install jupyter_nbextensions_configurator jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextensions_configurator enable --user
Using Conda
(Anaconda module manager):
conda install -c conda-forge jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextensions_configurator enable --user
Extensions can be activated most easily on the home screen when you first activate your Jupyter notebook.
pip install nbdime