Home > Net >  iPython custom cell magic - store output in variable?
iPython custom cell magic - store output in variable?

Time:12-02

I recently discovered iPython magic functions and wrote some custom magic. I would like to use cell magic to parse strings, change them slightly and return the result. Is there a way to store the output of my custom cell magic function in a variable?

I know you can store the output of a line magic function like this:

@register_line_magic
def linemagic(line, cell=None):
    #do something
    return line

hello = %linemagic hello
print(hello)

Which returns:

>>> hello

In case I have larger strings, I would like to use cell magic instead:

@register_cell_magic
def cellmagic(line, cell=None):
    #do something
    return cell

It's not possible to use hello = %

  • Related