Search code examples
ipythonjupyter-notebookipython-magiccross-language

IPython (Jupyter) cell magics and cross-language interaction


In a normal ipython notebook or repl session, I can refer to previously-run cells using the _# notation.

However, this is not possible when using cell magics such as %%perl or %%bash because these cells do not have entries in _oh. Nor can I find a way for any two languages to refer to values in the other's namespace.

Does anyone know a way to move information between cells that use different languages, other than using an external file?


Solution

  • I found a method that involves using multiple cell magics.

    %%capture perl_out
    %%perl
    

    will create a CapturedIO object and allow str(perl_out) to be used in a python cell. A fuller example is in this gist.