32

I want to be able to visit a webpage and it will run a python function and display the progress in the webpage.

So when you visit the webpage you can see the output of the script as if you ran it from the command line.

Based on the answer here

How to continuously display python output in a webpage?

I am trying to display output from PYTHON

I am trying to use Markus Unterwaditzer's code with a python function.

import flask
import subprocess

app = flask.Flask(__name__)

def test():
    print "Test"

@app.route('/yield')
def index():
    def inner():
        proc = subprocess.Popen(
            test(),
            shell=True,
            stdout=subprocess.PIPE
        )

        while proc.poll() is None:
            yield proc.stdout.readline() + '<br/>\n'
    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show the partial page immediately

app.run(debug=True, port=5005)

And it runs but I don't see anything in the browser.

Community
  • 1
  • 1
Siecje
  • 3,594
  • 10
  • 32
  • 50

2 Answers2

34

Hi looks like you don't want to call a test function, but an actual command line process which provides output. Also create an iterable from proc.stdout.readline or something. Also you said from Python which I forgot to include that you should just pull any python code you want in a subprocess and put it in a separate file.

import flask
import subprocess
import time          #You don't need this. Just included it so you can see the output stream.

app = flask.Flask(__name__)

@app.route('/yield')
def index():
    def inner():
        proc = subprocess.Popen(
            ['dmesg'],             #call something with a lot of output so we can see it
            shell=True,
            stdout=subprocess.PIPE
        )

        for line in iter(proc.stdout.readline,''):
            time.sleep(1)                           # Don't need this just shows the text streaming
            yield line.rstrip() + '<br/>\n'

    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show th$

app.run(debug=True, port=5000, host='0.0.0.0')
Drew Larson
  • 710
  • 7
  • 12
  • Whoops I changed the host so it worked in my vagrant environment. – Drew Larson Mar 12 '13 at 06:04
  • OMG, I have tried so many things to get this working and this is the only one that did! – JeffThompson Aug 11 '15 at 16:26
  • How would I pass the Response to a specific point in a template? – JeffThompson Aug 11 '15 at 16:27
  • @JeffThompson, as far as I know you'd need to alter `inner()` to iterate over the lines of the first half of your template and `yield` those before `yield`ing the subprocess, and then afterwards iterate over the lines of the last half and `yield` those as well. Then your process output will appear inserted into your template. – robru Feb 22 '16 at 20:07
  • @DrewLarson I got this to work, but at least in python3 I needed to do `iter(proc.stdout.readline, b'')` as subprocess readline produces `bytes`, not `str` – robru Feb 22 '16 at 20:08
  • @JeffThompson you may enjoy the answer I just posted: http://stackoverflow.com/a/35640692/1423157 – robru Feb 25 '16 at 23:47
  • 4
    In Python 3, adding universal_newlines=True to the subprocess.Popen call will result in string values coming back from readline() instead of bytes. – biomiker Apr 27 '16 at 05:44
  • Had an issue when I tried to call an external Python script instead of "dmesg" directly. See question here: http://stackoverflow.com/questions/40388238/popen-in-flask-starts-python-interpreter – ABM Nov 02 '16 at 19:31
  • The `line.rstrip()` function raise an error in Python 3, we'll need to `encode` it (`utf-I`), I can't get this example working to output json using ajax call. – Ibrahim.H Jan 04 '22 at 17:59
6

Here's a solution that allows you to stream the subprocess output & load it statically after the fact using the same template (assuming that your subprocess records it's own output to a file; if it doesn't, then recording the process output to a log file is left as an exercise for the reader)

from flask import Response, escape
from yourapp import app
from subprocess import Popen, PIPE, STDOUT

SENTINEL = '------------SPLIT----------HERE---------'
VALID_ACTIONS = ('what', 'ever')

def logview(logdata):
    """Render the template used for viewing logs."""
    # Probably a lot of other parameters here; this is simplified
    return render_template('logview.html', logdata=logdata)

def stream(first, generator, last):
    """Preprocess output prior to streaming."""
    yield first
    for line in generator:
        yield escape(line.decode('utf-8'))  # Don't let subproc break our HTML
    yield last

@app.route('/subprocess/<action>', methods=['POST'])
def perform_action(action):
    """Call subprocess and stream output directly to clients."""
    if action not in VALID_ACTIONS:
        abort(400)
    first, _, last = logview(SENTINEL).partition(SENTINEL)
    path = '/path/to/your/script.py'
    proc = Popen((path,), stdout=PIPE, stderr=STDOUT)
    generator = stream(first, iter(proc.stdout.readline, b''), last)
    return Response(generator, mimetype='text/html')

@app.route('/subprocess/<action>', methods=['GET'])
def show_log(action):
    """Show one full log."""
    if action not in VALID_ACTIONS:
        abort(400)
    path = '/path/to/your/logfile'
    with open(path, encoding='utf-8') as data:
        return logview(logdata=data.read())

This way you get a consistent template used both during the initial running of the command (via POST) and during static serving of the saved logfile after the fact.

robru
  • 2,313
  • 2
  • 29
  • 38