emacs-ipython-notebook/tests/notebook/nbformat3/Running Code.ipynb

467 lines
24 KiB
Text
Raw Normal View History

{
"metadata": {
"name": "",
"signature": "sha256:344aa301a0ce679ebf87e2d1dd206cf3784854cde5fedc34109f38710aef680c"
},
"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": [
"\n",
"<script type=\"text/javascript\">\n",
"var _toggle=false;\n",
"var hl = function (id, on){\n",
" $(id)[0].style.background = '';\n",
" if (on) {\n",
" $(id)[0].style.background = 'lightcyan';\n",
" }\n",
"};\n",
"</script>\n",
"\n",
"Run a code cell using `shift-enter` or pressing the <button><i class=\"icon-play\"></i></button> button in the <a href=\"#\" onMouseover=\"hl('#maintoolbar-container', 1)\" onMouseout=\"hl('#maintoolbar-container', 0)\">toolbar</a> above:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = 10"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(a)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n"
]
}
],
"prompt_number": 2
},
{
"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 <button><i class='icon-stop'></i></button> button in the <a href=\"#\" onMouseover=\"hl('#maintoolbar-container', 1)\" onMouseout=\"hl('#maintoolbar-container', 0)\">toolbar</a> above."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import time\n",
"time.sleep(10)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"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": []
},
{
"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": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"u'/Users/bgranger/Documents/Computing/IPython/code/ipython/examples/Notebook/Tutorials'"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ls"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Basic Output.ipynb Plotting with Matplotlib.ipynb data.csv\r\n",
"Custom Display Logic.ipynb Running Code.ipynb \u001b[34mimages\u001b[m\u001b[m/\r\n",
"Display System.ipynb Typesetting Math Using MathJax.ipynb\r\n",
"Markdown Cells.ipynb User Interface.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": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The IPython notebook is great!\r\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tab completion works:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"numpy.random."
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Shift-Tab on selection, or 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-6-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": 6
},
{
"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": 7
},
{
"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": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load http://matplotlib.org/mpl_examples/showcase/integral_demo.py"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"\"\"\n",
"Plot demonstrating the integral as the area under a curve.\n",
"\n",
"Although this is a simple example, it demonstrates some important tweaks:\n",
"\n",
" * A simple line plot with custom color and line width.\n",
" * A shaded region created using a Polygon patch.\n",
" * A text label with mathtext rendering.\n",
" * figtext calls to label the x- and y-axes.\n",
" * Use of axis spines to hide the top and right spines.\n",
" * Custom tick placement and labels.\n",
"\"\"\"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.patches import Polygon\n",
"\n",
"\n",
"def func(x):\n",
" return (x - 3) * (x - 5) * (x - 7) + 85\n",
"\n",
"\n",
"a, b = 2, 9 # integral limits\n",
"x = np.linspace(0, 10)\n",
"y = func(x)\n",
"\n",
"fig, ax = plt.subplots()\n",
"plt.plot(x, y, 'r', linewidth=2)\n",
"plt.ylim(ymin=0)\n",
"\n",
"# Make the shaded region\n",
"ix = np.linspace(a, b)\n",
"iy = func(ix)\n",
"verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)]\n",
"poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')\n",
"ax.add_patch(poly)\n",
"\n",
"plt.text(0.5 * (a + b), 30, r\"$\\int_a^b f(x)\\mathrm{d}x$\",\n",
" horizontalalignment='center', fontsize=20)\n",
"\n",
"plt.figtext(0.9, 0.05, '$x$')\n",
"plt.figtext(0.1, 0.9, '$y$')\n",
"\n",
"ax.spines['right'].set_visible(False)\n",
"ax.spines['top'].set_visible(False)\n",
"ax.xaxis.set_ticks_position('bottom')\n",
"\n",
"ax.set_xticks((a, b))\n",
"ax.set_xticklabels(('$a$', '$b$'))\n",
"ax.set_yticks([])\n",
"\n",
"plt.show()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAW8AAAEMCAYAAAALXDfgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl4FFW+xvFvp9NJCAphkdUECAgqLigG2UZgrsB4YQZQ\nAZVFQQRxlAFFUQR1BMVxRAV0QMFxAUXUgRkXBrioiCJIhLAjO5KwG7ORpde6f5SJooGQpLuru/N+\nnqefFElXnR+QvBxOnTrHZhiGgYiIhJUoqwsQEZHyU3iLiIQhhbeISBhSeIuIhCGFt4hIGFJ4i4iE\nIYW3iEgYUniLiIShMsN727ZtTJ06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zcXXvbnVVYU3DJiISOEVFeMeOxejalZiM\nDAouvpisZcsU3H6gnreIBMa33+K+7TYce/bgi4oi5777zA0U9MSkXyi8RcS/3G68f/0rtmnTcPh8\nFCQlUTBnDu42bayuLKIovEXEf9avx3nnncRu24Zhs5E9bBiFkyZBtWpWVxZxFN4iUnmHD+N56CGi\n33mHWKCoYUPyZ83C1bGj1ZVFLIW3iFRcQQG+v/8d45lniC4qwhsdzakRIyi8/36M886zurqIpvAW\nkfIzDIyFC/GMH4/j6FEAcrt3p+jJJ/E2aWJxcVWDwltEzp3PB0uX4nzsMWLT0nAA+S1bUjRtGq4O\nHayurkpReItI2dxuePddXFOnErN7N7GAs1YtCiZOpOiWW7TutgUU3iJyZvn5GPPm4Xn2WRxHjhAD\nOOvWpWj0aAqHDNG4toUU3lIx
"text": [
"<matplotlib.figure.Figure at 0x108604e50>"
]
}
],
"prompt_number": 11
}
],
"metadata": {}
}
]
}