1

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?

ntg
  • 12,950
  • 7
  • 74
  • 95
  • Part of it is that there's several ways to do this depending on what output is produced or the extent of feed back needed. One option is to package the shell code to a script and then use `os.system()` to call it. Probably though because you want to provide an arguments/variables, you probably want [subprocess.call](https://stackoverflow.com/a/14892402/8508004). Also see suggestions steering people to that [here](https://stackoverflow.com/questions/22101931/passing-more-than-one-variables-to-os-system-in-python). – Wayne Dec 15 '22 at 17:49
  • 1
    yes, but wanted to find a "general" way to execute %%magic commands (double %).... The %%sh is an example... – ntg Dec 16 '22 at 12:02

1 Answers1

1

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...................
''')
ntg
  • 12,950
  • 7
  • 74
  • 95