如何在烧瓶中的后端调用python脚本 [英] How to call a python script in backend in flask

查看:144
本文介绍了如何在烧瓶中的后端调用python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在烧瓶应用程序中调用一个复杂的python脚本,但是现在还不能完全确定如何去做。如果它很简单,我只是复制和脚本与烧瓶路由器粘贴,但是会太乱了。到目前为止,我有这个为我的main.py。我想调用一个脚本temp.py。

  from flask import Flask,render_template,request $ b $ app = Flask __name__)

@ app.route('/')
def base():
return render_template(base.html)


@ app.route('/ temp',methods = ['GET','POST'])
def temp():

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


解决方案<您可以使用子进程在命令行调用脚本

 导入子进程
进程= subprocess.Popen (['python','temp.py'],stdout = subprocess.PIPE)
out,err = process.communicate()
print(out)

它将提供输出或错误(如果有的话)



这样看:

python 2:
https://docs.python.org/2/library/subpr ocess.html



python 3:
https://docs.python.org/3/library/subprocess.html


I'm trying to call a complicated python script in a flask app, but not entirely sure on how to do it right now. If it was simple I would just copy and paste the script with a router in flask, but that would get too messy. So far I have this for my main.py. I want to call a script temp.py.

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

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


@app.route('/temp', methods=['GET', 'POST'])
def temp():


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

解决方案

You can use subprocess for calling script in command line

import subprocess
process = subprocess.Popen(['python' , 'temp.py' ], stdout=subprocess.PIPE)
out, err = process.communicate()
print(out)

it will provide the output or error(if its there)

for more info on this look for:

python 2: https://docs.python.org/2/library/subprocess.html

python 3: https://docs.python.org/3/library/subprocess.html

这篇关于如何在烧瓶中的后端调用python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆