2

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?

2
  • 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. Also see suggestions steering people to that here. Commented Dec 15, 2022 at 17:49
  • 1
    yes, but wanted to find a "general" way to execute %%magic commands (double %).... The %%sh is an example... Commented Dec 16, 2022 at 12:02

1 Answer 1

3

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...................
''')
Sign up to request clarification or add additional context in comments.

1 Comment

More documentation can be found in Defining custom magics — IPython 8.17.2 documentation .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.