{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook was put together by [Jake Vanderplas](http://www.vanderplas.com) for UW's [Astro 599](http://www.astro.washington.edu/users/vanderplas/Astr599/) course. Source and license info is on [GitHub](https://github.com/jakevdp/2013_fall_ASTR599/)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# IPython: an enviromnent for interactive computing" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## What is IPython?\n", "\n", "- Short for *I*nteractive *Python*\n", "- A platform for you to *interact* with your code and data\n", "- The *notebook*: a system for *literate computing*\n", " * The combination of narrative, code and results\n", " * Weave your scientific narratives together with your computational process\n", "- Tools for easy parallel computing\n", " * Interact with *many* processes" ] }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "IPython at the terminal" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The basic IPython client: at the terminal, simply type `ipython`:\n", "\n", " $ ipython\n", " Python 2.7.4 (default, Apr 19 2013, 18:28:01) \n", " Type \"copyright\", \"credits\" or \"license\" for more information.\n", " \n", " IPython 1.0.0 -- An enhanced Interactive Python.\n", " ? -> Introduction and overview of IPython's features.\n", " %quickref -> Quick reference.\n", " help -> Python's own help system.\n", " object? -> Details about 'object', use 'object??' for extra details.\n", " \n", " In [1]: print \"hello world\"\n", " hello world\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# The IPython book\n", "\n", "
\n", "

Also introduces Numpy, Pandas and Matplotlib

\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Some other tutorial help/resources :\n", "\n", " - The [IPython website](http://ipython.org)\n", " - Search for \"IPython in depth\" tutorial on youtube and pyvideo, much longer, much deeper\n", " - Ask for help on [Stackoverflow, tag it \"ipython\"](http://stackoverflow.com/questions/tagged/ipython)\n", " - [Mailing list](http://mail.scipy.org/mailman/listinfo/ipython-dev)\n", " - File a [github issue](http://github.com/ipython/ipython)\n", " - [Twitter](https://twitter.com/IPythonDev)\n", " - [Reddit](http://www.reddit.com/r/IPython)\n", " - [Notebook Gallery](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks)\n", " - full books" ] }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "IPython: beyond plain Python" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "When executing code in IPython, all valid Python syntax works as-is, but IPython provides a number of features designed to make the interactive experience more fluid and efficient." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "First things first: running code, getting help" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "In the notebook, to run a cell of code, hit `Shift-Enter`. This executes the cell and puts the cursor in the next cell below, or makes a new one if you are at the end. Alternately, you can use:\n", " \n", "- `Alt-Enter` to force the creation of a new cell unconditionally (useful when inserting new content in the middle of an existing notebook).\n", "- `Control-Enter` executes the cell and keeps the cursor in the same cell, useful for quick experimentation of snippets that you don't need to keep permanently." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Hello\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Getting help" ] }, { "cell_type": "code", "collapsed": false, "input": [ "?" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Help with `?` and `??`\n", "\n", "Typing `object_name?` will print all sorts of details about any object, including docstrings, function definition lines (for call arguments) and constructor details for classes." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import collections\n", "collections.namedtuple?" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "collections.Counter??" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "*int*?" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "An IPython quick reference card:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%quickref" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Tab completion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tab completion, especially for attributes, is a convenient way to explore the structure of any object you\u2019re dealing with. Simply type `object_name.` to view the object\u2019s attributes. Besides Python objects and keywords, tab completion also works on file and directory names." ] }, { "cell_type": "code", "collapsed": false, "input": [ "collections." ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m collections.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The interactive workflow: input, output, history" ] }, { "cell_type": "code", "collapsed": false, "input": [ "2+10" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "12" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "_+10" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "22" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Output control\n", "\n", "You can suppress the storage and rendering of output if you append `;` to the last cell (this comes in handy when plotting with matplotlib, for example):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "10+20;" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "_" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "22" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Output history\n", "\n", "The output is stored in `_N` and `Out[N]` variables:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "_10 == Out[10]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "NameError", "evalue": "name '_10' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0m_10\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mOut\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;31mNameError\u001b[0m: name '_10' is not defined" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "Out" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "{8: 12, 9: 22, 11: 22}" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "And the last three have shorthands for convenience:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'last output:', _\n", "print 'next one :', __\n", "print 'and next :', ___" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "last output: 22\n", "next one : 22\n", "and next : 12\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The input history is also available" ] }, { "cell_type": "code", "collapsed": false, "input": [ "In[11]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "u'_'" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "_i" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "u'In[11]'" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "_ii" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "u'In[11]'" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "print 'last input:', _i\n", "print 'next one :', _ii\n", "print 'and next :', _iii" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "last input: _ii\n", "next one : _i\n", "and next : In[11]\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "%history" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "print \"Hello\"\n", "?\n", "import collections\n", "collections.namedtuple?\n", "collections.Counter??\n", "*int*?\n", "%quickref\n", "collections.\n", "2+10\n", "_+10\n", "10+20;\n", "_\n", "_10 == Out[10]\n", "Out\n", "print 'last output:', _\n", "print 'next one :', __\n", "print 'and next :', ___\n", "In[11]\n", "_i\n", "_ii\n", "print 'last input:', _i\n", "print 'next one :', _ii\n", "print 'and next :', _iii\n", "%history\n" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Accessing the underlying operating system\n", "\n", "**Note:** the commands below work on Linux or Macs, but may behave differently on Windows, as the underlying OS is different. IPython's ability to access the OS is still the same, it's just the syntax that varies per OS." ] }, { "cell_type": "code", "collapsed": false, "input": [ "!pwd" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "/Users/jakevdp/Opensource/2013_fall_ASTR599/notebooks\r\n" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "files = !ls\n", "print \"My current directory's files:\"\n", "print files" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "My current directory's files:\n", "['00_intro.ipynb', '01_basic_training.ipynb', '02_advanced_data_structures.ipynb', '03_IPython_intro.ipynb', '04_Functions_and_modules.ipynb', '05_NumpyIntro.ipynb', '05_Trapezoid_Solution.ipynb', '06_Denoise_Solution.ipynb', '06_MatplotlibIntro.ipynb', '07_GitIntro.ipynb', '08_ScipyIntro.ipynb', '09_AdvancedStrings.ipynb', '10_AdvancedPython2.ipynb', '11_EfficientNumpy.ipynb', '12_AdvancedMatplotlib.ipynb', 'README.txt', 'clean_data.dat', 'data', 'func_fortran.f', 'func_fortran.so', 'ggplot.rc', 'images', 'inout.dat', 'messy_data.dat', 'modfun.py', 'my_output.dat', 'myfile.html', 'myfile.py', 'myfile.pyc', 'mymodule.py', 'mymodule.pyc', 'mymodule2.py', 'mymodule2.pyc', 'number_game.py', 'test.npy', 'test.npz', 'test.out', 'tmp.py~']\n" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "!echo $files" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[00_intro.ipynb, 01_basic_training.ipynb, 02_advanced_data_structures.ipynb, 03_IPython_intro.ipynb, 04_Functions_and_modules.ipynb, 05_NumpyIntro.ipynb, 05_Trapezoid_Solution.ipynb, 06_Denoise_Solution.ipynb, 06_MatplotlibIntro.ipynb, 07_GitIntro.ipynb, 08_ScipyIntro.ipynb, 09_AdvancedStrings.ipynb, 10_AdvancedPython2.ipynb, 11_EfficientNumpy.ipynb, 12_AdvancedMatplotlib.ipynb, README.txt, clean_data.dat, data, func_fortran.f, func_fortran.so, ggplot.rc, images, inout.dat, messy_data.dat, modfun.py, my_output.dat, myfile.html, myfile.py, myfile.pyc, mymodule.py, mymodule.pyc, mymodule2.py, mymodule2.pyc, number_game.py, test.npy, test.npz, test.out, tmp.py~]\r\n" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "!echo {files[0].upper()}" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "00_INTRO.IPYNB\r\n" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Beyond Python: magic functions" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The IPyhton 'magic' functions are a set of commands, invoked by prepending one or two `%` signs to their name, that live in a namespace separate from your normal Python variables and provide a more command-like interface. They take flags with `--` and arguments without quotes, parentheses or commas. The motivation behind this system is two-fold:\n", " \n", "- To provide an orthogonal namespace for controlling IPython itself and exposing other system-oriented functionality.\n", "\n", "- To expose a calling mode that requires minimal verbosity and typing while working interactively. Thus the inspiration taken from the classic Unix shell style for commands." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%magic" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Line vs cell magics:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit range(10)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1000000 loops, best of 3: 375 ns per loop\n" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "%%timeit\n", "range(10)\n", "range(100)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1000000 loops, best of 3: 1.06 \u00b5s per loop\n" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Line magics can be used even inside code blocks:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(5):\n", " size = i*100\n", " print 'size:',size, \n", " %timeit range(size)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "size: 0 " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "10000000 loops, best of 3: 157 ns per loop\n", "size: 100 " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1000000 loops, best of 3: 822 ns per loop\n", "size: 200 " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1000000 loops, best of 3: 1.14 \u00b5s per loop\n", "size: 300 " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "100000 loops, best of 3: 1.83 \u00b5s per loop\n", "size: 400 " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "100000 loops, best of 3: 2.55 \u00b5s per loop\n" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Magics can do anything they want with their input, so it doesn't have to be valid Python:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%bash\n", "echo \"My shell is:\" $SHELL\n", "echo \"My memory status is:\"\n", "free" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "My shell is: /bin/bash\n", "My memory status is:\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "bash: line 3: free: command not found\n" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Another interesting cell magic: create any file you want locally from the notebook:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%file test.txt\n", "This is a test file!\n", "It can contain anything I want...\n", "\n", "more..." ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing test.txt\n" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "!cat test.txt" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is a test file!\r\n", "It can contain anything I want...\r\n", "\r\n", "more..." ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Let's see what other magics are currently defined in the system:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%lsmagic" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "json": [ "{\"cell\": {\"prun\": \"ExecutionMagics\", \"file\": \"Other\", \"!\": \"OSMagics\", \"capture\": \"ExecutionMagics\", \"timeit\": \"ExecutionMagics\", \"script\": \"ScriptMagics\", \"ruby\": \"Other\", \"system\": \"OSMagics\", \"perl\": \"Other\", \"HTML\": \"Other\", \"bash\": \"Other\", \"python\": \"Other\", \"SVG\": \"Other\", \"javascript\": \"DisplayMagics\", \"writefile\": \"OSMagics\", \"pypy\": \"Other\", \"python3\": \"Other\", \"latex\": \"DisplayMagics\", \"sx\": \"OSMagics\", \"svg\": \"DisplayMagics\", \"html\": \"DisplayMagics\", \"sh\": \"Other\", \"time\": \"ExecutionMagics\", \"debug\": \"ExecutionMagics\"}, \"line\": {\"load\": \"CodeMagics\", \"psource\": \"NamespaceMagics\", \"lsmagic\": \"BasicMagics\", \"logstate\": \"LoggingMagics\", \"logstart\": \"LoggingMagics\", \"popd\": \"OSMagics\", \"ed\": \"Other\", \"pycat\": \"OSMagics\", \"loadpy\": \"CodeMagics\", \"install_ext\": \"ExtensionMagics\", \"cd\": \"OSMagics\", \"pastebin\": \"CodeMagics\", \"clear\": \"KernelMagics\", \"colors\": \"BasicMagics\", \"prun\": \"ExecutionMagics\", \"pushd\": \"OSMagics\", \"rep\": \"Other\", \"config\": \"ConfigMagics\", \"dirs\": \"OSMagics\", \"time\": \"ExecutionMagics\", \"who_ls\": \"NamespaceMagics\", \"install_profiles\": \"DeprecatedMagics\", \"macro\": \"ExecutionMagics\", \"autocall\": \"AutoMagics\", \"alias\": \"OSMagics\", \"bookmark\": \"OSMagics\", \"connect_info\": \"KernelMagics\", \"rehashx\": \"OSMagics\", \"pprint\": \"BasicMagics\", \"system\": \"OSMagics\", \"whos\": \"NamespaceMagics\", \"hist\": \"Other\", \"install_default_config\": \"DeprecatedMagics\", \"logoff\": \"LoggingMagics\", \"env\": \"OSMagics\", \"qtconsole\": \"KernelMagics\", \"load_ext\": \"ExtensionMagics\", \"save\": \"CodeMagics\", \"tb\": \"ExecutionMagics\", \"store\": \"StoreMagics\", \"more\": \"KernelMagics\", \"profile\": \"BasicMagics\", \"doctest_mode\": \"KernelMagics\", \"pylab\": \"PylabMagics\", \"run\": \"ExecutionMagics\", \"reset_selective\": \"NamespaceMagics\", \"pfile\": \"NamespaceMagics\", \"pinfo2\": \"NamespaceMagics\", \"pdef\": \"NamespaceMagics\", \"killbgscripts\": \"ScriptMagics\", \"who\": \"NamespaceMagics\", \"precision\": \"BasicMagics\", \"matplotlib\": \"PylabMagics\", \"quickref\": \"BasicMagics\", \"pinfo\": \"NamespaceMagics\", \"pwd\": \"OSMagics\", \"psearch\": \"NamespaceMagics\", \"autosave\": \"KernelMagics\", \"less\": \"KernelMagics\", \"sc\": \"OSMagics\", \"automagic\": \"AutoMagics\", \"reset\": \"NamespaceMagics\", \"sx\": \"OSMagics\", \"magic\": \"BasicMagics\", \"dhist\": \"OSMagics\", \"timeit\": \"ExecutionMagics\", \"edit\": \"KernelMagics\", \"logstop\": \"LoggingMagics\", \"gui\": \"BasicMagics\", \"xdel\": \"NamespaceMagics\", \"xmode\": \"BasicMagics\", \"notebook\": \"BasicMagics\", \"pdb\": \"ExecutionMagics\", \"recall\": \"HistoryMagics\", \"unalias\": \"OSMagics\", \"unload_ext\": \"ExtensionMagics\", \"alias_magic\": \"BasicMagics\", \"reload_ext\": \"ExtensionMagics\", \"man\": \"KernelMagics\", \"rerun\": \"HistoryMagics\", \"debug\": \"ExecutionMagics\", \"logon\": \"LoggingMagics\", \"page\": \"BasicMagics\", \"pdoc\": \"NamespaceMagics\", \"history\": \"HistoryMagics\"}}" ], "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "Available line magics:\n", "%alias %alias_magic %autocall %automagic %autosave %bookmark %cd %clear %colors %config %connect_info %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic %man %matplotlib %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %run %save %sc %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode\n", "\n", "Available cell magics:\n", "%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n", "\n", "Automagic is ON, % prefix IS NOT needed for line magics." ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Running normal Python code: execution and errors" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Not only can you input normal Python code, you can even paste straight from a Python or IPython shell session:" ] }, { "cell_type": "code", "collapsed": false, "input": [ ">>> # Fibonacci series:\n", "... # the sum of two elements defines the next\n", "... a, b = 0, 1\n", ">>> while b < 10:\n", "... print b\n", "... a, b = b, a+b" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "1\n", "2\n", "3\n", "5\n", "8\n" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "In [1]: for i in range(10):\n", " ...: print i,\n", " ...: " ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 5 6 7 8 9\n" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Error display\n", "And when your code produces errors, you can control how they are displayed with the `%xmode` magic:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%file mod.py\n", "\n", "def f(x):\n", " return 1.0/(x-1)\n", "\n", "def g(y):\n", " return f(y+1)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing mod.py\n" ] } ], "prompt_number": 34 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Now let's call the function `g` with an argument that would produce an error:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import mod\n", "mod.g(0)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "float division 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[0;32mimport\u001b[0m \u001b[0mmod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmod\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/Users/jakevdp/Opensource/2013_fall_ASTR599/notebooks/mod.py\u001b[0m in \u001b[0;36mg\u001b[0;34m(y)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/Users/jakevdp/Opensource/2013_fall_ASTR599/notebooks/mod.py\u001b[0m in \u001b[0;36mf\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1.0\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mZeroDivisionError\u001b[0m: float division by zero" ] } ], "prompt_number": 35 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Plain exceptions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%xmode plain\n", "mod.g(0)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Verbose exceptions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%xmode verbose\n", "mod.g(0)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The default `%xmode` is \"context\", which shows additional context but not all local variables. Let's restore that one for the rest of our session." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%xmode context" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Raw Input in the notebook" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Since 1.0 the IPython notebook web application support `raw_input` which for example allow us to invoke the `%debug` magic in the notebook:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mod.g(0)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%debug" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Don't foget to exit your debugging session. Raw input can of course be use to ask for user input:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "enjoy = raw_input('Are you enjoying this tutorial ?')\n", "print 'enjoy is :', enjoy" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Plotting in the notebook" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "This imports numpy as `np` and matplotlib's plotting routines as `plt`, plus setting lots of other stuff for you to work interactivel very easily:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from matplotlib.pyplot import gcf" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = np.linspace(0, 2*np.pi, 300)\n", "y = np.sin(x**2)\n", "plt.plot(x, y)\n", "plt.title(\"A little chirp\")\n", "f = gcf() # let's keep the figure object around for later..." ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Working with Notebooks\n", "### Directory Layout" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* IPython Notebooks are just files (`.ipynb`) on your file system\n", "* The Notebook server is aware of Notebooks in a single directory, which we call the **Notebook directory**\n", "* If you cd to a Notebook directory and type:\n", "\n", " ipython notebook\n", "\n", " you will see the Notebooks in that directory in the dashboard" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###Notebook Files\n", "* Are just that - files (`.ipynb`) on your file system\n", "* Contain JSON data" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.nbformat import current\n", "with open('03_IPython_intro.ipynb') as f:\n", " nb = current.read(f, 'json')" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "nb.worksheets[0].cells[0:5]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Embed code, Markdown text, LaTeX equations, images\n", "* Are version control friendly: post your Notebooks on GitHub\n", "* Can be viewed online by anyone at http://nbviewer.ipython.org" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Saving and Exporting\n", "\n", "IPython Notebooks can also be exported to `.py` files (see \"File:Download As\" menu item). You can tell the Notebook server to always save these `.py` files alongside the `.ipynb` files by starting the Notebook as:\n", "\n", " ipython notebook --script\n", "\n", "You can import Notebooks from the main Dashboard or simply by copying a Notebook into the Notebook directory." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Overview of the UI\n", "\n", "* Dashboard\n", "* Notebook area and cells\n", "* Menu\n", "* Toolbar" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Cell types\n", "\n", "* Code\n", "* Markdown\n", "* Raw text\n", "* Heading" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Keyboard Shortcuts\n", "\n", "* `Shift-Enter` to run a cell\n", "* `Ctrl-Enter` to run a cell in place\n", "* All other keyboard shortcuts have the form: `Ctrl-m ?`\n", "* Show all keyboard shortcuts using `Ctrl-m h`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Breakout #3: Notebook Practice" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### 1. Using notebooks made available by others on the Internet\n", "\n", "Find a Notebook shared from the [IPython Notebook Gallery](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks) and Download it via \n", "http://nbviewer.ipython.org. Then import it into your running Notebook server using the Dashboard." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### 2. Basic cell operations\n", "\n", "Recall, the Notebook can contain cells of the following types:\n", "\n", "* Code\n", "* Markdown\n", "* Raw text\n", "* Heading\n", "\n", "Create a new Notebook that has at least one of each cell type. Practice the following cell operations:\n", "\n", "* Moving up/down\n", "* Cut/Copy/Paste\n", "* Merge/Split" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### 3. Keyboard shortcuts\n", "\n", "Go back to that last Notebook and repeat some of those cell operations using keyboard shortcuts:\n", "\n", "* Inserting cells above/below\n", "* Delete cell\n", "* Move cell up/down\n", "* Cut/copy/paste cell\n", "* Changing cell types" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### 4. Markdown Cells\n", "\n", "If you have time, download and explore [this notebook](https://raw.github.com/profjsb/python-bootcamp/master/Lectures/04_IPythonNotebookIntroduction/Markdown%20Cells.ipynb), which will introduce you to the use of markdown cells in the notebook." ] } ], "metadata": {} } ] }