Working With Code Cells.ipynb
170 lines
| 4.2 KiB
| text/plain
|
TextLexer
Fernando Perez
|
r5779 | { | |
"metadata": { | |||
Brian E. Granger
|
r16108 | "name": "", | |
Brian E. Granger
|
r17495 | "signature": "sha256:1cf51b66a39fb370f2fb3d08af95cfc79b8884f310509181b3b0586400e20b81" | |
Fernando Perez
|
r5779 | }, | |
Brian Granger
|
r6035 | "nbformat": 3, | |
MinRK
|
r7739 | "nbformat_minor": 0, | |
Fernando Perez
|
r5779 | "worksheets": [ | |
{ | |||
"cells": [ | |||
{ | |||
Brian Granger
|
r9191 | "cell_type": "heading", | |
"level": 1, | |||
"metadata": {}, | |||
"source": [ | |||
"Running Code in the IPython Notebook" | |||
] | |||
}, | |||
{ | |||
Fernando Perez
|
r5779 | "cell_type": "markdown", | |
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian E. Granger
|
r17495 | "First and foremost, the IPython Notebook is an interactive environment for writing and running code. IPython is capable of running code in a wide range of languages. However, in IPython 2.0, the default kernel runs Python code." | |
Brian Granger
|
r9191 | ] | |
}, | |||
{ | |||
"cell_type": "heading", | |||
"level": 2, | |||
"metadata": {}, | |||
"source": [ | |||
"Code cells allow you to enter and run Python code" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"metadata": {}, | |||
"source": [ | |||
Brian E. Granger
|
r17495 | "Run a code cell using `Shift-Enter` or pressing the <button><i class=\"icon-play fa fa-play\"></i></button> button in the toolbar above:" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
Brian Granger
|
r9191 | "collapsed": false, | |
Fernando Perez
|
r5779 | "input": [ | |
Brian Granger
|
r9191 | "a = 10" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "outputs": [], | |
Brian E. Granger
|
r16108 | "prompt_number": 1 | |
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "code", | |||
Brian Granger
|
r9191 | "collapsed": false, | |
Fernando Perez
|
r5779 | "input": [ | |
Thomas Kluyver
|
r9198 | "print(a)" | |
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "outputs": [ | |
{ | |||
"output_type": "stream", | |||
"stream": "stdout", | |||
"text": [ | |||
"10\n" | |||
] | |||
} | |||
], | |||
Brian E. Granger
|
r16108 | "prompt_number": 2 | |
Fernando Perez
|
r5779 | }, | |
{ | |||
Brian E. Granger
|
r17495 | "cell_type": "markdown", | |
"metadata": {}, | |||
"source": [ | |||
"There are two other keyboard shortcuts for running code:\n", | |||
"\n", | |||
"* `Alt-Enter` runs the current cell and inserts a new one below.\n", | |||
"* `Ctrl-Enter` run the current cell and enters command mode." | |||
] | |||
}, | |||
{ | |||
Brian Granger
|
r9191 | "cell_type": "heading", | |
"level": 2, | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "Managing the IPython Kernel" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian E. Granger
|
r17495 | "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 <button><i class='icon-stop fa fa-stop'></i></button> button in the toolbar above." | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
Brian Granger
|
r9191 | "collapsed": false, | |
Fernando Perez
|
r5779 | "input": [ | |
Brian Granger
|
r9191 | "import time\n", | |
"time.sleep(10)" | |||
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
Brian E. Granger
|
r16108 | "outputs": [] | |
Fernando Perez
|
r5779 | }, | |
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian Granger
|
r9191 | "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:" | |||
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
"collapsed": false, | |||
"input": [ | |||
Brian Granger
|
r9191 | "import sys\n", | |
"from ctypes import CDLL\n", | |||
Brian E. Granger
|
r17495 | "# This will crash a Linux or Mac system\n", | |
"# equivalent calls can be made on Windows\n", | |||
Thomas Kluyver
|
r9198 | "dll = 'dylib' if sys.platform == 'darwin' else 'so.6'\n", | |
Brian Granger
|
r9191 | "libc = CDLL(\"libc.%s\" % dll) \n", | |
"libc.time(-1) # BOOM!!" | |||
Fernando Perez
|
r5779 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r15188 | "outputs": [] | |
Brian Granger
|
r9191 | }, | |
{ | |||
"cell_type": "heading", | |||
"level": 2, | |||
"metadata": {}, | |||
"source": [ | |||
Brian E. Granger
|
r17495 | "Cell menu" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian E. Granger
|
r17495 | "The \"Cell\" menu has a number of menu items for running code in different ways. These includes:\n", | |
"\n", | |||
"* Run and Select Below\n", | |||
"* Run and Insert Below\n", | |||
"* Run All\n", | |||
"* Run All Above\n", | |||
"* Run All Below" | |||
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
Brian Granger
|
r9191 | "cell_type": "heading", | |
"level": 2, | |||
MinRK
|
r7739 | "metadata": {}, | |
Fernando Perez
|
r5779 | "source": [ | |
Brian E. Granger
|
r17495 | "Restarting the kernels" | |
Fernando Perez
|
r5779 | ] | |
}, | |||
{ | |||
Brian Granger
|
r9191 | "cell_type": "markdown", | |
MinRK
|
r7739 | "metadata": {}, | |
Brian Granger
|
r9191 | "source": [ | |
Brian E. Granger
|
r17495 | "The kernel maintains the state of a notebook's computations. You can reset this state by restarting the kernel. This is done by clicking on the <button><i class='icon-repeat'></i></button> in the toolbar above." | |
Fernando Perez
|
r5779 | ] | |
} | |||
MinRK
|
r7739 | ], | |
"metadata": {} | |||
Fernando Perez
|
r5779 | } | |
] | |||
MinRK
|
r15188 | } |