How to run an IPython CELL magic from a script (%% magic)

1.5k Views Asked by At

Jupyter magic commands (starting with a single %, e.g. %timeit) can be run in a script using the answer from How to run an IPython magic from a script (or timing a Python script)

However, I cannot find an answer on how to run cell magic commands, e.g. In Jupyter we can do:

%%sh -s $task_name
#!/bin/bash
task_name=$1
echo This is a script running task [$task_name] that is executed from python
echo Many more bash commands here...................

How can this be written such that it can be executed from a python script?

1

There are 1 best solutions below

1
ntg On

It can be done using the not-so-well-documented run_cell_magic

run_cell_magic(magic_name, line, cell) Execute the given cell magic. Parameters:
magic_name : str Name of the desired magic function, without ‘%’ prefix. line : str The rest of the first input line as a single string. cell : str The body of the cell as a (possibly multiline) string.

So the code for this transformed for e.g. a python script is:

from IPython import get_ipython
task_name = 'foobar'
get_ipython().run_cell_magic('sh', '-s $task_name', '''
#!/bin/bash
task_name=$1
echo This is a script running task [$task_name] that is executed from python
echo Many more bash commands here...................
''')