This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
from IPython.core.magic import (register_line_magic,
register_cell_magic)
%
-prefixed magic command). The name of this function is the name of the magic. Then, let's decorate this function with @register_line_magic
. We're done!@register_line_magic
def hello(line):
if line == 'french':
print("Salut tout le monde!")
else:
print("Hello world!")
%hello
%hello french
%%csv
that parses a CSV string and returns a Pandas DataFrame object. This time, the function takes as argument the first line (what follows %%csv
), and the contents of the cell (everything in the cell except the first line).import pandas as pd
#from StringIO import StringIO # Python 2
from io import StringIO # Python 3
@register_cell_magic
def csv(line, cell):
# We create a string buffer containing the
# contents of the cell.
sio = StringIO(cell)
# We use Pandas' read_csv function to parse
# the CSV string.
return pd.read_csv(sio)
%%csv
col1,col2,col3
0,1,2
3,4,5
7,8,9
We can access the returned object with _
.
df = _
df.describe()
csvmagic.py
here) that implements the magic.%%writefile csvmagic.py
import pandas as pd
#from StringIO import StringIO # Python 2
from io import StringIO # Python 3
def csv(line, cell):
sio = StringIO(cell)
return pd.read_csv(sio)
def load_ipython_extension(ipython):
"""This function is called when the extension is loaded.
It accepts an IPython InteractiveShell instance.
We can register the magic with the `register_magic_function`
method of the shell instance."""
ipython.register_magic_function(csv, 'cell')
%load_ext
magic command takes the name of a Python module and imports it, calling immediately load_ipython_extension
. Here, loading this extension automatically registers our magic function %%csv
. The Python module needs to be importable. Here, it is in the current directory. In other situations, it has to be in the Python path. It can also be stored in ~\.ipython\extensions
which is automatically put in the Python path.%load_ext csvmagic
%%csv
col1,col2,col3
0,1,2
3,4,5
7,8,9
Finally, to ensure that this magic is automatically defined in our IPython profile, we can instruct IPython to load this extension at startup. To do this, let's open the file ~/.ipython/profile_default/ipython_config.py
and let's put 'csvmagic'
in the c.InteractiveShellApp.extensions
list. The csvmagic
module needs to be importable. It is common to create a Python package implementing an IPython extension, which itself defines custom magic commands.
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).