Live stdout output from Python subprocess in Jupyter notebook
<p>I'm using subprocess to run a command line program from a Python (3.5.2) script, which I am running in a Jupyter notebook. The subprocess takes a long time to run and so I would like its stdout to be printed live to the screen in the Jupyter notebook.</p>

<p>I can do this no problem in a normal Python script run from the terminal. I do this using:</p>

<pre><code>def run_command(cmd):
from subprocess import Popen, PIPE
import shlex

with Popen(shlex.split(cmd), stdout=PIPE, bufsize=1, universal_newlines=True) as p:
    for line in p.stdout:
        print(line, end='')
    exit_code = p.poll()
return exit_code
</code></pre>

<p>However, when I run the script in a Jupyter notebook, it does not print the stdout live to the screen. Instead, it prints everything after the subprocess has finished running.</p>

<p>Does anyone have any ideas on how to remedy this?</p>

<p>Many thanks,
Johnny</p>