Mohamed Gabr Mohamed Gabr - 7 months ago 372
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

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

if __name__ == '__main__':
#call the functions here
app.run()


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

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.

@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

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)

    app.run()

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

Comments