Displaying and writing standard at the same time in IPython?

I am interested in implementing behavior in IPython that would be like a combination of !and !!. I am trying to use an IPython terminal as a complement to my (Windows) shell. For a long command (e.g. build script), I would like to see the output when it passes threads, as it does !. I would also like to capture the output of the command into the output history of how !!, but this delays printing until the entire output is available.

Does anyone have any suggestions on how to implement something like this? I suppose an object would be useful here IPython.utils.io.Tee(), but I don’t know enough about how IPython correctly picked it up.

+4
source share
1 answer

Here is a piece of code that I just tried in iPython notebook v2.3 that seems to do what was requested:

import sys
import IPython.utils.io
outputstream = IPython.utils.io.Tee("outputfile.log", "w", channel="stdout")
outputstream.write("Hello worlds!\n")
outputstream.close()

logstream=open("outputfile.log", "r")
sys.stdout.write("Read back from log file:\n")
sys.stdout.write(logstream.read())

The log file is created in the same directory as the iPython laptop file, and the output from this cell is displayed as follows:

Hello worlds!
Read back from log file:
Hello worlds!

I have not tried this in the iPython terminal, but I see no reason why it will not work.

(Researched and answered as part of Oxford's participation at http://aaronswartzhackathon.org )

0
source

All Articles