python - Running shell script on flask -


my app has been setup following app.py file , 2 .html files: index.html (base template), , upload.html client can see images uploaded. problem have that, want program (presumable app.py) execute matlab function before user redirected upload.html template. i've found q&a's how run bash shell commands on flask (yet not command), haven't found 1 scripts.

a workaround got create shell script: hack.sh run matlab code. in terminal straight forward:

$bash hack.sh 

hack.sh:

nohup matlab -nodisplay -nosplash -r run_image_alg > text_output.txt & 

run_image_alg matlab file (run_image_alg.m)

here code app.py:

import os  flask import flask, render_template, request, redirect, url_for, send_from_directory werkzeug import secure_filename  # initialize flask application  app = flask(__name__)  # th path upload directory app.config['upload_folder'] = 'uploads/'  # these extension accepting uploaded app.config['allowed_extensions'] = set(['png','jpg','jpeg'])  # given file, return whether it's allowed type or not def allowed_file(filename):   return '.' in filename , \     filename.rsplit('.',1)[1] in app.config['allowed_extensions']  # route show form perform ajax request # jquery loaded execute request , update  # value of operation  @app.route('/') def index():   return render_template('index.html')  #route process file upload @app.route('/upload',methods=['post']) def upload():   uploaded_files = request.files.getlist("file[]")   filenames = []   file in uploaded_files:     if file , allowed_file(file.filename):       filename = secure_filename(file.filename)       file.save(os.path.join(app.config['upload_folder'],filename))       filenames.append(filename)    print uploaded_files    #run bash script here.     return render_template('upload.html',filenames=filenames)  @app.route('/uploads/<filename>') def uploaded_file(filename):   return send_from_directory(app.config['upload_folder'],filename)   if __name__ == '__main__':   app.run(     host='0.0.0.0',     #port=int("80"),     debug=true   ) 

i might presumably missing library? found similar q&a on stackoverflow wanted run (known) shell command ($ls -l). case different since it's not known command, created script:

from flask import flask import subprocess  app = flask(__name__)  @app.route("/")  def hello():     cmd = ["ls","-l"]     p = subprocess.popen(cmd, stdout = subprocess.pipe,                             stderr=subprocess.pipe,                             stdin=subprocess.pipe)     out,err = p.communicate()     return out if __name__ == "__main__" :     app.run() 

if want run matlab, change command to

cmd = ["matlab", "-nodisplay", "-nosplash", "-r", "run_image_alg"] 

if want redirect output file:

with open('text_output.txt', 'w') fout:     subprocess.popen(cmd, stdout=fout,                           stderr=subprocess.pipe,                           stdin=subprocess.pipe) 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -