{ "metadata": { "name": "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": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(a)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can run a cell in place using `Ctrl-Enter`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [] }, { "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": 3, "metadata": {}, "source": [ "Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a variable in a Notebook, then use the \"Kernel:Restart\" menu item to restart the Kernel and verify that the variable has been cleared." ] }, { "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": [] }, { "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": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Go to the Matplotlib gallery, view the raw Python code for an example, use `%load` to load it into a cell and then run it. Make sure you run:\n", "\n", " %pylab inline\n", "\n", "first to enable plotting support." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load soln/load.py" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Run all" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to run all cells in a Notebook by using the \"Cell\" menu its \"Run All\"." ] } ], "metadata": {} } ] }