I am currently working with flask endpoints that are connected to my react frontend. My return value for val is "null". Return data is 100% correct as it appears in my server log.
How can I get the return value to be the value of data? I would appreciate a answer and also feedback on the overall code on how to make it better.
@app.route("/handyman", methods=["POST"])
def get_data():
if request.method == "POST":
data = request.form.get("url")
print(data)
return data
@app.route("/response")
def response():
try:
val = get_data()
return {
"sucess": val
}
except Exception as e:
print(f"exception", e)
Below is my entire code part for context. Here the error is:
TypeError: path should be path-like or io.BytesIO, not <class 'NoneType'>
@app.route("/handyman", methods=["POST"])
def get_data():
if request.method == "POST":
data = request.form.get("url")
print(data)
return data
def model():
val = get_data()
img = load_img(val, target_size=(150, 150 ))
img_array= img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
prediction = loaded_model.predict(img_array)[0]
print(prediction)
return prediction
def calculation():
prediction = model()
results = []
x1 = prediction[0]
x2 = prediction[1]
if x1 > 0.5:
result = "Händler"
results.append(result)
if x2 > 0.5:
result = "Baustelle"
results.append(result)
return results
@app.route("/response")
def response():
model()
result = calculation()
return {
"success": result[-1]
}
responseendpoint, is it a POST request? If the request to that endpoint is a GET, then your get_data function will return a null implicitly to the calling function.get_data()function, you have an if condition that checks if it is a POST request. However, if you're simply callingget_data()it is not a POST request. That also wouldn't make sense, asget_data()requires some"url"parameter. Where exactly should that"url"come from? Because when a user opens "/response", it will be a simple GET request that contains no"url".responsefunction is callingmodel()without ever using its return value. I think this could be omitted sincecalculation()also callsmodel().last_url = Nonethen Inget_datawriteglobal last_urlandlast_url = request.form.get("url")With this the issue was solved and I could work with the return value of this function. However the question remained how overall one can work better with the return value of flask endpoints?