IPython magic commands

Jupyter notebooks are a scientific computing development platform that can run many different programming languages, including Python via the interactive Python interpreter IPython. The key advantage in a notebook environment is that code can be sectioned into cells for testing while retaining all the interactive features of IPython, including magic commands.
IPython magics are a specialised set of commands hosted by the Python kernel (ie. the core of a computer system) and require the prefix %
to specify the commands. For example, if we are working in a Jupyter notebook and wanted to plot a histogram, our code might look something like this:
1 2 3 4 5 6 |
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
dist_norm = np.random.normal(loc=0, scale=1, size=1000)
plt.hist(dist_norm, bins=30, color='0.30');
|
In line 3, the magic command %matplotlib inline
tells the notebook to display a matplotlib figure embedded in the notebook, which looks something like this:
Pretty cool! Other useful magic commands include %pwd
which prints the working directory (these commands are shown here when working in the Terminal):
1 2 |
In [1]: %pwd
Out[1]: '/home/joanna/Dropbox/Sketchbook/wordpress'
|
and %whos
which prints all variables in the workspace:
1 2 3 4 5 6 7 |
In [2]: %whos
Variable Type Data/Info
--------------------------------------
dist_norm ndarray 1000: 1000 elems, type `float64`, 8000 bytes
fahr_to_kelvin function <function fahr_to_kelvin at 0x7f34f398aae8>
np module <module 'numpy' from '/ho<...>kages/numpy/__init__.py'>
plt module <module 'matplotlib.pyplo<...>es/matplotlib/pyplot.py'>
|
A final useful command is %paste
, which removes the indentation of copied code before it is pasted to the console. This command is most useful when writing a Python script because it allows bits of indented code to be pasted directly into a console for testing. To use %paste
, copy the lines of code we want to test into the clipboard, type %paste
in IPython, press the Enter key and IPython executes the code.
For example, suppose we have a function to perform a temperature conversion, but we simply want to test the line of code inside the function that performs the conversion. First, create the variable temp_f
outside the function, then copy line 2 in the code below and %paste
it into IPython. The output looks something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
In [2]: def fahr_to_kelvin(temp_f):
temp_k = ((temp_f-32)*(5/9)) + 273.15
return temp_k
In [3]: temp_f = 10
In [4]: %paste
temp_k = ((temp_f-32)*(5/9)) + 273.15
## -- End pasted text --
In [5]: print(temp_k)
260.92777777777775
|
As of IPython 5, pasting this way does not require a magic command, and code can simply be pasted. But if you are using an older version of IPython, %paste
is still useful. More information on IPython’s built-in magic commands can be found here.
Summary
IPython magic commands provide extra functionality to develop code, and some are especially useful when writing code in Jupyter notebooks.