[IPython-User] Adding a magic to ipython_config

Fernando Perez fperez.net@gmail....
Fri Oct 22 15:28:51 CDT 2010


Hi Jorgen,

On Fri, Oct 22, 2010 at 1:15 PM, Jörgen Stenarson
<jorgen.stenarson@bostream.nu> wrote:
>
> I'm running ipython 0.11. I want to add a magic function.
> In the manual in the magics section ip.expose_magic is used.
> But in the section on configuration ip. is never used.
>
> What is the recommended way to add a magic in ipython_config?
>

The config system is now split in two parts: the _config file is read
at startup time, and ipython itself isn't fully ready yet.  So only
static values can be set at that point.  If you need to execute code
once ipython itself is active, the easiest thing to do is to define a
file to be executed later:

c.Global.exec_files = ['extras.py']

My extras file then looks like this:

# a bunch of function definitions...

#-----------------------------------------------------------------------------
# MAIN: Expose magics and cleanup global namespace so our local names don't
# collide with their actual magic versions
ip = _ip = get_ipython()

for mm in ['rundemo','symreals','symintegers']:
    exec 'ip.define_magic("%s",%s)' % (mm,mm)
    exec 'del %s' % mm
del mm

# My personal aliases for some magics
ip.magic_c   = ip.magic_cd
ip.magic_pd  = ip.magic_pushd
ip.magic_pop = ip.magic_popd
ip.magic_ds  = ip.magic_dirs


I hope this helps (and yes, we need to update the manuals badly).

Cheers,

f


More information about the IPython-User mailing list