Writing extensions

An extension is essentially a module you import, that accesses IPython functionality to expose various new features to the IPython interpreter.

Typically, every extension module begins with:

import IPython.ipapi
ip = IPython.ipapi.get()

You can take a look at IPython/ipapi.py to see what is available in the IPython public api, or play with the _ip object in interactive IPython session to see what you can do with the cental 'ip' object, above:

[_ipython]|19> _ip.
_ip.IP             _ip.ex             _ip.options        _ip.user_ns
_ip.__doc__        _ip.expose_magic   _ip.runlines       _ip.__class__
_ip.__init__       _ip.getdb          _ip.set_custom_exc
_ip.__module__     _ip.magic          _ip.set_hook
_ip.ev             _ip.meta           _ip.system
[_ipython]|19> _ip.options?
Type:           instancemethod
Base Class:     <type 'instancemethod'>
String Form:    <bound method IPApi.options of <IPython.ipapi.IPApi instance at
0x00C17648>>
Namespace:      Python builtin
File:           f:\prj\ipython\ipython\ipapi.py
Definition:     _ip.options(self)
Docstring:
    All configurable variables

[_ipython]|20>

Declaring magics

Here's a way to declare a new magic function:

def mynewmagic_f(self,arg):
    """ docstring for magic
    """
    ip = self.getapi()

    ...

ip.expose_magic("mymagic",mynewmagic_f)

After this, and after the module with the above definition is imported (e.g. from ipy_user_conf.py), you can just do

$ %mymagic arg1 arg2 blah foo bar

You should use the ipapi instance to access the IPython functionality. Add Online generic