{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "The IPython Notebook is a front-end to the IPython interpreter that is meant to be a modern scientific notebook.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can\n", "\n", "- write __rich__ _formatted_ `notes`\n", "- display equations and derivations $\\alpha^2 = 15x$, \n", "- embed plots, images, and video, TODO:\n", "- then share it or render it into Latex, HTML, or a number of other output formats. \n", "\n", "It's a great place for data exploration and logging your research progress. Let's learn about some of the features in detail." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The front-end relies on __cells__, which are just text boxes that can be designated as _text_ or _code_. To edit a cell, you just double click on the text for that cell. To create a new cell, use the `Insert` menu at the top of the page to insert a cell above or below the currently selected cell.\n", "\n", "You can change the cell type using the drop-down menu to the left of the words `Cell Toolbar` in the icon bar above. The cell type can be __Code__, __Markdown__, __Raw Text__, or a heading." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Code cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Code cells allow you to write and execute Python code in blocks. This is a bit nicer than the standard interpreter, where you have to do it line-by-line. This is especially helpful when you have to re-run some code -- you can just re-run the whole cell, instead of running each line independently. To run the code, use:\n", "\n", "* Shift + Enter : Runs cell and moved to next cell\n", "* Ctrl + Enter : Runs cell, stays in cell\n", "\n", "In the backend, code cells use the IPython interpreter, and thus has all of the same magic functions, tab complete, and extra help features that we have already seen." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 8*11 + 16\n", "print \"demitri\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"spam\"*10" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All the fancy IPython stuff is available here too:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "int?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "%timeit range(1000)" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Markdown cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Markdown is a human-readable markup format, similar to HTML but much more user-friendly. Markdown cells allow rich text formatting but will also display plain-text as expected." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Markdown basics: lists, markup and code\n", "\n", "* list item \n", "* list item\n", " * nested list item\n", "\n", "\n", "* _italics_\n", "* __bold__\n", "* `fixed-width font`\n", "\n", "Code examples:\n", "\n", " def hello_ipython():\n", " print \"Hello IPython!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I can also render equations __in-line__ by enclosing latex in \\$'s:\n", "\n", "$\\alpha * \\beta + \\gamma^3$\n", "\n", "$\\int^6_0 x^2 dx$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Rich media" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can include images and video, and even write HTML directly in the notebook:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Image\n", "Image(url='http://o.onionstatic.com/images/7/7954/original/700.hq.jpg', width=300)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo('xEhaVhta7sI')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import HTML\n", "HTML(\"\"\n", " \"Everyone loves comic sans!\"\n", " \"\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Misc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* To save a notebook, use __Cmd + s__ (mac) or __Ctrl + s__ (other). For now, you can use the toolbar above to add new cells, and change the type, but there are keyboard bindings for most commands. To see what key-bindings are available, do __Ctrl + m__, __h__." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "nbconvert" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Comes with IPython version >1.0)\n", "\n", "A tool that converts the notebook file into another format, e.g., HTML, Latex, Markdown.\n", "\n", "It's a command-line tool, so you run with:\n", "\n", " ipython nbconvert --to FORMAT notebook-name.ipynb\n", " \n", "Where `FORMAT` can be html, latex, markdown, rtf, or slides." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "[NBViewer](http://nbviewer.ipython.org/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A web service that lets you enter a link to a notebook (you __can't__ upload the file, it has to be hosted somewhere), and it will render it as an HTML page at a permanent link for anyone to view!\n", "\n", "For example: [check this out](http://nbviewer.ipython.org/github/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/blob/master/Chapter1_Introduction/Chapter1_Introduction.ipynb)" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Plotting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you started with --pylab=inline, you're fine. If not, run this command:\n", "\n", " %pylab inline" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "plot(np.random.random(size=(100)))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "[