【发布时间】:2020-08-04 17:40:43
【问题描述】:
所以我正在使用烧瓶构建一个 Web 应用程序,它可以跟踪多辆汽车并提供更新。 python 脚本帮助收集所有数据并将它们放入字典中。
我遇到的问题是这个字典在 js 中没有被正确解析,因此我没有得到任何数据。
现在我有 {{truck_dict}} 占位符来在 html 中保存来自 python 的 dict 对象。
PS。我不是最擅长JS的所以不要评判XD
#Python Code
return render_template('pages/index.html', trucks = truck.driver_locator(truck.locations()))
#Even when I jsonify/json.dump the variable in the trucks object, nothing happens
#JS Code
var truck_dict = {{trucks | tojson}}
var i;
for (var key in truck_dict){
var value = truck_dict[key];
var geojson = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: value
},
properties: {
title: 'Mapbox',
description: '1303'
}
}]
};
python 生成的字典的样本输出
{'1301': [43.1220307, -78.9352247], '1302': [42.3107737, -77.2519131], '1304': [40.3809016, -74.5665863], '1305': [40.2453049, -74.5707928], '1303': [39.6435448, -75.9325289]}
【问题讨论】:
-
我们无法看到您是如何生成字典的,或者示例输出是什么。我建议您检查浏览器上的元素并查看实际插入到您正在运行的模板 javascript 中的内容。与这些小 sn-ps 相比,这些信息对您(和您的帖子)的帮助要大得多。
-
抓住了芽。编辑问题以反映输出
标签: javascript python web flask