Part 1 - Running Code.ipynb
438 lines
| 11.4 KiB
| text/plain
|
TextLexer
Fernando Perez
|
r5779 | { | |
"metadata": { | |||
Brian Granger
|
r9191 | "name": "Part 1 - Running Code" | |
Fernando Perez
|
r5779 | }, | |
Brian Granger
|
r6035 | "nbformat": 3, | |
MinRK
|
r7739 | "nbformat_minor": 0, | |
Fernando Perez
|
r5779 | "worksheets": [ | |
{ | |||
"cells": [ | |||
{ | |||
Brian Granger
|
r9191 | "cell_type": "heading", | |
"level": 1, | |||
"metadata": {}, | |||
"source": [ | |||
"Running Code in the IPython Notebook" | |||
] | |||
}, | |||
{ | |||
Fernando Perez
|
r5779 | "cell_type": "markdown", | |
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "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:" | |||
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
Brian Granger
|
r9191 | "collapsed": false, | |
Fernando Perez
|
r5779 | "input": [ | |
Brian Granger
|
r9191 | "a = 10" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "outputs": [], | |
Brian Granger
|
r9191 | "prompt_number": 10 | |
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "code", | |||
Brian Granger
|
r9191 | "collapsed": false, | |
Fernando Perez
|
r5779 | "input": [ | |
Thomas Kluyver
|
r9198 | "print(a)" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "outputs": [ | |
{ | |||
"output_type": "stream", | |||
"stream": "stdout", | |||
"text": [ | |||
"10\n" | |||
] | |||
} | |||
], | |||
"prompt_number": 11 | |||
Fernando Perez
|
r5779 | }, | |
{ | |||
Brian Granger
|
r9191 | "cell_type": "heading", | |
"level": 2, | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Managing the IPython Kernel" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "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." | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
Brian Granger
|
r9191 | "collapsed": false, | |
Fernando Perez
|
r5779 | "input": [ | |
Brian Granger
|
r9191 | "import time\n", | |
"time.sleep(10)" | |||
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "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 | |||
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "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:" | |||
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "import sys\n", | |
"from ctypes import CDLL\n", | |||
"# This will crash a Linux or Mac system; equivalent calls can be made on Windows\n", | |||
Thomas Kluyver
|
r9198 | "dll = 'dylib' if sys.platform == 'darwin' else 'so.6'\n", | |
Brian Granger
|
r9191 | "libc = CDLL(\"libc.%s\" % dll) \n", | |
"libc.time(-1) # BOOM!!" | |||
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "outputs": [], | |
"prompt_number": "*" | |||
}, | |||
{ | |||
"cell_type": "heading", | |||
"level": 2, | |||
"metadata": {}, | |||
"source": [ | |||
"All of the goodness of IPython works" | |||
] | |||
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Here are two system aliases:" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "pwd" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "outputs": [ | |
{ | |||
"output_type": "pyout", | |||
Brian Granger
|
r9191 | "prompt_number": 4, | |
Fernando Perez
|
r5779 | "text": [ | |
Brian Granger
|
r9191 | "u'/Users/bgranger/Documents/Computation/IPython/code/ipython/examples/notebooks'" | |
Fernando Perez
|
r5779 | ] | |
} | |||
], | |||
Brian Granger
|
r9191 | "prompt_number": 4 | |
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "ls" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "outputs": [ | |
{ | |||
Brian Granger
|
r9191 | "output_type": "stream", | |
"stream": "stdout", | |||
Fernando Perez
|
r5779 | "text": [ | |
Brian Granger
|
r9191 | "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" | |||
Fernando Perez
|
r5779 | ] | |
} | |||
], | |||
Brian Granger
|
r9191 | "prompt_number": 2 | |
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Any command line program can be run using `!` with string interpolation from Python variables:" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
Brian Granger
|
r9191 | "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": [] | |||
}, | |||
{ | |||
Fernando Perez
|
r5779 | "cell_type": "markdown", | |
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Tab completion works:" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
Brian Granger
|
r9191 | "collapsed": false, | |
Fernando Perez
|
r5779 | "input": [ | |
Brian Granger
|
r9191 | "import numpy\n", | |
"numpy.random." | |||
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "outputs": [], | |
Brian Granger
|
r9191 | "prompt_number": 9 | |
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Tab completion after `(` brings up a tooltip with the docstring:" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "numpy.random.rand(" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "outputs": [] | |
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Adding `?` opens the docstring in the pager below:" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "magic?" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "outputs": [], | |
"prompt_number": 8 | |||
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Exceptions are formatted nicely:" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "x = 1\n", | |
"y = 4\n", | |||
"z = y/(1-x)" | |||
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "outputs": [ | |
{ | |||
Brian Granger
|
r9191 | "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" | |||
Fernando Perez
|
r5779 | ] | |
} | |||
], | |||
Brian Granger
|
r9191 | "prompt_number": 15 | |
Fernando Perez
|
r5779 | }, | |
{ | |||
Brian Granger
|
r9191 | "cell_type": "heading", | |
"level": 2, | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Working with external code" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
Brian Granger
|
r9191 | "cell_type": "markdown", | |
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "source": [ | |
"There are a number of ways of getting external code into code cells." | |||
] | |||
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Pasting code with `>>>` prompts works as expected:" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | ">>> the_world_is_flat = 1\n", | |
">>> if the_world_is_flat:\n", | |||
Thomas Kluyver
|
r9198 | "... print(\"Be careful not to fall off!\")" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "outputs": [ | |
{ | |||
Brian Granger
|
r9191 | "output_type": "stream", | |
"stream": "stdout", | |||
Fernando Perez
|
r5779 | "text": [ | |
Brian Granger
|
r9191 | "Be careful not to fall off!\n" | |
Fernando Perez
|
r5779 | ] | |
} | |||
], | |||
Brian Granger
|
r9191 | "prompt_number": 1 | |
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "The `%load` magic lets you load code from URLs or local files:" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "%load?" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "outputs": [], | |
"prompt_number": 14 | |||
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "%pylab inline" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "outputs": [ | |
{ | |||
Brian Granger
|
r9191 | "output_type": "stream", | |
"stream": "stdout", | |||
Fernando Perez
|
r5779 | "text": [ | |
MinRK
|
r7739 | "\n", | |
Martin Spacek
|
r9455 | "Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline].\n", | |
Brian Granger
|
r9191 | "For more information, type 'help(pylab)'.\n" | |
Fernando Perez
|
r5779 | ] | |
} | |||
], | |||
Brian Granger
|
r9191 | "prompt_number": 2 | |
Fernando Perez
|
r5779 | }, | |
{ | |||
Brian Granger
|
r9191 | "cell_type": "code", | |
"collapsed": false, | |||
"input": [ | |||
"%load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py" | |||
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "outputs": [], | |
"prompt_number": 3 | |||
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "#!/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" | |||
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "outputs": [] | |
Fernando Perez
|
r5779 | } | |
MinRK
|
r7739 | ], | |
"metadata": {} | |||
Fernando Perez
|
r5779 | } | |
] | |||
} |