繁体   English   中英

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

[英]How to show index.html page in heroku using Python/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页面

您必须先在flask应用程序中创建路由,然后才能访问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的路由。

PS,您在getDistance()函数中的url参数应该为/geodesicDistance getDistance()您不需要127.0.0.1:5000

我已经添加了路由并将上述Javascript中包含的URL修改为“ / 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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