Topics

We will cover:

  • Basic math
  • Exploring modules and getting help
  • Rich displaying
  • Inline plots
  • Interactive inline plots
  • Interactive elements

Basic math

Let's do some basic math to get everyone started:


In [ ]:
import math

print("The square root of 3 is:", math.sqrt(3))
print("π is:", math.pi)
print("The sin of 90 degrees is:", math.sin(math.radians(90)))

Exploring modules -- getting help

If you do not know what is available inside a module you can get help by hitting < tab >.


In [ ]:
math.

To get help with a particular function, type math.sin?


In [ ]:
math.erf?

In [ ]:
s = "Hello!"

In [ ]:
s.join() # or press <shift>+<tab> inside the brackets

A notebook can display (nearly) anything

Because the notebook runs in your browser it can display nearly anything. Basically anything your browser can display is allowed.


In [ ]:
from IPython.display import Image
Image('imgs/leonardo.jpg')

In [ ]:
# HTML
from IPython.display import HTML
HTML('<b>Hello world!</b>')

In [ ]:
# Artsy videos
from IPython.display import VimeoVideo
VimeoVideo("150594088")

In [ ]:
# LaTeX!
from IPython.display import Latex
Latex(r"""\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, 
\frac{\partial\vec{\mathbf{E}}}{\partial t} & = & \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = & 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, 
\frac{\partial\vec{\mathbf{B}}}{\partial t} & = & \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = & 0 
\end{eqnarray}""")

Custom objects

You can teach your own classes how to display themselves! Read the Rich display help.


In [ ]:
class Apple:
    def __init__(self, colour):
        self.colour = colour

    def _repr_html_(self):
        if self.colour == 'green':
            url = ("https://www.infusioneliquid.com/wp-content"
                  "/uploads/2013/12/Green-Apples.jpg")
        elif self.colour == 'red':
            url = ("http://boutonrougedesigns.com/wp-content"
                   "/uploads/red-apple.jpg")
            
        else:
            url = ("http://static1.squarespace.com/static"
                   "/51623c20e4b01df404d682ae/t/542d5d05e4b09d76c5f801d8"
                   "/1412259082640/Be+the+job+candidate+that+stands+out+"
                   "from+the+rest+and+the+one+recruitment+consultants+choose+jpeg")

        return "<img src=%s />"%url

In [ ]:
Apple("red")

Inline plots

You can display your plots right next to the code that made them.


In [ ]:
# Matplotlib
%config InlineBackend.figure_format='retina'
# gives you interactive plots
%matplotlib notebook
# rendered inline plots
#%matplotlib inline

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (6, 6)
plt.rcParams["figure.max_open_warning"] = -1

In [ ]:
# Taken from https://github.com/fperez/talk-1504-boulder/blob/master/QuickTour.ipynb
from scipy import special
import numpy as np

def plot_bessel(xmax=20, nmin=0, nmax=10, nstep=3):
    x = np.linspace(0, xmax, 200)
    f, ax = plt.subplots()
    for n in range(nmin, nmax+1, nstep):
        plt.plot(x, special.jn(n, x), label=r'$J_{%i(x)}$' % n)
    plt.grid()
    plt.legend()
    plt.title('Bessel Functions')

plot_bessel()

Interactive interactive plots (twice as interactive!)

IPython makes it super easy to build little webapps:


In [ ]:
from IPython.html.widgets import interact

interact(plot_bessel, xmax=(10,100.), nmin=(0, 10),
         nmax=(10, 30), nstep=(1,5));

In [ ]: