I am running my flask application on google cloud that works fine locally and also in cloud editor.I have to deploy the flask application on google cloud so, i need to add app.yaml and requirement.txt file before deployment.I have added those files to my project directory & Once i deploy my flask application and browse to the url i am getting nginx error 502 Bad Gateway.How to solve this error.Error screenshot Since it works just fine on my local environment, my current workflow to solve this issue involves changing my code locally and deploying it to see if it works afterwards,but each deployment takes over 30min only to figure out it still does not work. I have my app variable set in my main.py file as described below.The flask application which i want to deploy is a simple app where i can upload-process-download the processed image
main.py
import os
import cv2
from werkzeug.utils import secure_filename
from flask import Flask,flash,request,redirect,send_file,render_template,send_from_directory
from PIL import Image, ImageDraw, ImageFont
import datetime
UPLOAD_FOLDER = 'uploads/'
#PROCESSED_FOLDER = 'processed/'
app = Flask(__name__)
app.template_folder = 'templates'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Upload API
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
print('no file')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
print('no filename')
return redirect(request.url)
else:
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
print("saved file successfully")
#send file name as parameter to downlad
return redirect('/process-file/'+ filename)
return render_template('upload_file.html')
# Download API
@app.route("/downloadfile/<filename>", methods = ['GET'])
def download_file(filename):
return render_template('download.html',value = filename)
@app.route('/return-files/<filename>')
def return_files_tut(filename):
file_path = 'processed/' + filename
return send_file(file_path, as_attachment=True)
if __name__ == "__main__":
app.run(debug=False)
app = Flask(__name__, template_folder='templates')
requirement.txt
aniso8601==9.0.1
click==8.1.3
colorama==0.4.6
distlib==0.3.6
filelock==3.8.2
Flask==2.2.2
Flask-RESTful==0.3.9
Flask-Uploads==0.2.1
Flask-WTF==1.0.1
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
numpy==1.24.1
opencv-python==4.7.0.68
Pillow==9.3.0
platformdirs==2.6.0
PyPDF2==3.0.0
python-dotenv==0.21.0
pytz==2022.7
six==1.16.0
virtualenv==20.17.1
Werkzeug==2.2.2
WTForms==3.0.1
when i deploy app with the requirement.txt as decribed above i get
Error:could not find the version that satisfies the requirement
I have solved those by editing my requirements with available versions . Final requirement.txt
Flask==2.0.2
gunicorn==20.1.0
Jinja2==3.0.2
opencv-python==4.6.0.66
Pillow==8.3.0
python-dotenv==0.20.0
Werkzeug==2.0.2
Once deployed i get [Error screenshot][1] [1]: https://i.stack.imgur.com/MdQMj.png
app.yaml
runtime: python311
this is my app.yaml and when i deploy my app gets deployed and gives 502 error