简体   繁体   中英

How to show index.html page in heroku using Python/Flask

I've deployed my first Python/Flask app to heroku. The app consists in an html page, let's say index.html, with a javascript function which creates a query to the python app running at 'url'

function getDistance(position) {
                         url = 'http://127.0.0.1:5000/geodesicDistance';
                         query = position.coords.latitude.toString()+','+position.coords.longitude.toString();
                         $.get(url, {'q': query},function(data) {
                         $('#results .distance')[0].innerHTML = Math.round(data['result']['distance']*1000)/1000;

                         })
}

The python app takes the query and gives back a result

The app is actually running at http://geodesicdistance.herokuapp.com/ but I can't manage to show the index.html page which is at the root folder

You have to create the route in your flask app before you can hit index.html .

You'll want something like:

from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route('/')
def geo_distance():
    return render_template('index.html')

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

Then when you hit http://geodesicdistance.herokuapp.com/ , it will render index.html , which should run your JS.

You'll also need a route for /geodesicDistance .

PS, your url param in your getDistance() function should be /geodesicDistance - you don't need the 127.0.0.1:5000

I've added the route and modified the url contained in the above Javascript to "/geodesicDistance". Now the html page is rendering but I can't get distance value as if the query wasn't made

app = Flask(__name__)

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

@app.route("/geodesicDistance")
def geodesicDistance():
    query = parseQ(request.args)
    if query:
        position = geocodeAddress(query)
        if (position) and (len(position[u'results'])>0):
            lat = position[u'results'][0][u'geometry'][u'location'][u'lat']
            lng = position[u'results'][0][u'geometry'][u'location'][u'lng']
            response = createResponse(200,'Good query',lat,lng,position[u'results'][0]  [u'formatted_address'],getDistance(lat,lng))
        else:
            response = createResponse(400,'Bad formed query','','','','')
    else:
        response = createResponse(204,'No query     made',givenAddress['lat'],givenAddress['lng'],'White Bear Yard, 144a Clerkenwell Road,    London, EC1R 5DF, UK',0.0)                               
    http_response = make_response(json.dumps(response))
    http_response.headers['Content-type'] = 'application/json'
    http_response.headers['Access-Control-Allow-Origin'] = '*'
    return http_response

My procfile looks like this

web: gunicorn geodesicDistance:app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM