Skip to content

Instantly share code, notes, and snippets.

@ivanov
Created February 5, 2014 17:45
Show Gist options
  • Save ivanov/8829312 to your computer and use it in GitHub Desktop.
Save ivanov/8829312 to your computer and use it in GitHub Desktop.
SWC demo notebook
Display the source blob
Display the rendered blob
Raw
{
"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": {},
"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": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(a)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tab completion"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plume = 'Plume was the name of my cat. He was very fat'"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plu"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HELP!\n",
"\n",
"You can get the docstring of an obect by \n",
"\n",
"- ? help\n",
"\n",
"- ?? also cool"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plume?"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# magics\n",
"- part of IPython (not available in Python)\n",
"- generically ``%stuff``, but by default will also run without the leading `%`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%quickref #there are a lot of magics, use this to get oriented"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Running python files"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%run mytest.py"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"%writefile (use the notebook as an editor)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"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 /etc/issue\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"%cd and %ls also useful, but it maybe be cognitive overload for students."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# IPython Notebook supports rich displays\n",
"\n",
"Your code can present itself to the user as anything that a webbrowser can display.\n",
"\n",
"For example, here's how you can link another notebook:\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import FileLink\n",
"\n",
"FileLink('Part 5 - Rich Display System.ipynb')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<a href='Part 5 - Rich Display System.ipynb' target='_blank'>Part 5 - Rich Display System.ipynb</a><br>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"/home/pi/code/ipython/examples/notebooks/Part 5 - Rich Display System.ipynb"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting specific"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we won't talk about, but may be useful for you\n",
"\n",
"- %debug\n",
"- presentation UI"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"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[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-3-d7b436e260d5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"prompt_number": 3
},
{
"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": "heading",
"level": 2,
"metadata": {},
"source": [
"Access to the Shell"
]
},
{
"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": 4,
"text": [
"u'/Users/bgranger/Documents/Computation/IPython/code/ipython/examples/notebooks'"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ls"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"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"
]
}
],
"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": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment