from flask import Flask, render_template, request
import subprocess
from subprocess import Popen, PIPE
from subprocess import check_output

def get_shell_script_output_using_communicate(script, params):
    
    session = Popen([script,params], stdout=PIPE, stderr=PIPE)
    stdout, stderr = session.communicate()
    if stderr:
        raise Exception("Error "+str(stderr))
    return stdout.decode('utf-8')

def get_shell_script_output_using_check_output(script, params):
    
    stdout = check_output([script,params]).decode('utf-8')
    return stdout

app = Flask(__name__)

#home page
@app.route("/") #Change the route to home
def home():
    script_list = get_shell_script_output_using_check_output('ls','./resource')
    return render_template("home.html",script_list=script_list)

@app.route("/", methods=['GET','POST'])
def func_test():
    if request.method == 'POST':
        script_list = get_shell_script_output_using_check_output('ls','./resource')
        scriptname = request.form['scriptname']
        script = "./resource/"+scriptname
        #script = "./resource/date.sh"
        params = request.form['scriptparams']
        stdout = get_shell_script_output_using_communicate(script, params)
        return render_template('home.html',script_list=script_list ,script=scriptname, params = params, output=stdout)

#about page
@app.route("/about") #Change the route to about
def about():
    return render_template("about.html")
    
if __name__ == "__main__":
    app.run(debug=True)

from flask import Flask, render_template, request
import subprocess
from subprocess import Popen, PIPE
from subprocess import check_output

def get_shell_script_output_using_communicate(script, params):
    
    session = Popen([script,params], stdout=PIPE, stderr=PIPE)
    stdout, stderr = session.communicate()
    if stderr:
        raise Exception("Error "+str(stderr))
    return stdout.decode('utf-8')

def get_shell_script_output_using_check_output(script, params):
    
    stdout = check_output([script,params]).decode('utf-8')
    return stdout

app = Flask(__name__)

#home page
@app.route("/") #Change the route to home
def home():
    script_list = get_shell_script_output_using_check_output('ls','./resource')
    return render_template("home.html",script_list=script_list)

@app.route("/", methods=['GET','POST'])
def func_test():
    if request.method == 'POST':
        script_list = get_shell_script_output_using_check_output('ls','./resource')
        scriptname = request.form['scriptname']
        script = "./resource/"+scriptname
        #script = "./resource/date.sh"
        params = request.form['scriptparams']
        stdout = get_shell_script_output_using_communicate(script, params)
        return render_template('home.html',script_list=script_list ,script=scriptname, params = params, output=stdout)

#about page
@app.route("/about") #Change the route to about
def about():
    return render_template("about.html")
    
if __name__ == "__main__":
    app.run(debug=True)

#home page #Change the route to home #script = "./resource/date.sh" #about page #Change the route to about