Magics

IPython magics do... interesting things. Listing them is beyond the scope of this document, but here are some examples, as well as limitations.

Cell magics

...that take Python

For example, %%timeit will display how long a cell takes to run, running an appropriately large number of samples.


In [1]:
%%timeit
(+ 1 1)


100000000 loops, best of 3: 11.5 ns per loop
Magics with parameters are not supported
If your cell magic doesn't take code, use %%%

...that don't

%%html will just jam some html out to the frontend. To indicate that it shouldn't be compiled as hy, throw in some more magic with another %:


In [2]:
%%%html
<ul>
    <li><input type="radio" name="magic" checked="true"/> magic</li>
    <li><input type="radio" name="magic"/> <a href="http://www.catb.org/jargon/html/magic-story.html">more magic</a></li>
</ul>


Magics with parameters are not supported

Line Magics

! will run a shell command, and show the output.


In [3]:
!ls


Automation.ipynb  Tutorial.ipynb   Untitled.ipynb
Magics.ipynb	  Untitled1.ipynb  Widgets.ipynb
Line magics inside expressions or with args probably won't work

Limitations

Magic arguments aren't supported yet. This means a lot of great stuff won't work, %%file, %%run.


In [4]:
print?


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-205c049c2135> in <module>()
----> 1 is_print

NameError: name 'is_print' is not defined