将Python脚本运行到Flask中

时间:2016-04-28 16:11:25

标签: python flask

我有一个桌面应用程序来检测用python脚本编写的面部,使用opencv和numpy。 我想把这些python文件放入烧瓶并运行它,它会运行没有问题吗?喜欢

import cv2
import numpy as np
from flask import Flask
app = Flask(__name__)

## define my functions here

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    #call the functions here
    app.run()
那会有用吗?如果不是我怎么把它包括在内?谢谢!

1 个答案:

答案 0 :(得分:1)

是的,它会起作用,你应该知道的一件事是,如果你喜欢下面的话,HTTP请求将不会在处理完成之后返回,例如。

@app.route('/webcam')
def webcam_capture():
    """
    Returns a picture snapshot from the webcam
    """
    image = cv2... # call a function to get an image

    response = make_response(image) # make an HTTP response with the image
    response.headers['Content-Type'] = 'image/jpeg'
    response.headers['Content-Disposition'] = 'attachment; filename=img.jpg'

    return response

否则,如果你输入如下的主要功能

if __name__ == '__main__':
    # <-- No need to put things here, unless you want them to run before 
    # the app is ran (e.g. you need to initialize something)

    app.run()

然后你的烧瓶应用程序将无法启动,直到初始化/处理完成。