Home:ALL Converter>How to continuously display Python output in a Webpage?

How to continuously display Python output in a Webpage?

Ask Time:2013-02-26T23:30:08         Author:Siecje

Json Formatter

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.

Author:Siecje,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15092961/how-to-continuously-display-python-output-in-a-webpage
Drew Larson :

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.\n\nimport flask\nimport subprocess\nimport time #You don't need this. Just included it so you can see the output stream.\n\napp = flask.Flask(__name__)\n\[email protected]('/yield')\ndef index():\n def inner():\n proc = subprocess.Popen(\n ['dmesg'], #call something with a lot of output so we can see it\n shell=True,\n stdout=subprocess.PIPE\n )\n\n for line in iter(proc.stdout.readline,''):\n time.sleep(1) # Don't need this just shows the text streaming\n yield line.rstrip() + '<br/>\\n'\n\n return flask.Response(inner(), mimetype='text/html') # text/html is required for most browsers to show th$\n\napp.run(debug=True, port=5000, host='0.0.0.0')\n",
2013-03-12T06:04:05
yy