ipython notebook

NOTE: This is the notebook from 2013, used as reference for now.


In [30]:
%matplotlib inline

In [31]:
from numpy import sin,cos,pi,linspace
from pylab import plot

# Play with these number and ignore the rest
R = 10
r = .4
O = 12

t = linspace(-pi,pi,3000)
x = (R+r)*cos(t) - (r+O)*cos(((R+r)/r)*t)
y = (R+r)*sin(t) - (r+O)*sin(((R+r)/r)*t)
plot(x,y)


Out[31]:
[<matplotlib.lines.Line2D at 0x1071ccc90>]
  • IPython Notebook is an interactive web environment for running and annotating Python code.
  • IPython Notebook gives output as it is generated.
  • Quickly experiment with code and explore data.

Starting IPython Notebook

From the command line:

ipython notebook --pylab inline

  • Starts a light-weight web server locally on your machine.
  • This command will typically connect you to a web browser displaying the notebook dashboard.
  • pylab bundles some scientific computing libraries together.
  • inline enables embedded figures in the notebook.

Notebook Cells

  • An IPython Notebook is a series of "cells".
  • The active cell has an outline around it.
  • Navigate to different cells with the arrow keys.
  • A cell can either be code, markdown, or "raw".
  • Create cells from the IPython Notebook menu or better yet learn the keyboard shortcuts.
  • Shift-Enter : run cell
  • Ctrl-m b : insert cell below

Hello world


In [32]:
print("Hello world")


Hello world

Exercise

  • Create a new note book cell here.
  • Write a one line program to print your name.
  • Execute cell

Embedding Markdown

You can embed your notebook with markdown. Markdown is a lightweight web markup language can be inserted right into the notebook. This is useful for annotating your notebook code samples.

Nice Errors


In [43]:
1 + foo


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-43-130777adf3f0> in <module>()
----> 1 1 + foo

NameError: name 'foo' is not defined

Inline Documentation

Executing the cell will split the pane and open documentation.


In [34]:
open?

Tab Completion

Type tab after o and see what happens. You should see a pop-up drop-down menu of choices.


In [35]:
o

Plotting

Ipython Notebook has inline plotting capability.


In [36]:
# Lissajous curve
from numpy import sin,pi,linspace
from pylab import plot

delta = pi/2
t = linspace(-pi,pi,300)
x = sin(3 * t + delta)
y = sin(5 * t)
plot(x,y)


Out[36]:
[<matplotlib.lines.Line2D at 0x107adbf50>]

Interrupts

If IPython Notebook gets jammed up, you can restart it with the Kernel -> Interrupt and Kernel -> Restart menu items.

LaTeX

The Ipython Notebook uses mathjax to render LaTeX.

$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$$

yields:

$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$$

Run Shell Commands with !


In [37]:
!ls


Geocoded_METAR.ipynb          ncss.ipynb
LICENSE                       netcdf-by-coordinates.ipynb
Overview.pdf                  numpy.ipynb
Overview.pptx                 pydap.ipynb
README.old.md                 radar_level2.ipynb
README.org                    reading_netCDF.ipynb
conda_binstar.ipynb           scratch.ipynb
data                          test.png
git.ipynb                     test_wind.png
images                        todo-2014.org
intro_netCDF.ipynb            todo-2014.org~
introduction.ipynb            wakari.ipynb
ipython-notebook-server.ipynb wms_sample.ipynb
ipython-notebook.ipynb        writing_netCDF.ipynb
matplotlib.ipynb

Local or Remote Images and Even Videos


In [38]:
from IPython.core.display import Image 
Image(url='http://python.org/images/python-logo.gif')


Out[38]:

Take Python Code and Display it in LaTeX

  • SymPy is a Python library for symbolic mathematics

In [39]:
from sympy import *
init_printing()
x, y = symbols("x y")
Rational(3,2)*pi + exp(I*x) / (x**2 + y)


Out[39]:
$$\frac{3 \pi}{2} + \frac{e^{i x}}{x^{2} + y}$$

Load External Codes


In [40]:
loadpy http://matplotlib.org/mpl_examples/pylab_examples/simple_plot.py

In [41]:
from pylab import *

t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s, linewidth=1.0)

xlabel('time (s)')
ylabel('voltage (mV)')
title('About as simple as it gets, folks')
grid(True)
savefig("test.png")
show()


Sharing

  • Note an IPython notebook when saved as a .ipynb file is simply a JSON file that you can share with colleagues via email, dropbox, etc.
  • There is also a neat site called nbviewer that allows you to view and share notebooks.
  • Note you cannot run your code at nbviewer. It is simply a rendering of a notebook.

In [42]:
from IPython.display import HTML
HTML('<iframe src=http://nbviewer.ipython.org width=700 height=350></iframe>')


Out[42]:

Some interesting IPython Notebook possibilities

  • Typically runs on local host, but can theoretically run remotely or on the cloud (e.g. Amazon EC2) and accessed through your web browser.
  • Interact with IPython Notebook in the browser so available on any platform that has a web browser (e.g., tablets).
  • Perform "big data" analysis where your big data live (assuming you can start a notebook server where the data are located).