ayrton_senna
ayrton_senna

Reputation: 163

how to split lines of the output of subprocess in Flask webapp

How to split the output of a script and display in flask webapp ?

from flask import Flask, render_template, redirect, url_for, request
import subprocess
import os
import datetime
import time
app = Flask(__name__)

def api():
    cmd = ["/usr/dummy.ksh"]
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, 
    stderr=subprocess.PIPE, universal_newlines=True)                    
    stdout, stderr = p.communicate()
    return stdout
@app.route("/")
def index():
    return render_template('subprocess.html', subprocess_output=api())


if __name__ == "__main__":
   app.run(host='0.0.0.0', debug=True)

"I expect on the flask app to see the stdout of the dummy script line by line. However, im getting all the output in a single string.

Desired output in flask web page:

Script completed step 1
script completed step 2
all done...exiting

Current output in flask web page:

Script completed step 1 script completed step 2 all done...exiting

Upvotes: 0

Views: 929

Answers (1)

Selcuk
Selcuk

Reputation: 59415

  1. You can wrap the output in <pre>...</pre> tags.

  2. You can add HTML line breaks to your output, for example:

    return "<br/>".join(stdout.splitlines())

Upvotes: 1

Related Questions