如何在网页中连续显示Python输出?

时间:2013-02-26 15:30:08

标签: python-2.7 flask

我希望能够访问网页,它将运行python函数并在网页中显示进度。

因此,当您访问该网页时,您可以看到脚本的输出,就像您从命令行运行它一样。

根据这里的答案

How to continuously display python output in a webpage?

我正在尝试显示 PYTHON

的输出

我正在尝试使用Markus Unterwaditzer的代码和python函数。

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)

它运行但我在浏览器中看不到任何内容。

2 个答案:

答案 0 :(得分:23)

你看起来好像不想调用测试函数,而是一个提供输出的实际命令行进程。还可以从proc.stdout.readline或其他东西创建一个iterable。你也从Python中说过我忘了包含你应该在子进程中提取你想要的任何python代码并将它放在一个单独的文件中。

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')

答案 1 :(得分:1)

这是一个允许您流式传输子流程输出的解决方案。事后使用相同的模板静态加载它(假设您的子进程将它自己的输出记录到文件中;如果没有,则将进程输出记录到日志文件中留给读者练习)

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())

通过这种方式,您可以在命令的初始运行期间(通过POST)和在事实之后静态提供保存的日志文件时使用一致的模板。