Creating an IPython extension with custom magic commands
Although IPython comes with a wide variety of magic commands, there are cases where we need to implement custom functionality in a new magic command. In this recipe, we will show how to create line and magic cells, and how to integrate them in an IPython extension.
How to do it...
- Let's import a few functions from the IPython magic system:
In : from IPython.core.magic import (register_line_magic, register_cell_magic)
- Defining a new line magic is particularly simple. First, we create a function that accepts the contents of the line (except the initial
%-prefixed name). The name of this function is the name of the magic. Then, we decorate this function with
In : @register_line_magic ...