2013-02-26 22 views
17

मैं एक वेबपेज पर जाने में सक्षम होना चाहता हूं और यह एक पायथन फ़ंक्शन चलाएगा और वेबपृष्ठ में प्रगति प्रदर्शित करेगा।वेबपृष्ठ में पाइथन आउटपुट को लगातार प्रदर्शित कैसे करें?

तो जब आप वेबपेज पर जाते हैं तो आप स्क्रिप्ट के आउटपुट को देख सकते हैं जैसे कि आप इसे कमांड लाइन से चलाते हैं।

यहाँ जवाब के आधार पर

How to continuously display python output in a webpage?

मैं अजगर

मैं एक अजगर समारोह के साथ मार्कस Unterwaditzer के कोड का उपयोग करने की कोशिश कर रहा हूं, उनसे उत्पादन प्रदर्शित करने के लिए कोशिश कर रहा हूँ।

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 बनाएँ। इसके अलावा आपने पाइथन से कहा था कि मैं यह शामिल करना भूल गया था कि आपको किसी भी पाइथन कोड को केवल एक उपप्रजाय में खींचना चाहिए और इसे एक अलग फ़ाइल में रखना चाहिए।

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

ओएमजी, मैंने यह काम करने के लिए इतनी सारी चीजों की कोशिश की है और यह केवल यही है! – 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()) 

इस तरह आप दोनों (पोस्ट के माध्यम से) आदेश के प्रारंभिक चल दौरान और बचाया के स्थिर सेवारत के दौरान इस्तेमाल के लिए एक सुसंगत टेम्पलेट मिल तथ्य के बाद logfile।

संबंधित मुद्दे