[英]Unsuccessful Flask app deployment on EC2 instance
我正在嘗試在 EC2 實例上部署 flask 應用程序,但遇到以下錯誤:
我的應用程序的目錄結構如下:
/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
ImgClassApp.wsgi的內容如下:
import sys
sys.path.insert(0, '/var/www/html/ImgClassApp')
from ImgClassApp import app as application
最后,app.py 的內容如下:
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)
解決該問題的最佳方法是稍微重新組織目錄結構。 事實上,如果你只是將文件 ImgClassApp.wsgi 移動到上層目錄,這應該可以工作,它可以從目錄 ImgClassApp 導入,就在下面。
無需弄亂 Python 路徑。 Python 已經在其搜索路徑中包含了您的應用程序工作目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.