Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython: quickly dump the output of a cell to file

Tags:

ipython

It is very easy to save an ipython notebook cell or several cells to a file or to pastebin.

But sometimes I want to dump the output of some operation to file. What's the fast way of doing that?

%save output Out[56]

gives me:

Out[56] is neither a string nor a macro.

And if I do:

 %save output str(Out[56])

It works but I lose the pretty-print formatting, and have an automatic .py extension added to the output file name.

like image 642
Yoav Avatar asked Jul 16 '13 20:07

Yoav


People also ask

What does Clear_output () do in Python?

clear_output to clear the output of a cell.

What does %% capture do?

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. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

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.


1 Answers

to keep pretty-print formatting try

%save output repr(_56)

if an object you return provides __str__ and __repr__ methods it might help

extention .py or .ipy is hardcoded in the IPython magics core. So it is easier to rename each time. or if you are not planning to update IPython, change in ...\IPython\core\magics\code.py , line 188:

    if not fname.endswith((u'.py',u'.ipy')):
        pass #fname += ext
like image 147
sherdim Avatar answered Oct 24 '22 22:10

sherdim