##// END OF EJS Templates
Pulling in content and rewriting from ipython-in-depth.
Pulling in content and rewriting from ipython-in-depth.

File last commit:

r17495:da98aae1
r17496:b0d6ca14
Show More
Working With Code Cells.ipynb
170 lines | 4.2 KiB | text/plain | TextLexer
/ examples / Notebook / Working With Code Cells.ipynb

Running Code in the IPython Notebook

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.

Code cells allow you to enter and run Python code

Run a code cell using Shift-Enter or pressing the <button></button> button in the toolbar above:

In [1]:
a = 10
In [2]:
print(a)
10

There are two other keyboard shortcuts for running code:

  • Alt-Enter runs the current cell and inserts a new one below.
  • Ctrl-Enter run the current cell and enters command mode.

Managing the IPython Kernel

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></button> button in the toolbar above.

In [ ]:
import time
time.sleep(10)

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 ctypes to segfault the Python interpreter:

In [ ]:
import sys
from ctypes import CDLL
# This will crash a Linux or Mac system
# equivalent calls can be made on Windows
dll = 'dylib' if sys.platform == 'darwin' else 'so.6'
libc = CDLL("libc.%s" % dll) 
libc.time(-1)  # BOOM!!

Cell menu

The "Cell" menu has a number of menu items for running code in different ways. These includes:

  • Run and Select Below
  • Run and Insert Below
  • Run All
  • Run All Above
  • Run All Below

Restarting the kernels

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></button> in the toolbar above.