[IPython-dev] [IPython-User] Cell magics are in!

Brian Granger ellisonbg at gmail.com
Sun May 27 13:53:32 EDT 2012


Kudos to Fernando for pushing this through!

On Sun, May 27, 2012 at 12:07 AM, Fernando Perez <fperez.net at gmail.com> wrote:
> Hi folks,
>
>
> I'm excited to report that we now have cell magics in IPython... PR
> 1732 [1] has just been merged [2], which implements the design
> discussed in IPEP 1 [3]. This is probably one of the largest PRs we've
> had so far, with over 100 commits, over 100 comments and a diff that's
> almost 11000 lines long (a lot of it moving code around, obviously
> it's not all new code).  But it brings two very important thigns:
>
> 1) a refactor of the magic system to finally remove the old mixin
> class we'd had since the very first days of IPython in 2001.  This is
> a cleanup I've been wanting to do for over 10 years!  The new setup
> makes the magic system have  a very clean api, that is easy to use
> both for the implementation of core features and for users to create
> their own magics.

This is a huge improvement and is one of the last areas of the core
that hadn't been cleaned up.  This puts us in a great position.

> 2) the new concept of cell magics: these are magics that get not only
> the line they're on, but the entire cell body as well.  And while
> these are most naturally used in the notebook, as you would expect
> we've built them at the core of IPython, so you can use them with all
> the clients (terminal, qt console, notebook).  For example, this is a
> Cython magic that Brian just prototyped out (we'll have a production
> version of it soon included).  Note that this was copied *from a
> regular text terminal*, not from the notebook:
>
> In [3]: from IPython.core.magic import register_line_cell_magic
>
> In [4]: @register_line_cell_magic
>   ...: def cython(line, cell):
>   ...:     """Compile and import a cell as a .pyx file."""
>   ...:     import sys
>   ...:     from importlib import import_module
>   ...:     module = line.strip()
>   ...:     fname = module + '.pyx'
>   ...:     with open(fname,'w') as f:
>   ...:         f.write(cell)
>   ...:     if 'pyximport' not in sys.modules:
>   ...:         import pyximport
>   ...:         pyximport.install(reload_support=True)
>   ...:     globals()[module] = import_module(module)
>   ...:
>
> In [5]: %%cython bam
>   ...: def f(x):
>   ...:     return 2.0*x
>   ...:
>
> In [6]: bam.f(10)
> Out[6]: 20.0

I have a much improved version of the cython magic that I will submit
a PR for soon (hopefully today).

> In a similar spirit, Jonathan Taylor recently created one to call R
> transparently in the notebook:
>
> https://github.com/jonathan-taylor/Rmagic
>
> This one hasn't been fully updated to the final API, but the core code
> is there and now it should be a trivial matter to update it.
>
>
> I want to thank everyone who pitched in with ideas during the
> discussion and review period, and I hope you'll all enjoy this and
> come up with great ways to use the system.  For now, you can see how
> the system works by playing with %%timeit and %%prun, the only two
> builtins that I extended to work also as cell magics.

Even these two are a great step.

> For more details, see the documentation where we've added also a long
> new section with details and examples of how to create your own [4].

Again, great work!

Cheers,

Brian

> Cheers,
>
> f
>
> [1] https://github.com/ipython/ipython/pull/1732
> [2] https://github.com/ipython/ipython/commit/61eb2ffeebb91a94fe9befe2c30e7839781ddc52
> [2] https://github.com/ipython/ipython/issues/1611
> [3] http://ipython.org/ipython-doc/dev/interactive/reference.html#magic-command-system
> _______________________________________________
> IPython-User mailing list
> IPython-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user



-- 
Brian E. Granger
Cal Poly State University, San Luis Obispo
bgranger at calpoly.edu and ellisonbg at gmail.com



More information about the IPython-dev mailing list