IPython Notebook lets you do more than just program

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Making Magic

The IPython Notebook offers many so-called magic commands. They always begin with the % symbol. The %quickref command gives a quick overview, and Listing 1 shows an excerpt of the possibilities. Two important magic command representatives are the %ls and %less commands. The functionality of these commands corresponds to the shell commands of the same name. However, they are available independently of the operating system and consequently can also be used when working in Windows. If the magical powers are not strong enough, you can use ! to directly execute shell commands. Figure 4 shows examples.

Listing 1

IPython %quickref

IPython -- An enhanced Interactive Python - Quick Reference Card
obj?, obj??      : Get help, or more help for object (also works as
                   ?obj, ??obj).
?foo.*abc*       : List names in 'foo' containing 'abc' in them.
%magic           : Information about IPython's 'magic' % functions.
Magic functions are prefixed by % or %%, and typically take
their arguments without parentheses, quotes or even commas for
convenience. Magical line commands take a single % and magical
cell commands are prefixed with two %%.
Example magic function calls:
%alias d ls -F   : 'd' is now an alias for 'ls -F'
alias d ls -F    : Works if 'alias' is not a Python name
alist = %alias   : Get list of aliases to 'alist'
cd /usr/share    : Obvious. Use cd -<tab> to choose from visited dirs.
%cd??            : See help AND source for magic %cd
%timeit x=10     : time the 'x=10' statement with high precision.
%%timeit x=2**100
x**100           : time 'x**100' with a setup of 'x=2**100';
setup code is not counted.  This is an example of a cell magic.
System commands:
!cp a.txt b/     : System command escape, calls os.system()
cp a.txt b/      : after %rehashx, most system commands work without !
cp ${f}.txt $bar : Variable expansion in magic and system commands
files = !ls /usr : Capture system command output
files.s, files.l, files.n: "a b c", ['a','b','c'], 'a\nb\nc'
_i, _ii, _iii    : Previous, next previous, next next previous input
_i4, _ih[2:5]    : Input history line 4, lines 2-4
exec _i81        : Execute input history line #81 again
%rep 81          : Edit input history line #81
_, __, ___       : previous, next previous, next next previous output
_dh              : Directory history
_oh              : Output history
%hist            : Command history. '%hist -g foo' search history for 'foo'
f 1,2            : f(1,2)  # Off by default, enable with %autocall magic.
/f 1,2           : f(1,2) (forced automatic parenthesization)
,f 1 2           : f("1","2")
;f 1 2           : f("1 2")
Remember: Tab completion works in many contexts, not just file names
or Python names.
The following magic functions are currently available:
    Define an alias for a system command.
    Make functions callable without having to type parentheses.
    Make magic functions callable without having to type the initial %.
    Set the autosave interval in the notebook (in seconds).
    Manage IPython's bookmark system.
    Callable object storing the details of one alias.
    Change the current working directory.
Figure 4: Complete access to the command line from IPython Notebook.

The return values can also be saved in Python variables. As a result, the t = !ls | grep -i ubuntu call saves the result of the grep command in t as a ['Ubuntu User iPython Demo.ipynb'] list.

Code Gallery

Python has a standard way of representing diagrams in the form of matplotlib [6]. IPython Notebook supports the direct output of images. The magic command %matplotlib inline turns on the required mode. Figure 5 shows the result. In this case, the image is only a static PNG file; however, several extensions make it possible to show interactive diagrams in the notebook. These extensions work with JavaScript to interactively enlarge excerpts.

Figure 5: Images are directly displayed in IPython Notebook.

The IPython Notebook environment can also display tables in an attractive way. Objects need only have a _repr_html_() method. IPython Notebook will invoke this method before the usually invoked method __repr__() . Figure 6 shows how a Pandas data frame, which has a _repr_html_() method, looks as a table. Pandas [7] is a commonly used library for fast and extremely convenient work with tabular data.

Figure 6: The table in HTML format comes from the DataFrame object.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content