Unsuccessful Flask app deployment on EC2 instance

Issue

I am trying to deploy a flask app on an EC2 instance but running into the following error:
enter image description here

The directory structure of my app is as follows:

/var/www/html/ImgClassApp/
        |--ImgClassApp.wsgi
        |--1000_imagenet_classes_file.txt
        |--app.py
        |--app_helper.py
        |--requirements.txt
        |--/static
           |--/css
           |--/uploads
        |--/templates
           |--index.html
           |--layout.html
           |--upload.html

Contents of ImgClassApp.wsgi are as follows:

import sys
sys.path.insert(0, '/var/www/html/ImgClassApp')
from ImgClassApp import app as application

And finally, contents of app.py are as follows:

from flask import Flask, request, render_template
from werkzeug.utils import secure_filename
import os
from app_helper import *

# Define a flask app
app = Flask(__name__)

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


@app.route('/uploader', methods = ['POST'])
def upload_file():
    predictions=""

    if request.method == 'POST':
        f = request.files['file']

        # Save the file to ./uploads
        basepath = os.path.dirname(__file__)
        file_path = os.path.join(basepath, 'static','uploads', secure_filename(f.filename))
        f.save(file_path)

        predictions = get_classes(file_path)
        pred_strings = []
        for _,pred_class,pred_prob in predictions:
            pred_strings.append(str(pred_class).strip()+" : "+str(round(pred_prob,5)).strip())
        preds = ", ".join(pred_strings)
        print("preds:::",preds)
    return render_template("upload.html", predictions=preds, display_image=f.filename) 


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

Solution

The best way to address the issue would be to reorganize directory structure a bit.
In fact if you just move the file ImgClassApp.wsgi to the upper directory, this should work and it will be able to import from directory ImgClassApp, that is just underneath.

No need to mess up with the Python path. Python already includes your application working directory in its search path.

Answered By – Anonymous

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published