[英]Running shell script on flask
My app has been setup with the following app.py file and two .html files: index.html (base template), and upload.html where the client can see the images that he just uploaded. 我的应用程序已使用以下app.py文件和两个.html文件进行设置:index.html(基本模板)和upload.html,其中客户端可以看到他刚刚上传的图像。 The problem I have is that, I want my program (presumable app.py) to execute a matlab function before the user is redirected to the upload.html template.
我遇到的问题是,我希望我的程序(presumable app.py)在用户重定向到upload.html模板之前执行matlab功能。 I've found Q&A's about how to run bash shell commands on flask (yet this is not a command), but I haven't found one for scripts.
我发现Q&A是关于如何在flask上运行bash shell命令(但这不是命令),但我还没有找到一个脚本。
A workaround that I got was to create a shell script: hack.sh that will run the matlab code. 我得到的解决方法是创建一个shell脚本:hack.sh,它将运行matlab代码。 In my terminal this is straight forward:
在我的终端中,这是直截了当的:
$bash hack.sh
hack.sh: hack.sh:
nohup matlab -nodisplay -nosplash -r run_image_alg > text_output.txt &
run_image_alg is my matlab file (run_image_alg.m) run_image_alg是我的matlab文件(run_image_alg.m)
Here is my code for app.py: 这是我的app.py代码:
import os
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename
# Initialize the Flask application
app = Flask(__name__)
# This will be th path to the upload directory
app.config['UPLOAD_FOLDER'] = 'uploads/'
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['png','jpg','jpeg'])
# For a given file, return whether it's an allowed type or not
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.',1)[1] in app.config['ALLOWED_EXTENSIONS']
# This route will show a form to perform an AJAX request
# jQuery is loaded to execute the request and update the
# value of the operation
@app.route('/')
def index():
return render_template('index.html')
#Route that will process the file upload
@app.route('/upload',methods=['POST'])
def upload():
uploaded_files = request.files.getlist("file[]")
filenames = []
for file in uploaded_files:
if file and 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 be missing a library? 我可能错过了一个图书馆? I found a similar Q&A on stackoverflow where someone wanted to run a (known) shell command ($ls -l).
我在stackoverflow上发现了一个类似的Q&A,其中有人想运行(已知的)shell命令($ ls -l)。 My case is different since it's not a known command, but a 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 you want to run matlab, just change your command to 如果您想运行matlab,只需将命令更改为
cmd = ["matlab", "-nodisplay", "-nosplash", "-r", "run_image_alg"]
If you want to redirect the output to a file: 如果要将输出重定向到文件:
with open('text_output.txt', 'w') as fout:
subprocess.Popen(cmd, stdout=fout,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.