{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "slideshow": { "slide_type": "skip" } }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import theme\n", "theme.load_style()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Introducing the IPython Notebook\n", "\n", "This lecture is an adaptation of the lecture at [Teaching Numerics with Notebooks](http://nbviewer.ipython.org/github/ketch/teaching-numerics-with-notebooks/blob/master/Introducing%20the%20IPython%20Notebook.ipynb) and is covered by the following license\n", "\n", "\"Creative\n", "Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## What is this?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a gentle introduction to the IPython Notebook aimed at lecturers who wish to incorporate it in their teaching, written in an IPython Notebook. This presentation adapts material from the [IPython official documentation](http://nbviewer.ipython.org/github/ipython/ipython/blob/2.x/examples/Notebook)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## What is an IPython Notebook?\n", "\n", "An IPython Notebook is a:\n", "\n", "**[A]** Interactive environment for writing and running code \n", "**[B]** Weave of code, data, prose, equations, analysis, and visualization \n", "**[C]** Tool for prototyping new code and analysis \n", "**[D]** Reproducible workflow for scientific research \n", "**[E]** All of the above" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "**[E]** All of the above" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## How Do I Open an IPython Notebook?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open a command prompt and execute \n", "\n", " ipython notebook\n", "and an IPython Notebook server will start and launch a viewer in the default web browser. Notebooks found in the working directory will show up in the list of notebooks. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Example\n", "\n", "For example, navigating to the course `Lessons` directory and executing `ipython notebook` will launch a browser window similar to that shown below\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Writing and Running Code\n", "\n", "The IPython Notebook consists of an ordered list of cells. \n", "\n", "There are three important cell types:\n", "\n", "* **Code**\n", "* **Markdown**\n", "* **Raw NBConvert**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Code Cells" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# This is a code cell made up of Python comments\n", "# We can execute it by clicking on it with the mouse\n", "# then clicking the \"Run Cell\" button" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World\n" ] } ], "source": [ "# A comment is a pretty boring piece of code\n", "# This code cell generates \"Hello, World\" when executed\n", "\n", "print \"Hello, World\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Code Cells" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "image/png": [ "iVBORw0KGgoAAAANSUhEUgAAAXUAAAEACAYAAABMEua6AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n", "AAALEgAACxIB0t1+/AAADstJREFUeJzt3X+M5PVdx/Hni7trbUNSQkjOwm1zjUBSTI1HG3qhVdZa\n", "E3oxV/8gSmNDQ4wlRAT5w9Q2jT3/0n+MFZviqUCoVQihhlz1asXKYRuTs+39KD8OBVPiHQ1H0+vR\n", "wqXJnX37x8zBMOzOzO7N3Hfus89HsmG+M5+deWcz99zPfHcmpKqQJLXhvK4HkCRNj1GXpIYYdUlq\n", "iFGXpIYYdUlqiFGXpIaMjH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n", "5CRwe1X9LLAV+J01/LM47TbgSSZ/l1Wr/hzYXVXvAH4OONTxPJ3o/+3ut4Erq+qd9E75Xt/lTGfZ\n", "PfRaOegPgIer6nLgq/3jkWa9U5/kw0trQlU9X1UH+pdfovcP9+Jup+pOkk3ANuBvOBffYjAlSd4C\n", "/EJV3Q29v2NVVYdv/e3UD+ltft6cZD3wZpZ9J117quprwA+Grn7lw539//7auPuZddQn+fDSmtPf\n", "kWwB9nY7Saf+DPh94CddD9KxtwPfS3JPkn1J/jrJm7seqgtVdQz4U+B/6b3b7nhV/Wu3U3Vu48C7\n", "CY8CYz+tP+uor/WX1a+T5HzgQeC2/o59zUnyq8ALVbWfNbxL71sPXAl8rqqupPcOsrEvsVuU5GeA\n", "3wM203sVe36S3+x0qDlSvXe1jG3qrKM+yYeX1owkG4AvAl+oqoe6nqdDVwPbk3wHuA94f5LPdzxT\n", "V44AR6rqG/3jB+lFfi16N/AfVfX9qjoF/AO958padjTJTwMkeSvwwrhvmHXUX/nwUpI30Pvw0q4Z\n", "P+ZcShLgLuDJqvpM1/N0qao+WVULVfV2en8I+7equqHrubpQVc8Dh5Nc3r/qA8ATHY7UpaeArUne\n", "1P/38gF6f0hfy3YBH+1f/igwdjM47hOlZ2S5Dy/N8jHn2HuBjwDfTrK/f90nquqfO5xpXqz103S/\n", "C/xdf+PzP6zRD/BV1cH+K7Zv0vtbyz7gr7qd6uxJch9wDXBRksPAHwJ/AjyQ5LeAZ4FfH3s/fvhI\n", "ktrh/85Okhpi1CWpIUZdkhpi1CWpIUZdkhpi1CWpIUZdkhpi1CWpIf8Phsz8LRTwjL0AAAAASUVO\n", "RK5CYII=\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Code cells can also generate graphical output\n", "%matplotlib inline\n", "import matplotlib\n", "matplotlib.pyplot.hist([0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 10]);" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Modal editor\n", "\n", "Starting with IPython 2.0, the IPython Notebook has a modal user interface. This means that the keyboard does different things depending on which mode the Notebook is in. There are two modes: edit mode and command mode." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Edit mode\n", "\n", "Edit mode is indicated by a green cell border and a prompt showing in the editor area:\n", "\n", "\n", "\n", "When a cell is in edit mode, you can type into the cell, like a normal text editor.\n", "\n", "
\n", "Enter edit mode by pressing `enter` or using the mouse to click on a cell's editor area.\n", "
\n", "\n", "
\n", "While in edit mode, tab-completion works for variables the kernel knows about from executing previous cells.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Command mode\n", "\n", "Command mode is indicated by a grey cell border:\n", "\n", "\n", "\n", "When you are in command mode, you are able to edit the notebook as a whole, but not type into individual cells. Most importantly, in command mode, the keyboard is mapped to a set of shortcuts that let you perform notebook and cell actions efficiently. For example, if you are in command mode and you press `c`, you will copy the current cell - no modifier is needed.\n", "\n", "
\n", "Don't try to type into a cell in command mode; unexpected things will happen!\n", "
\n", "\n", "
\n", "Enter command mode by pressing `esc` or using the mouse to click *outside* a cell's editor area.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Mouse navigation\n", "\n", "All navigation and actions in the Notebook are available using the mouse through the menubar and toolbar, which are both above the main Notebook area:\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Cell Clicking\n", "\n", "The first idea of mouse based navigation is that **cells can be selected by clicking on them.** The currently selected cell gets a grey or green border depending on whether the notebook is in edit or command mode. If you click inside a cell's editor area, you will enter edit mode. If you click on the prompt or output area of a cell you will enter command mode.\n", "\n", "If you are running this notebook in a live session (not on http://nbviewer.ipython.org) try selecting different cells and going between edit and command mode. Try typing into a cell." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Current Selection\n", "\n", "The second idea of mouse based navigation is that **cell actions usually apply to the currently selected cell**. Thus if you want to run the code in a cell, you would select it and click the \"Play\" button in the toolbar or the \"Cell:Run\" menu item. Similarly, to copy a cell you would select it and click the \"Copy\" button in the toolbar or the \"Edit:Copy\" menu item. With this simple pattern, you should be able to do most everything you need with the mouse." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Markdown Rendering\n", "\n", "Markdown and heading cells have one other state that can be modified with the mouse. These cells can either be rendered or unrendered. When they are rendered, you will see a nice formatted representation of the cell's contents. When they are unrendered, you will see the raw text source of the cell. To render the selected cell with the mouse, click the \"Play\" button in the toolbar or the \"Cell:Run\" menu item. To unrender the selected cell, double click on the cell." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Keyboard Navigation\n", "\n", "The modal user interface of the IPython Notebook has been optimized for efficient keyboard usage. This is made possible by having two different sets of keyboard shortcuts: one set that is active in edit mode and another in command mode." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most important keyboard shortcuts are `enter`, which enters edit mode, and `esc`, which enters command mode.\n", "\n", "In edit mode, most of the keyboard is dedicated to typing into the cell's editor. Thus, in edit mode there are relatively few shortcuts:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Keyboard Navigation\n", "\n", "In command mode, the entire keyboard is available for shortcuts.\n", "\n", "Here the rough order in which the IPython Developers recommend learning the command mode shortcuts:\n", "\n", "1. Basic navigation: `enter`, `shift-enter`, `up/k`, `down/j`\n", "2. Saving the notebook: `s`\n", "2. Cell types: `y`, `m`, `1-6`, `t`\n", "3. Cell creation and movement: `a`, `b`, `ctrl+k`, `ctrl+j`\n", "4. Cell editing: `x`, `c`, `v`, `d`, `z`, `shift+=`\n", "5. Kernel operations: `i`, `0`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I suggest learning `h` first!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The IPython Notebook Architecture\n", "\n", "So far, we have learned the basics of using IPython Notebooks.\n", "\n", "For simple demonstrations, the typical user doesn't need to understand how the computations are being handled, but to successfully write and present computational notebooks, **you** will need to understand how the notebook architecture works." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "A *live* notebook is composed of an interactive web page (the front end), a running IPython session (the kernel or back end), and a web server responsible for handling communication between the two (the, err..., middle-end)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "A *static* notebook, as for example seen on NBViewer, is a static view of the notebook's content. The default format is HTML, but a notebook can also be output in PDF or other formats." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "The centerpiece of an IPython Notebook is the \"kernel\", the IPython instance responsible for executing all code. Your IPython kernel maintains its state between executed cells." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "x = 0\n", "print x" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "x += 1\n", "print x" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Interacting with the Kernel\n", "\n", "There are two important actions for interacting with the kernel. The first is to interrupt it. This is the same as sending a Control-C from the command line. The second is to restart it. This completely terminates the kernel and starts it anew. None of the kernel state is saved across a restart. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Markdown cells\n", "\n", "Text can be added to IPython Notebooks using Markdown cells. Markdown is a popular markup language that is a superset of HTML. Its specification can be found here:\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Markdown basics" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Text formatting\n", "\n", "You can make text *italic* or **bold**." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Itemized Lists\n", "\n", "* One\n", " - Sublist\n", " - This\n", " - Sublist\n", " - That\n", " - The other thing\n", "* Two\n", " - Sublist\n", "* Three\n", " - Sublist" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Enumerated Lists\n", "\n", "1. Here we go\n", " 1. Sublist\n", " 2. Sublist\n", "2. There we go\n", "3. Now this" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Horizontal Rules\n", "\n", "---\n", "\n", "---\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Blockquotes\n", "\n", "> To me programming is more than an important practical art. It is also a gigantic undertaking in the foundations of knowledge. -- Rear Admiral Grace Hopper" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Links\n", "\n", "[IPython's website](http://ipython.org)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Code\n", "\n", " def f(x):\n", " \"\"\"a docstring\"\"\"\n", " return x**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use triple-backticks to denote code blocks.\n", "This also allows you to choose the appropriate syntax highlighter.\n", "\n", "```C\n", "if (i=0; i Be Bold! " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Mathematical Equations\n", "\n", "Courtesy of MathJax, you can beautifully render mathematical expressions, both inline: \n", "$e^{i\\pi} + 1 = 0$, and displayed:\n", "\n", "$$e^x=\\sum_{i=0}^\\infty \\frac{1}{i!}x^i$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Equation Environments\n", "\n", "You can also use a number of equation environments, such as `align`:\n", "\n", "\\begin{align}\n", " x &= 4 \\\\\n", "y+z &= x\n", "\\end{align}\n", "\n", "[A full list of available TeX and LaTeX commands is maintained by Dr. Carol Burns.](http://www.onemathematicalcat.org/MathJaxDocumentation/TeXSyntax.htm)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Other Useful MathJax Notes\n", "\n", "* inline math is demarcated by `$ $`, or `\\( \\)`\n", "* displayed math is demarcated by `$$ $$` or `\\[ \\]`\n", "* displayed math environments can also be directly demarcated by `\\begin` and `\\end`\n", "* `\\newcommand` and `\\def` are supported, *within* areas MathJax processes (such as in a `\\[ \\]` block)\n", "* equation numbering is not officially supported, but it can be indirectly enabled" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## A Note about Notebook Security\n", "\n", "By default, a notebook downloaded to a new computer is *untrusted*\n", "\n", "* HTML and Javascript in Markdown cells is now *never* executed\n", "* HTML and Javascript code outputs must be explicitly *re-executed*\n", "* Some of these restrictions can be mitigrated through shared accounts (Sage MathCloud) and secrets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More information on notebook security is in the [IPython Notebook documentation](http://ipython.org/ipython-doc/stable/notebook/security.html)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Magics\n", "\n", "IPython kernels execute a superset of the Python language. The extension functions, commonly referred to as *magics*, come in two variants. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Line Magics\n", "\n", "* A *line magic* looks like a command line call. The most important of these is `%matplotlib inline`, which embeds all matplotlib plot output as images in the notebook itself." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Variable Type Data/Info\n", "--------------------------------\n", "matplotlib module matplotlib/__init__.pyc'>\n", "x int 1\n" ] } ], "source": [ "%whos" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Cell Magics\n", "\n", "* A *cell magic* takes its entire cell as an argument. Although there are a number of useful cell magics, you may find `%%timeit` to be useful for exploring code performance." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 464.75 times longer than the fastest. This could mean that an intermediate result is being cached \n", "100000 loops, best of 3: 17.6 µs per loop\n" ] } ], "source": [ "%%timeit\n", "\n", "import numpy as np\n", "np.sum(np.random.rand(1000))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Interacting with the Command Line\n", "\n", "IPython supports one final trick, the ability to interact directly with your shell by using the `!` operator." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[30mImages\u001b[m\u001b[m Lesson11_HigherOrderShapeFuncs.ipynb\r\n", "Lesson01_IPythonNotebookTutorial.ipynb Lesson12_ThermalConvection.ipynb\r\n", "Lesson01_IPythonNotebookTutorial.slides.html Lesson13_AdvectionDiffusion.ipynb\r\n", "Lesson02_IntroductionToPython.ipynb Lesson14_NonlinearMaterials.ipynb\r\n", "Lesson03_DirectMethods.ipynb Lesson15_ErrorAnalysis.ipynb\r\n", "Lesson04_PlaneTruss.ipynb Lesson16_MethodOfManufacturedSolutions.ipynb\r\n", "Lesson05_StrongForm.ipynb Lesson17_EulerBernouliBeamElement.ipynb\r\n", "Lesson06_VariationalForms.ipynb Lesson20_GeneralizedBoundaryConditions.ipynb\r\n", "Lesson07_TheWeakForm.ipynb MathematicalPreliminaries.ipynb\r\n", "Lesson08_DerivationOf1DEquations.ipynb \u001b[30mstyles\u001b[m\u001b[m\r\n", "Lesson09_ExamplesIn1D.ipynb theme.py\r\n", "Lesson10_NumericalIntegration.ipynb theme.pyc\r\n" ] } ], "source": [ "!ls" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = !ls" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Images', 'Lesson01_IPythonNotebookTutorial.ipynb', 'Lesson01_IPythonNotebookTutorial.slides.html', 'Lesson02_IntroductionToPython.ipynb', 'Lesson03_DirectMethods.ipynb', 'Lesson04_PlaneTruss.ipynb', 'Lesson05_StrongForm.ipynb', 'Lesson06_VariationalForms.ipynb', 'Lesson07_TheWeakForm.ipynb', 'Lesson08_DerivationOf1DEquations.ipynb', 'Lesson09_ExamplesIn1D.ipynb', 'Lesson10_NumericalIntegration.ipynb', 'Lesson11_HigherOrderShapeFuncs.ipynb', 'Lesson12_ThermalConvection.ipynb', 'Lesson13_AdvectionDiffusion.ipynb', 'Lesson14_NonlinearMaterials.ipynb', 'Lesson15_ErrorAnalysis.ipynb', 'Lesson16_MethodOfManufacturedSolutions.ipynb', 'Lesson17_EulerBernouliBeamElement.ipynb', 'Lesson20_GeneralizedBoundaryConditions.ipynb', 'MathematicalPreliminaries.ipynb', 'styles', 'theme.py', 'theme.pyc']\n" ] } ], "source": [ "print x" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## A Note about Notebook Version Control\n", "\n", "The IPython Notebook is stored using canonicalized JSON for ease of use with version control systems.\n", "\n", "There are two things to be aware of:\n", "\n", "* By default, IPython embeds all content and saves kernel execution numbers. You may want to get in the habit of clearing all cells before committing.\n", "\n", "* As of IPython 2.0, all notebooks are signed on save. This increases the chances of a commit collision during merge, forcing a manual resolution. Either signature can be safely deleted in this situation." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }