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

标签 python flask

我有一个小型 Flask 应用程序,它接受用户输入并返回一些文本。在这里,用户输入被提供给另一个 python 脚本,比如 temp.py,这个 temp.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)

顺便说一下,temp.py 本身就是一场噩梦。我了解该工具的作用。有关该工具的更多信息:Google Caprica

最佳答案

temp.py 正在做的任何事情包装在一个函数中。将它放在与 flask.py 相同的目录中。在flask.py中调用import temp,然后使用temp.myfunction()

关于python - 如何在 flask 后端运行 python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45553629/

相关文章:

html - Flask - 没有正确响应 GET 请求?

Python Flask 网络服务器停止响应

python - Django 和 'virtualenv' - 正确的项目结构

python - 列表超出范围,为什么?

python - 如何使用异步 for 循环遍历列表?

python - 在 GTK 中缩放图像

python - turtle (及其屏幕)出现在 scipy/anaconda/spyder (2.7) 中的哪里?

python - Flask:ModuleNotFoundError 使用 `flask run` 但能够使用 `python app.py` 运行应用程序

python - 当列表长度不同时在 Python 中迭代多个列表

python - Flask 错误 werkzeug 路由故障排除