Search code examples
pythonjupyter-notebookoutputcapture

How to capture jupyter cell output without supressing live printing?


In jupyter there is a magic function that allows to capture and parse output of a cell:

%%capture capt_out
# cell body that prints interesting stuff every second
# another cell
# code for printing or processing output (i.e for plots):
print(capt_out)

The problem is, I need to see the output live (because this is a Darknet model training and I need to know how much progress has been made) and then parse it to create plots based on it.

Is there an easy way to capture the output without suppressing printing?


Solution

  • This is not a general solution, but works for my situation:

    !./darknet detector some_parameters 2>&1 | tee some_file.txt
    

    Since my jupyter cell runs a binary through bash, I can simply use tee command to save output to file without suppressing live printing. Then I read this file with python

    with open(some_file.txt, r) as f:
       text = f.read()
    

    2>&1 redirects stderr to stdout.