First go and reorganizing the examples.

This commit is contained in:
Brian Granger 2012-10-20 20:22:21 -07:00
parent 27d5b1b8cb
commit 7cca8cecfa
3 changed files with 814 additions and 0 deletions

View File

@ -0,0 +1,157 @@
{
"metadata": {
"name": "Frontend-Kernel Model"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"The Frontend/Kernel Model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The traditional IPython (`ipython`) consists of a single process that combines a terminal based UI with the process that runs the users code.\n",
"\n",
"While this traditional application still exists, the modern IPython consists of two processes:\n",
"\n",
"* Kernel: this is the process that runs the users code.\n",
"* Frontend: this is the process that provides the user interface where the user types code and sees results.\n",
"\n",
"IPython currently has 3 frontends:\n",
"\n",
"* Terminal Console (`ipython console`)\n",
"* Qt Console (`ipython qtconsole`)\n",
"* Notebook (`ipython notebook`)\n",
"\n",
"The Kernel and Frontend communicate over a ZeroMQ/JSON based messaging protocol, which allows multiple Frontends (even of different types) to communicate with a single Kernel. This opens the door for all sorts of interesting things, such as connecting a Console or Qt Console to a Notebook's Kernel. For example, you may want to connect a Qt console to your Notebook's Kernel and use it as a help\n",
"browser, calling `??` on objects in the Qt console (whose pager is more flexible than the\n",
"one in the notebook). \n",
"\n",
"This Notebook describes how you would connect another Frontend to a Kernel that is associated with a Notebook."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Manual connection"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To connect another Frontend to a Kernel manually, you first need to find out the connection information for the Kernel using the `%connect_info` magic:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%connect_info"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{\n",
" \"stdin_port\": 52858, \n",
" \"ip\": \"127.0.0.1\", \n",
" \"hb_port\": 52859, \n",
" \"key\": \"7efd45ca-d8a2-41b0-9cea-d9116d0fb883\", \n",
" \"shell_port\": 52856, \n",
" \"iopub_port\": 52857\n",
"}\n",
"\n",
"Paste the above JSON into a file, and connect with:\n",
" $> ipython <app> --existing <file>\n",
"or, if you are local, you can connect with just:\n",
" $> ipython <app> --existing kernel-b3bac7c1-8b2c-4536-8082-8d1df24f99ac.json \n",
"or even just:\n",
" $> ipython <app> --existing \n",
"if this is the most recent IPython session you have started.\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see that this magic displays everything you need to connect to this Notebook's Kernel."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Automatic connection using a new Qt Console"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also start a new Qt Console connected to your current Kernel by using the `%qtconsole` magic. This will detect the necessary connection\n",
"information and start the Qt Console for you automatically."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = 10"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%qtconsole"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"The kernel's `raw_input` and `%debug`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The one feature the notebook currently doesn't support as a Frontend is the ability to send data to the Kernel's standard input socket. That is, if the Kernel requires information to be typed interactively by calling the builtin `raw_input` function, the Notebook will be blocked. This happens for example if you run a script that queries interactively for parameters, and very importantly, is how the interactive IPython debugger that activates when you type `%debug` works.\n",
"\n",
"So, in order to be able to use `%debug` or anything else that requires `raw_input`, you can either use a Terminal Console or Qt Console connected to a Notebook's Kernel as described above.\n"
]
}
],
"metadata": {}
}
]
}

View File

@ -0,0 +1,439 @@
{
"metadata": {
"name": "Part 1 - Running Code"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Running Code in the IPython Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First and foremost, the IPython Notebook is an interactive environment for writing and running Python code."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Code cells allow you to enter and run Python code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run a code cell using `Shift-Enter` or pressing the \"Play\" button in the toolbar above:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = 10"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Managing the IPython Kernel"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Code is run in a separate process called the IPython Kernel. The Kernel can be interrupted or restarted. Try running the following cell and then hit the \"Stop\" button in the toolbar above."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import time\n",
"time.sleep(10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-16-d7b436e260d5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If the Kernel dies you will be prompted to restart it. Here we call the low-level system libc.time routine with the wrong argument via\n",
"ctypes to segfault the Python interpreter:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sys\n",
"from ctypes import CDLL\n",
"# This will crash a Linux or Mac system; equivalent calls can be made on Windows\n",
"dll = 'dylib' if sys.platform == 'darwin' else '.so.6'\n",
"libc = CDLL(\"libc.%s\" % dll) \n",
"libc.time(-1) # BOOM!!"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": "*"
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"All of the goodness of IPython works"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here are two system aliases:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pwd"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 4,
"text": [
"u'/Users/bgranger/Documents/Computation/IPython/code/ipython/examples/notebooks'"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ls"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"01_notebook_introduction.ipynb Octave Magic.ipynb\r\n",
"Animations Using clear_output.ipynb PyLab and Matplotlib.ipynb\r\n",
"Basic Output.ipynb R Magics.ipynb\r\n",
"Custom Display Logic.ipynb Running Code.ipynb\r\n",
"Cython Magics.ipynb Script Magics.ipynb\r\n",
"Data Publication API.ipynb SymPy Examples.ipynb\r\n",
"Display System.ipynb Trapezoid Rule.ipynb\r\n",
"JS Progress Bar.ipynb Typesetting Math Using MathJax.ipynb\r\n",
"Local Files.ipynb animation.m4v\r\n",
"Markdown Cells.ipynb python-logo.svg\r\n",
"Notebook Tour.ipynb\r\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Any command line program can be run using `!` with string interpolation from Python variables:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"message = 'The IPython notebook is great!'\n",
"# note: the echo command does not run on Windows, it's a unix command.\n",
"!echo $message"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tab completion works:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy\n",
"numpy.random."
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tab completion after `(` brings up a tooltip with the docstring:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"numpy.random.rand("
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Adding `?` opens the docstring in the pager below:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"magic?"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exceptions are formatted nicely:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 1\n",
"y = 4\n",
"z = y/(1-x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "integer division or modulo by zero",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-15-dc39888fd1d2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m: integer division or modulo by zero"
]
}
],
"prompt_number": 15
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Working with external code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are a number of ways of getting external code into code cells."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pasting code with `>>>` prompts works as expected:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
">>> the_world_is_flat = 1\n",
">>> if the_world_is_flat:\n",
"... print \"Be careful not to fall off!\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Be careful not to fall off!\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `%load` magic lets you load code from URLs or local files:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load?"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].\n",
"For more information, type 'help(pylab)'.\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#!/usr/bin/env python\n",
"\n",
"# implement the example graphs/integral from pyx\n",
"from pylab import *\n",
"from matplotlib.patches import Polygon\n",
"\n",
"def func(x):\n",
" return (x-3)*(x-5)*(x-7)+85\n",
"\n",
"ax = subplot(111)\n",
"\n",
"a, b = 2, 9 # integral area\n",
"x = arange(0, 10, 0.01)\n",
"y = func(x)\n",
"plot(x, y, linewidth=1)\n",
"\n",
"# make the shaded region\n",
"ix = arange(a, b, 0.01)\n",
"iy = func(ix)\n",
"verts = [(a,0)] + zip(ix,iy) + [(b,0)]\n",
"poly = Polygon(verts, facecolor='0.8', edgecolor='k')\n",
"ax.add_patch(poly)\n",
"\n",
"text(0.5 * (a + b), 30,\n",
" r\"$\\int_a^b f(x)\\mathrm{d}x$\", horizontalalignment='center',\n",
" fontsize=20)\n",
"\n",
"axis([0,10, 0, 180])\n",
"figtext(0.9, 0.05, 'x')\n",
"figtext(0.1, 0.9, 'y')\n",
"ax.set_xticks((a,b))\n",
"ax.set_xticklabels(('a','b'))\n",
"ax.set_yticks([])\n",
"show()\n"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

View File

@ -0,0 +1,218 @@
{
"metadata": {
"name": "Part 4 - Markdown Cells"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Markdown Cells"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Text can be added to IPython Notebooks using Markdown cells. Markdown is a popular markup language that is a superset of HTML. Its specification can be found here:\n",
"\n",
"<http://daringfireball.net/projects/markdown/>"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Markdown basics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can make text *italic* or **bold**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can build nested itemized or enumerated lists:\n",
"\n",
"* One\n",
" - Sublist\n",
" - This\n",
" - Sublist\n",
" - That\n",
" - The other thing\n",
"* Two\n",
" - Sublist\n",
"* Three\n",
" - Sublist\n",
"\n",
"Now another list:\n",
"\n",
"1. Here we go\n",
" 1. Sublist\n",
" 2. Sublist\n",
"2. There we go\n",
"3. Now this"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can add horizontal rules:\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a blockquote:\n",
"\n",
"> Beautiful is better than ugly.\n",
"> Explicit is better than implicit.\n",
"> Simple is better than complex.\n",
"> Complex is better than complicated.\n",
"> Flat is better than nested.\n",
"> Sparse is better than dense.\n",
"> Readability counts.\n",
"> Special cases aren't special enough to break the rules.\n",
"> Although practicality beats purity.\n",
"> Errors should never pass silently.\n",
"> Unless explicitly silenced.\n",
"> In the face of ambiguity, refuse the temptation to guess.\n",
"> There should be one-- and preferably only one --obvious way to do it.\n",
"> Although that way may not be obvious at first unless you're Dutch.\n",
"> Now is better than never.\n",
"> Although never is often better than *right* now.\n",
"> If the implementation is hard to explain, it's a bad idea.\n",
"> If the implementation is easy to explain, it may be a good idea.\n",
"> Namespaces are one honking great idea -- let's do more of those!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And shorthand for links:\n",
"\n",
"[IPython's website](http://ipython.org)"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Headings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want, you can add headings using Markdown's syntax:\n",
"\n",
"# Heading 1\n",
"# Heading 2\n",
"## Heading 2.1\n",
"## Heading 2.2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**BUT most of the time you should use the Notebook's Heading Cells to organize your Notebook content**, as they provide meaningful structure that can be interpreted by other tools, not just large bold fonts."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Embedded code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can embed code meant for illustration instead of execution in Python:\n",
"\n",
" def f(x):\n",
" \"\"\"a docstring\"\"\"\n",
" return x**2\n",
"\n",
"or other languages:\n",
"\n",
" if (i=0; i<n; i++) {\n",
" printf(\"hello %d\\n\", i);\n",
" x += 4;\n",
" }"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"LaTeX Equations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Courtesy of MathJax, you can include mathematical expressions both inline: \n",
"$e^{i\\pi} + 1 = 0$ and displayed:\n",
"\n",
"$$e^x=\\sum_{i=0}^\\infty \\frac{1}{i!}x^i$$"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"General HTML"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because Markdown is a superset of HTML you can even add things like HTML tables:\n",
"\n",
"<table>\n",
"<tr>\n",
"<th>Header 1</th>\n",
"<th>Header 2</th>\n",
"</tr>\n",
"<tr>\n",
"<td>row 1, cell 1</td>\n",
"<td>row 1, cell 2</td>\n",
"</tr>\n",
"<tr>\n",
"<td>row 2, cell 1</td>\n",
"<td>row 2, cell 2</td>\n",
"</tr>\n",
"</table>"
]
}
],
"metadata": {}
}
]
}