简体   繁体   English

如何在flask后端运行python脚本?

[英]How to run a python script on flask backend?

I have a small flask app where it takes user input and returns some text. 我有一个小瓶子应用程序,它需要用户输入并返回一些文本。 Here the user input is fed to another python script say temp.py and this temp.py will return a value which should be returned to user. 这里用户输入被送到另一个python脚本说temp.py,这个temp.py将返回一个应该返回给用户的值。 For eg: 例如:

flask.py flask.py

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/')
def result():
    return render_template("index.html")

@app.route('/getconfig', methods=['GET', 'POST'])
def config():
    value = request.form['config']
    print ("This is the user value :  ", value);
    // This value is written to a file say x.pol and my temp.py reads from x.pol file and writes the output to y.pol. I'm not sure how to trigger the script like "python temp.py" on the server.
    // The reason I'm doing this because I don't want to tweak the third party tool where, the third party tool reads from input files and generates output files. 

    return content_generated_by_temp.py // by reading y.pol.

if __name__ == '__main__':
   app.run(debug = True)

By the way, temp.py is itself a nightmare. 顺便说一句,temp.py本身就是一场噩梦。 I understood what the tool does. 我理解这个工具的功能。 For more info about the tool: Google Caprica 有关该工具的更多信息: Google Caprica

Wrap whatever temp.py is doing in a function. 包装temp.py正在函数中执行的任何temp.py Place it in the same directory as flask.py . 将它放在与flask.py相同的目录中。 Call import temp in flask.py , then use temp.myfunction() flask.py调用import temp ,然后使用temp.myfunction()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM