{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Running Code in the IPython Notebook" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Based on [this notebook](http://nbviewer.ipython.org/url/github.com/ipython/ipython/raw/master/examples/notebooks/Part%201%20-%20Running%20Code.ipynb) with very minor changes.\n", "\n", "---------\n", "\n", "First and foremost, the IPython Notebook is an interactive environment for writing and running Python code." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Code cells allow you to enter and run Python code" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "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": { "slideshow": { "slide_type": "-" } }, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "print(a)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] } ], "prompt_number": 3 }, { "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\u001b[0m in \u001b[0;36m\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": 4 }, { "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": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "u'/Users/fangohr/hgdocs/teaching/python/notebook'" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "IPythonNotebookIntroduction.ipynb\r\n", "IPythonNotebookIntroduction.py\r\n", "Matplotlib.ipynb\r\n", "Matplotlib.py\r\n", "Testing_12_testing_plus_pytest.ipynb\r\n", "Untitled0.ipynb\r\n", "animation.mp4\r\n", "filename.png\r\n", "filename.svg\r\n", "inverse-function-through-rootfinding.ipynb\r\n", "lab1.aux\r\n", "lab1.html\r\n", "lab1.idx\r\n", "lab1.ipynb\r\n", "lab1.log\r\n", "lab1.out\r\n", "lab1.pdf\r\n", "lab1.pdf.orig\r\n", "lab1.py\r\n", "lab1.tex\r\n", "\u001b[1m\u001b[36mlab1_files\u001b[m\u001b[m/\r\n", "lab2.ipynb\r\n", "lab2.py\r\n", "lab2.tmp\r\n", "lab3.ipynb\r\n", "lab4.ipynb\r\n", "lab5.ipynb\r\n", "lab6.ipynb\r\n", "lab7.ipynb\r\n", "lab8.ipynb\r\n", "mydata.txt\r\n", "same-or-differents-object.ipynb\r\n", "speed-map-vs-for-loop.ipynb\r\n", "update-web.sh\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\n", "numpy.random." ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 2)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m numpy.random.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 4 }, { "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": 5 }, { "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\u001b[0m in \u001b[0;36m\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.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a file on the fly\n", "\n", "Using the ``%%file`` magic, we can directly create a file" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%file helloworld.py\n", "print(\"Hello World\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing helloworld.py\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "and can see the file on the file system:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!ls -l helloworld.py" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-rw-r--r-- 1 fangohr staff 20 20 Sep 13:25 helloworld.py\r\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "and execute the file using the python interpreter:\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!python helloworld.py" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World\r\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Type markdown for text and latex\n", "\n", "For example, if $x=\\alpha$ then $x^2 = \\alpha^2$. Furthermore \n", "$$ \\int\\limits_a^b f(x) \\mathrm{d} x = \\Gamma.$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Further reading\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://github.com/ipython/ipython/tree/master/examples/notebooks#a-collection-of-notebooks-for-using-ipython-effectively" ] } ], "metadata": {} } ] }