2013-02-26 16 views
17

私はウェブページにアクセスしたいと思うし、それはPythonの機能を実行し、ウェブページの進捗状況を表示します。WebページにPython出力を継続的に表示するにはどうすればいいですか?

ウェブページにアクセスすると、スクリプトの出力をコマンドラインから実行したように見ることができます。

ここでの回答に基づいて

How to continuously display python output in a webpage?

私はPythonの関数とマルクス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) 

これは実行されますが、ブラウザには何も表示されません。

答えて

20

あなたはテスト機能を呼びたくないようですが、出力を提供する実際のコマンドラインプロセスです。また、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') 
+0

おっと私は迷惑な環境で働くようにホストを変更しました。 –

+0

OMG、私はこの作業をするために非常に多くのことを試みましたが、これは唯一のものでした! – JeffThompson

+0

レスポンスをテンプレートの特定のポイントに渡すにはどうすればよいですか? – JeffThompson

0

ここにあなたのサブプロセスは、それがファイルに自身の出力の記録と仮定して(同じテンプレートを使用して、あなたが実際にした後、静的サブプロセスの出力&負荷にそれをストリーミングすることを可能にするソリューションですログファイルへのプロセス出力は、読者の練習)として残され

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経由でコマンドの最初の実行中に、保存されたの静的なサービス提供中の両方に使用一貫性のテンプレートを取得します。この方法事実の後のログファイル。

関連する問題