Setting attributes for use in your magics
from IPython.core.magic import magics_class, Magics, line_magic, cell_magic
@magics_class
class MyMagics(Magics):
foo = 0
@line_magic
def setfoo(self, line):
self.foo = int(line)
@cell_magic
def foocell(self, line, cell):
print cell.format(foo=self.foo)
ip = get_ipython()
ip.magics_manager.register(MyMagics)
%%foocell
is a cell magic, that just renders the cell as a string template
%%foocell
foo = {foo}
foo = 0
%setfoo
just sets the foo attribute, for use in %%foocell
%setfoo 5
%%foocell
foo was 0, but now foo is {foo}
foo was 0, but now foo is 5