Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe Ipython magic output to a variable?

I want to run a bash script in my ipython Notebook and save the output as a string in a python variable for further manipulation. Basically I want to pipe the output of the bash magic to a variable, For example the output of something like this:

%%bash some_command [options] foo bar 
like image 277
Gioelelm Avatar asked Dec 20 '13 12:12

Gioelelm


People also ask

What is %% capture in Python?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.

What does %% do in Jupyter Notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.

What is %% Writefile in Jupyter Notebook?

%%writefile lets you output code developed in a Notebook to a Python module. The sys library connects a Python program to the system it is running on. The list sys. argv contains the command-line arguments that a program was run with.

How do I export output from Jupyter?

2. Download Jupyter Notebook as PDF. The Jupyter Notebook has an option to export the notebook to many formats. It can be accessed by clicking File -> Download as -> PDF via LaTeX (or PDF via HTML - not visible in the screenshot).


2 Answers

What about using this:

myvar = !some_command --option1 --option2 foo bar 

instead of the %%bash magic? Using the ! symbol runs the following command as a shell command, and the results are all stored in myvar. For running multiple commands and collecting the output of all of them, just put together a quick shell script.

like image 194
MattDMo Avatar answered Sep 20 '22 17:09

MattDMo


For completeness, if you would still like to use the %%bash cell magic, you can pass the --out and --err flags to redirect the outputs from the stdout and stderr to a variable of your choice.

From the doucumentation:

%%bash --out output --err error echo "hi, stdout" echo "hello, stderr" >&2 

will store the outputs in the variables output and error so that:

print(error) print(output) 

will print to the python console:

hello, stderr hi, stdout 
like image 24
oLas Avatar answered Sep 21 '22 17:09

oLas