[IPython-dev] cell magics

Jason Grout jason-sage@creativetrax....
Fri Feb 15 21:21:18 CST 2013


William Stein and I have been discussing cell magics lately as he is 
implementing them in his Salvus project.  I think the result, which is a 
little different than the current IPython system, is more consistent, 
easier to read, and more elegant.  What are your thoughts?

PROPOSAL:

Line and cell magics are normal python functions that take a string as 
their first input.  The string is either the rest of the line, or if 
there is no discernible string on the rest of the line, the string is 
taken as the rest of the cell.  As an example:

%timeit(runs=10) 2+3

which gets translated to time("2+3", runs=10)

A cell magic is exactly the same function, only it is invoked by putting 
the string on the following lines, rather than on the same line, so the 
string is taken as the remainder of the cell:

%timeit(runs=10)
2+3
5+6

which gets translated to timeit("2+3\n5+6", runs=10)

Notice there is no distinction between line and cell magic 
functions---the distinction entirely happens in how they are invoked 
(whether the string is on the same line or on following lines).  Also, 
magic options are passed in exactly the same way that arguments are 
always passed to python functions, instead of having a non-pythonic, 
bash-like syntax for options.

END OF PROPOSAL

That's the proposal.  I got to thinking more about it too.  The whole 
concept of magics (passing a string to a function) is like decorators. 
In fact, we could make it exactly like decorators, if we wanted (I'm not 
sure that we want to introduce the extra complexity, but it would be 
more consistent with decorators):

%timeit 2+3

transforms to timeit("2+3"), but

%timeit(runs=4) 2+3

effectively does timeit(runs=4)("2+3")

In some sense, the magics are implementing a very (*very*) rudimentary 
macro system for python, the advantages being that we can pass an 
unevaluated string into a function without having to type the quote 
marks.  Right now we can only pass in single-line strings or the entire 
rest of the cell.  But what if we brainstormed for other ways to denote 
a string that was to be treated special?  For example, we could use a 
github/markdown-like syntax:

```timeit(runs=4)
2+3
5+6
```

(except that python supports `a` is repr(a)).  But this might look nice:

```R
hist(x)
```

translating to R("hist(x)"). In fact, we could make it even more lispy 
by supporting parameter expansion:

```R
hist(%d)
```%x

which would do something like R("hist(%d)"%x)

I'm just brainstorming at the end here, but I'm still curious your 
thoughts on the subject, especially on the first proposal of unifying 
the line and cell magic syntax and calling syntax to be more consistent 
with each other and with python.

Thanks,

Jason



More information about the IPython-dev mailing list