IPython Notebooks

What are IPython Notebooks?

  • IPython Notebooks are browser based files
  • They contain Python code and output from code
  • They can contain documentation written in Markdown amrkup as well as mathematical equations typeset in $\LaTeX{}$
  • They can contain rich media - images, video, other web pages

Cells

  • An IPython Notebook consists of cells
  • Cells can be of different types
    • Markdown - documentation with embedded $\LaTeX{}$ equations
    • Code - Python code
    • Slide - HTML presentation slides

$\LaTeX{}$ in Markdown

  • IPython Notebooks use MathJax Javascript library to render $\LaTeX$ equations
  • MathJax Javascript library is downloaded from the Internet once per session
  • If Internet connection is unavailable, $\LaTeX$ equations will not be rendered
  • It is possible to install a local copy of MathJax so that $\LaTeX$ equations can be rendered without an Internet connection

With your machine connected to the Internet, open IPython console and at the IPython prompt, type the following statements

from IPython.external.mathjax import install_mathjax
install_mathjax()

Wait for MathJax to be downloaded and installed. Now you have a local copy of MathJax and $\LaTeX$ equations will be rendered even when you are not connected to the Internet

Converting IPython Notebooks to Other Formats

IPython Notebooks are live, that is code can be changed and executed to see the change to the output. Notebooks can be converted to other formats, but the Notebook is now static, that is, code can no longer be changed and executed to see the changed output.

Following output formats are supported:

  • HTML
  • $\LaTeX$ if latex is installed
  • PDF if pdflatex is installed

Commands to convert Notebooks to other formats are:

  • Convert to HTML: ipython nbconvert notebook.ipynb
  • Convert to $\LaTeX$: ipython nbconvert notebook.ipynb --to latex
  • Convert to PDF: ipython nbconvert notebook.ipynb --to latex --post PDF
  • Convert to HTML slides and start slideshow: ipython notebook notebook.ipynb --to slides --post serve

Starting the Notebook Server

  • IPython Notebooks have the filename extension .ipynb
  • Choose a directory where you want to put your IPython Notebooks
  • Change over to the chosen directory and type the command at the command prompt

    ipython notebook

  • This will start up the IPython Notebook server, which is a web server listening usually on port 8888

  • Your default browser will be opened and will display the IPython Notebook dashboard
  • Dashboard lists all Notebook files, if any, in the directory from where the Notebook server is started
  • Dashboard will also show any directories that may be present in the Notebook directory

Working with Notebooks

  • You can create new Notebooks. New Notebooks are assigned a name starting with Untitledx.ipynb
  • You can delete existing Notebooks from the dashboard
  • You can rename an opened Notebook
  • You can shutdown a running Notebook from the dashboard

Quick Introduction to Markdown

Headings

  • Level 1 headings are created with the markup # Level 1 Heading
  • Two ## indicate a Level 2 heading, Three ### indicate a Level 3 heading and so on

Bold, Italic

  • Anything marked up as **Bold** will show up as Bold
  • Anything marked up as *Italic* shows up as Italic
  • monospaced is typeset by enclosing monospaced within two backquotes, namely `

$\LaTeX$ Equations

  • Inline equations use $ to demarcate a $\LaTeX$ equation. For example $f(x) = a^2 x + b x + x$ will be typeset as $f(x) = a^2 x + b x + x$
  • Displayed equations without numbers are enclosed within matching pair of $$ f(x) = \frac{\sqrt{a^2 + b^2}}{2 \sin \theta} $$
$$ f(x) = \frac{\sqrt{a^2 + b^2}}{2 \sin \theta} $$
  • You can use \begin{equation}
  • You can use \begin{align}

Source

\begin{align} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{align}

\begin{align} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{align}

Shutting Down Notebook Server

  • Save and close all Notebooks
  • Close the dashboard
  • Go to the MS DOS prompt or Terminal from where the Notebook Server was started and stop it by pressing Ctrl+C twice. If server does not shutdown, close the MS DOS prompt window

In [1]:
from IPython.display import Image
Image(filename='python-logo.png')


Out[1]:

In [ ]: