%% This file was auto-generated by IPython. %% Conversion from the original notebook file: %% \documentclass[11pt,english]{article} %% This is the automatic preamble used by IPython. Note that it does *not* %% include a documentclass declaration, that is added at runtime to the overall %% document. \usepackage{amsmath} \usepackage{amssymb} \usepackage{graphicx} \usepackage{ucs} \usepackage[utf8x]{inputenc} % needed for markdown enumerations to work \usepackage{enumerate} % Slightly bigger margins than the latex defaults \usepackage{geometry} \geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=2.5cm,rmargin=2.5cm} % Define a few colors for use in code, links and cell shading \usepackage{color} \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} \definecolor{darkorange}{rgb}{.71,0.21,0.01} \definecolor{darkgreen}{rgb}{.12,.54,.11} \definecolor{myteal}{rgb}{.26, .44, .56} \definecolor{gray}{gray}{0.45} \definecolor{lightgray}{gray}{.95} \definecolor{mediumgray}{gray}{.8} \definecolor{inputbackground}{rgb}{.95, .95, .85} \definecolor{outputbackground}{rgb}{.95, .95, .95} \definecolor{traceback}{rgb}{1, .95, .95} % Framed environments for code cells (inputs, outputs, errors, ...). The % various uses of \unskip (or not) at the end were fine-tuned by hand, so don't % randomly change them unless you're sure of the effect it will have. \usepackage{framed} % remove extraneous vertical space in boxes \setlength\fboxsep{0pt} % codecell is the whole input+output set of blocks that a Code cell can % generate. % TODO: unfortunately, it seems that using a framed codecell environment breaks % the ability of the frames inside of it to be broken across pages. This % causes at least the problem of having lots of empty space at the bottom of % pages as new frames are moved to the next page, and if a single frame is too % long to fit on a page, will completely stop latex from compiling the % document. So unless we figure out a solution to this, we'll have to instead % leave the codecell env. as empty. I'm keeping the original codecell % definition here (a thin vertical bar) for reference, in case we find a % solution to the page break issue. %% \newenvironment{codecell}{% %% \def\FrameCommand{\color{mediumgray} \vrule width 1pt \hspace{5pt}}% %% \MakeFramed{\vspace{-0.5em}}} %% {\unskip\endMakeFramed} % For now, make this a no-op... \newenvironment{codecell}{} \newenvironment{codeinput}{% \def\FrameCommand{\colorbox{inputbackground}}% \MakeFramed{\advance\hsize-\width \FrameRestore}} {\unskip\endMakeFramed} \newenvironment{codeoutput}{% \def\FrameCommand{\colorbox{outputbackground}}% \vspace{-1.4em} \MakeFramed{\advance\hsize-\width \FrameRestore}} {\unskip\medskip\endMakeFramed} \newenvironment{traceback}{% \def\FrameCommand{\colorbox{traceback}}% \MakeFramed{\advance\hsize-\width \FrameRestore}} {\endMakeFramed} % Use and configure listings package for nicely formatted code \usepackage{listingsutf8} \lstset{ language=python, inputencoding=utf8x, extendedchars=\true, aboveskip=\smallskipamount, belowskip=\smallskipamount, xleftmargin=2mm, breaklines=true, basicstyle=\small \ttfamily, showstringspaces=false, keywordstyle=\color{blue}\bfseries, commentstyle=\color{myteal}, stringstyle=\color{darkgreen}, identifierstyle=\color{darkorange}, columns=fullflexible, % tighter character kerning, like verb } % The hyperref package gives us a pdf with properly built % internal navigation ('pdf bookmarks' for the table of contents, % internal cross-reference links, web links for URLs, etc.) \usepackage{hyperref} \hypersetup{ breaklinks=true, % so long urls are correctly broken across lines colorlinks=true, urlcolor=blue, linkcolor=darkorange, citecolor=darkgreen, } % hardcode size of all verbatim environments to be a bit smaller \makeatletter \g@addto@macro\@verbatim\small\topsep=0.5em\partopsep=0pt \makeatother % Prevent overflowing lines due to urls and other hard-to-break entities. \sloppy \begin{document} \section{A brief tour of the IPython notebook} This document will give you a brief tour of the capabilities of the IPython notebook.\\You can view its contents by scrolling around, or execute each cell by typing \texttt{Shift-Enter}. After you conclude this brief high-level tour, you should read the accompanying notebook titled \texttt{01\_notebook\_introduction}, which takes a more step-by-step approach to the features of the system. The rest of the notebooks in this directory illustrate various other aspects and capabilities of the IPython notebook; some of them may require additional libraries to be executed. \textbf{NOTE:} This notebook \emph{must} be run from its own directory, so you must \texttt{cd} to this directory and then start the notebook, but do \emph{not} use the \texttt{-{}-notebook-dir} option to run it from another location. The first thing you need to know is that you are still controlling the same old IPython you're used to, so things like shell aliases and magic commands still work: \begin{codecell} \begin{codeinput} \begin{lstlisting} pwd \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{verbatim} u'/Users/minrk/dev/ip/mine/docs/examples/notebooks' \end{verbatim} \end{codeoutput} \end{codecell} \begin{codecell} \begin{codeinput} \begin{lstlisting} ls \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{verbatim} 00_notebook_tour.ipynb callbacks.ipynb python-logo.svg 01_notebook_introduction.ipynb cython_extension.ipynb rmagic_extension.ipynb Animations_and_Progress.ipynb display_protocol.ipynb sympy.ipynb Capturing Output.ipynb formatting.ipynb sympy_quantum_computing.ipynb Script Magics.ipynb octavemagic_extension.ipynb trapezoid_rule.ipynb animation.m4v progbar.ipynb \end{verbatim} \end{codeoutput} \end{codecell} \begin{codecell} \begin{codeinput} \begin{lstlisting} message = 'The IPython notebook is great!' # note: the echo command does not run on Windows, it's a unix command. !echo $message \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{verbatim} The IPython notebook is great! \end{verbatim} \end{codeoutput} \end{codecell} \section{Plots with matplotlib} IPython adds an `inline' matplotlib backend, which embeds any matplotlib figures into the notebook. \begin{codecell} \begin{codeinput} \begin{lstlisting} %pylab inline \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{verbatim} Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'. \end{verbatim} \end{codeoutput} \end{codecell} \begin{codecell} \begin{codeinput} \begin{lstlisting} x = linspace(0, 3*pi, 500) plot(x, sin(x**2)) title('A simple chirp'); \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{center} \includegraphics[width=0.7\textwidth, height=0.9\textheight, keepaspectratio]{_fig_07.png} \par \end{center} \end{codeoutput} \end{codecell} You can paste blocks of input with prompt markers, such as those from \href{http://docs.python.org/tutorial/interpreter.html\#interactive-mode}{the official Python tutorial} \begin{codecell} \begin{codeinput} \begin{lstlisting} >>> the_world_is_flat = 1 >>> if the_world_is_flat: ... print "Be careful not to fall off!" \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{verbatim} Be careful not to fall off! \end{verbatim} \end{codeoutput} \end{codecell} Errors are shown in informative ways: \begin{codecell} \begin{codeinput} \begin{lstlisting} %run non_existent_file \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{verbatim} ERROR: File `u'non_existent_file.py'` not found. \end{verbatim} \end{codeoutput} \end{codecell} \begin{codecell} \begin{codeinput} \begin{lstlisting} x = 1 y = 4 z = y/(1-x) \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{traceback} \begin{verbatim} --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) in () 1 x = 1 2 y = 4 ----> 3 z = y/(1-x) ZeroDivisionError: integer division or modulo by zero \end{verbatim} \end{traceback} \end{codeoutput} \end{codecell} When IPython needs to display additional information (such as providing details on an object via \texttt{x?} it will automatically invoke a pager at the bottom of the screen: \begin{codecell} \begin{codeinput} \begin{lstlisting} magic \end{lstlisting} \end{codeinput} \end{codecell} \subsection{Non-blocking output of kernel} If you execute the next cell, you will see the output arriving as it is generated, not all at the end. \begin{codecell} \begin{codeinput} \begin{lstlisting} import time, sys for i in range(8): print i, time.sleep(0.5) \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{verbatim} 0 1 2 3 4 5 6 7 \end{verbatim} \end{codeoutput} \end{codecell} \subsection{Clean crash and restart} We call the low-level system libc.time routine with the wrong argument via ctypes to segfault the Python interpreter: \begin{codecell} \begin{codeinput} \begin{lstlisting} import sys from ctypes import CDLL # This will crash a Linux or Mac system; equivalent calls can be made on Windows dll = 'dylib' if sys.platform == 'darwin' else '.so.6' libc = CDLL("libc.%s" % dll) libc.time(-1) # BOOM!! \end{lstlisting} \end{codeinput} \end{codecell} \subsection{Markdown cells can contain formatted text and code} You can \emph{italicize}, \textbf{boldface} \begin{itemize} \item build \item lists \end{itemize} and embed code meant for illustration instead of execution in Python: \begin{verbatim} def f(x): """a docstring""" return x**2 \end{verbatim} or other languages: \begin{verbatim} if (i=0; i \end{verbatim} \end{codeoutput} \end{codecell} SVG images are also supported out of the box (since modern browsers do a good job of rendering them): \begin{codecell} \begin{codeinput} \begin{lstlisting} from IPython.display import SVG SVG(filename='python-logo.svg') \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{center} \includegraphics[width=0.7\textwidth]{_fig_26.pdf} \par \end{center} \end{codeoutput} \end{codecell} \paragraph{Embedded vs Non-embedded Images} As of IPython 0.13, images are embedded by default for compatibility with QtConsole, and the ability to still be displayed offline. Let's look at the differences: \begin{codecell} \begin{codeinput} \begin{lstlisting} # by default Image data are embedded Embed = Image( 'http://scienceview.berkeley.edu/view/images/newview.jpg') # if kwarg `url` is given, the embedding is assumed to be false SoftLinked = Image(url='http://scienceview.berkeley.edu/view/images/newview.jpg') # In each case, embed can be specified explicitly with the `embed` kwarg # ForceEmbed = Image(url='http://scienceview.berkeley.edu/view/images/newview.jpg', embed=True) \end{lstlisting} \end{codeinput} \end{codecell} Today's image from a webcam at Berkeley, (at the time I created this notebook). This should also work in the Qtconsole. Drawback is that the saved notebook will be larger, but the image will still be present offline. \begin{codecell} \begin{codeinput} \begin{lstlisting} Embed \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{center} \includegraphics[width=0.7\textwidth, height=0.9\textheight, keepaspectratio]{_fig_31.jpeg} \par \end{center} \end{codeoutput} \end{codecell} Today's image from same webcam at Berkeley, (refreshed every minutes, if you reload the notebook), visible only with an active internet connexion, that should be different from the previous one. This will not work on Qtconsole. Notebook saved with this kind of image will be lighter and always reflect the current version of the source, but the image won't display offline. \begin{codecell} \begin{codeinput} \begin{lstlisting} SoftLinked \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{verbatim} \end{verbatim} \end{codeoutput} \end{codecell} Of course, if you re-run the all notebook, the two images will be the same again. \subsubsection{Video} And more exotic objects can also be displayed, as long as their representation supports the IPython display protocol. For example, videos hosted externally on YouTube are easy to load (and writing a similar wrapper for other hosted content is trivial): \begin{codecell} \begin{codeinput} \begin{lstlisting} from IPython.display import YouTubeVideo # a talk about IPython at Sage Days at U. Washington, Seattle. # Video credit: William Stein. YouTubeVideo('1j_HxD4iLn8') \end{lstlisting} \end{codeinput} \begin{codeoutput} \begin{verbatim} \end{verbatim} \end{codeoutput} \end{codecell} Using the nascent video capabilities of modern browsers, you may also be able to display local videos. At the moment this doesn't work very well in all browsers, so it may or may not work for you; we will continue testing this and looking for ways to make it more robust. The following cell loads a local file called \texttt{animation.m4v}, encodes the raw video as base64 for http transport, and uses the HTML5 video tag to load it. On Chrome 15 it works correctly, displaying a control bar at the bottom with a play/pause button and a location slider. \begin{codecell} \begin{codeinput} \begin{lstlisting} from IPython.display import HTML video = open("animation.m4v", "rb").read() video_encoded = video.encode("base64") video_tag = '