Mohamed Gabr Mohamed Gabr - 2 years ago 589
Python Question

Run Python Script into Flask

I've a desktop application to detect faces written in python script, using opencv and numpy.
i want to put these python files into flask and run it, would it run without problems? like

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

## define my functions here

def hello_world():
return 'Hello World!'

if __name__ == '__main__':
#call the functions here

would that work? if not how do i get it included? thanks

or shall i try blueprints? it works with html and javascript files, would it work with python files?

Answer Source

Yes it would work, one thing you should know is if you do like below, the HTTP request won't return until after the processing is done e.g.

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

Otherwise, if you put in the main function like below

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)

Then your flask app won't start until the init/processing is done.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download