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

2024-06-07 00:30:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小瓶子应用程序,它接受用户输入并返回一些文本。在这里,用户输入被输入到另一个python脚本,比如temp.py,这个temp.py将返回一个应该返回给用户的值。例如:

烧瓶.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


Tags: andtheto用户namefrompyapp
1条回答
网友
1楼 · 发布于 2024-06-07 00:30:18

在函数中包装temp.py正在执行的任何操作。将它放在与flask.py相同的目录中。在flask.py中调用import temp,然后使用temp.myfunction()

相关问题 更多 >