【问题标题】:My flask app is not working fine in heroku Can you help me?我的烧瓶应用程序在 heroku 中运行不正常你能帮帮我吗?
【发布时间】:2020-09-05 05:15:42
【问题描述】:

我的烧瓶应用程序在本地主机中运行良好,但在 heroku 中无法运行。Procfile 也已添加。我什至尝试过 web: gunicorn --bind 0.0.0.0:$PORT app:app this in Procfile.txt app.py 包括

from flask import Flask,request, redirect,render_template,jsonify
from werkzeug.utils import secure_filename
import xlrd
import pymongo

import os
from pymongo import MongoClient

app = Flask(__name__)

app.config["IMAGE_UPLOADS"] = "./static"
app.config["ALLOWED_IMAGE_EXTENSIONS"] = ["JPEG", "JPG", "PNG", "GIF","XLSX"]
app.config["MAX_IMAGE_FILESIZE"] = 0.5 * 1024 * 1024
client = MongoClient("mongodb+srv://hemanth:hemanth123@cluster0-pi4b7.mongodb.net/test?retryWrites=true&w=majority")
db = client["creditscore"]
collection = db["creditscore"]

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

@app.route("/api")
def api():
    return jsonify({'status':os.getcwd()})

@app.route("/upload", methods=["GET", "POST"])
def upload_image():
    if request.method == "POST":
        if request.files:
            image = request.files["image"]
            filename = secure_filename(image.filename)
            image.save(os.path.join(app.config["IMAGE_UPLOADS"], filename))
            file_location=os.path.join(app.config["IMAGE_UPLOADS"], filename)
            workbook = xlrd.open_workbook(file_location)
            sheet = workbook.sheet_by_index(0)
            psitrnid = int(sheet.cell_value(9,4))
            psiootid  = int(sheet.cell_value(9,5))
            goodtrnid = int(sheet.cell_value(9,7))
            badtrnid = int(sheet.cell_value(9,8))
            goodootid = int(sheet.cell_value(9,10))
            badootid = int(sheet.cell_value(9,11))
            print("Image saved")
            driverIds={
                "driverNames":"Nikhil",
                "psitrnid":psitrnid,
                "psiootid":psiootid,
                "goodtrnid":goodtrnid,
                "badtrnid":badtrnid,
                "goodootid":goodootid,
                "badootid":badootid
                }
            trn_id = { "_id": psitrnid }
            trn_insert = {**trn_id , **driverIds}
            collection.insert_one(trn_insert)
            return jsonify({"status":"Worked",'driverIds':driverIds})
        else:
            print("That file extension is not allowed")
            return jsonify({"status":"file extension not allwed"})
    return render_template("layouts/index1.html")

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

heroku 日志显示

2020-05-19T07:49:30.575609+00:00 app[api]: Initial release by user nikhilkumarthota42@gmail.com
2020-05-19T07:49:30.575609+00:00 app[api]: Release v1 created by user nikhilkumarthota42@gmail.com
2020-05-19T07:49:30.684285+00:00 app[api]: Release v2 created by user nikhilkumarthota42@gmail.com
2020-05-19T07:49:30.684285+00:00 app[api]: Enable Logplex by user nikhilkumarthota42@gmail.com
2020-05-19T07:50:10.000000+00:00 app[api]: Build started by user nikhilkumarthota42@gmail.com
2020-05-19T07:50:37.655389+00:00 app[api]: Release v3 created by user nikhilkumarthota42@gmail.com
2020-05-19T07:50:37.655389+00:00 app[api]: Deploy d3a84204 by user nikhilkumarthota42@gmail.com
2020-05-19T07:50:47.000000+00:00 app[api]: Build succeeded
2020-05-19T07:51:14.829022+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=creditscore.herokuapp.com request_id=ac9a892f-958e-4a2e-850d-5b20fa911145 fwd="183.82.137.4" dyno= connect= service= status=503 bytes= protocol=https
2020-05-19T07:51:15.196438+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=creditscore.herokuapp.com request_id=8f756e08-5dd5-4cab-95b6-0fb1340aca41 fwd="183.82.137.4" dyno= connect= service= status=503 bytes= protocol=https

所有安装都已正确完成,我不知道是什么导致了这个错误。请帮助我

【问题讨论】:

  • 在此处添加您的Procfile

标签: python flask heroku


【解决方案1】:

不需要在 Heroku 绑定地址和端口。我还有一个托管在 Heroku 的应用程序,我的 Procfile 如下所示:

web: gunicorn main:app --log-file -

我的main.py使用工厂方法创建app:

from myapp import create_app
...
app = create_app()
...

尝试咨询DevCenter,“使用 Gunicorn 部署 Python 应用程序”部分。反正很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多