[英]Python dictionary from flask returning null
我创建了一个家庭预测 model,目前正在尝试将其部署到 heroku。我正在使用 flask、javascript 和 html。
当我在本地运行时一切正常,但是当部署在 heroku 时,位置的下拉菜单是空的。 在 web 浏览器上检查我的控制台时,我注意到 JS 实际上从 flask 服务器获得了响应,但响应是 null 而不是位置列表。 这是相关代码。
import json
with open('columns.json', 'r') as f
__data_columns = json.load(f)['data_columns']
__locations= __data_columns[4:]
def get_location_names():
return __locations
@app.route('/locations', methods=['GET'])
def locations()
response =jsonify({'locations': get_location_names()})
return response
我在打印响应中得到的结果是: locations: null 我如何将其解析为 columns.json 包含如下内容:{"data_columns": ["New york", "Washigton"]}
问题可能与文件columns.json
的路径有关。 您尝试以这种方式构建:
import os
with open(os.path.join(os.path.dirname(__file__), 'columns.json'), 'r') as f:
__data_columns = json.load(f)['data_columns']
__locations= __data_columns[4:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.