1

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]
    }
4
  • 1
    When you call the response endpoint, 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. Commented Nov 15 at 21:31
  • 1
    In your get_data() function, you have an if condition that checks if it is a POST request. However, if you're simply calling get_data() it is not a POST request. That also wouldn't make sense, as get_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". Commented Nov 17 at 19:02
  • Also, I saw that your response function is calling model() without ever using its return value. I think this could be omitted since calculation() also calls model(). Commented Nov 17 at 19:03
  • a previous answer solved this issue. Set last_url = None then In get_data write global last_url and last_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? Commented Nov 20 at 13:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.