31

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

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

根据这里的答案

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

我正在尝试显示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)

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

4

2 回答 2

33

嗨,您似乎不想调用测试函数,而是要调用提供输出的实际命令行进程。还可以从 proc.stdout.readline 或其他东西创建一个迭代。您还从 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')
于 2013-03-12T06:04:05.857 回答
4

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

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)和事后静态服务保存的日志文件期间获得一致的模板。

于 2016-02-25T23:46:36.300 回答