'TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, but it was a int

I have encountered this error while working on my first flask web app. In this app, I am trying to get the distance between two points by using Uber H3 and the haversine formula. I am a beginner in this so any help would be appreciated.:

TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a int.

Traceback (most recent call last)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 2091, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 2076, in wsgi_app
response = self.handle_exception(e)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 1519, in full_dispatch_request
return self.finalize_request(rv)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 1538, in finalize_request
response = self.make_response(rv)
File "/Users/shipsy/Documents/flask_tutorial/venv/lib/python3.7/site-packages/flask/app.py", line 1731, in make_response
"The view function did not return a valid"
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a int.
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
import h3
import googlemaps
from haversine import haversine

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class ETAcalculation(db.Model):
    SNo = db.Column(db.Integer, primary_key=True)
    origin = db.Column(db.String(16), nullable = False)
    destination = db.Column(db.String(16), nullable = False)
    mode = db.Column(db.String(16), default = 'walking')
    # eta = db.Column(db.Integer)
    distance = db.Column(db.Integer)

    def __repr__(self):
        return self.distance
    
@app.route("/", methods = ['GET', 'POST'])
def hello_world():
    if request.method == 'POST':
        origin_hex_id = request.form['origin']
        destination_hex_id = request.form['destination']
        travelMode = request.form['mode']
        origin_coordinates = h3.h3_to_geo(origin_hex_id)
        destination_coordinates = h3.h3_to_geo(destination_hex_id)
        if (h3.h3_is_valid(origin_hex_id) and h3.h3_is_valid(destination_hex_id)):
            available = ETAcalculation.query.filter_by(origin = origin_hex_id, destination = destination_hex_id, mode = travelMode).first()
            reverse_available = ETAcalculation.query.filter_by(origin = destination_hex_id, destination = origin_hex_id, mode = travelMode).first()
            if available is None and reverse_available is not None:
                return reverse_available.distance
            if available is not None and reverse_available is None:
                return available.distance
            if available is None and reverse_available is None:
                result_distance = int(haversine(origin_coordinates, destination_coordinates))
                new_record = ETAcalculation(origin = origin_hex_id, destination = destination_hex_id, mode = travelMode, distance = result_distance)
                return result_distance
                try:
                    db.session.add(new_record)
                    db.session.commit()
                except:
                    "There was an error encountered in adding the details"
    else:
        return render_template('index.html')


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


Solution 1:[1]

The type of the returned variable in flask should be a string , dict or a tuple. Convert result_distance from an int to a string. It should eliminate the error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anonymous_Beast