如何使用Python / Flask在heroku中显示index.html页面

时间:2013-08-22 07:25:32

标签: javascript python html heroku flask

我已将我的第一个Python / Flask应用程序部署到heroku。 该应用程序包含一个html页面,让我们说index.html,带有一个javascript函数,该函数为运行在' url'

的python应用程序创建查询
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;

                         })
}

python应用程序接受查询并返回结果

该应用实际上是在http://geodesicdistance.herokuapp.com/运行,但我无法显示位于根文件夹的index.html页面

2 个答案:

答案 0 :(得分:0)

您必须先在烧瓶应用中创建路线,然后才能点击index.html

你会想要这样的东西:

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()

然后当你点击http://geodesicdistance.herokuapp.com/时,它会呈现index.html,它应该运行你的JS。

您还需要/geodesicDistance的路线。

P.S。,url函数中的getDistance()参数应为/geodesicDistance - 您不需要127.0.0.1:5000

答案 1 :(得分:0)

我添加了路由并将上面Javascript中包含的网址修改为“/ geodesicDistance”。现在html页面正在渲染,但是我无法获得距离值,就像查询没有被创建一样

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

我的procfile看起来像这样

web: gunicorn geodesicDistance:app