我正在使用flask构建网页,并且我具有python语法:
@app.route('/uploads/<filename>')
def uploaded_file(filename):
reader = shapefile.Reader("./uploads/"+filename)
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
for sr in reader.shapeRecords():
atr = dict(zip(field_names, sr.record))
geom = sr.shape.__geo_interface__
buffer.append(dict(type="Feature", \
geometry=geom, properties=atr))
json_string = json.dumps(buffer)
return render_template('view.html',r = json_string))
这给出了json响应
[{"type": "Feature", "geometry": {"type": "Point", "coordinates": [595371.8167114258, 28460830.87548828]}, "properties": {"Enabled": 1}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [595508.9202880859, 28465509.365478516]}, "properties": {"Enabled": 1}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [595478.0269165039, 28465540.729675293]}, "properties": {"Enabled": 1}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [595594.5479125977, 28465644.839111328]}, "properties": {"Enabled": 1}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [595690.145690918, 28465719.45727539]}, "properties": {"Enabled": 1}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [596209.5372924805, 28465729.346679688]}, "properties": {"Enabled": 1}}, .....etc
我想用miranda.js以html表格式打印
<table class="table table-bordered" id="myTable">
<thead>
<tr>
<th>TYPE</th>
<th>GEOMETRY</th>
<th>GEOMETRY TYPE</th>
<th>COORDINATES</th>
</tr>
</thead>
<tbody id="mydata">
<tr>
<td>[[type]]</td>
<td>[[geometry]]</td>
<td>[[type]]</td>
<td>[[coordinates]]</td>
</tr>
</tbody>
</table>
$("#mydata").mirandajs("{{r}}");
但是什么都没有发生。我只想要一种将这个python obj json解析为html表的方法。你能告诉我我做错了什么吗?或者你能告诉我一种轻松完成我的事情的方法
参考方案
为json
数据创建一个类并使用内置的jinja
模板是最简单的。另外,似乎键"Geometry"
:
class Stat:
def __init__(self, stat):
self.__dict__ = {a:Stat(b) if isinstance(b, dict) else b for a, b in stat.items()}
class Stats:
def __init__(self, full_data):
self.full_data = full_data
def __iter__(self):
for i in self.full_data:
yield Stat(i)
然后,调用render_template
并传递类实例:
return render_template('view.html',r = Stats(json_string)))
最后,在view.html
中,应用jinja
模板:
<table class="table table-bordered" id="myTable">
<thead>
<tr>
<th>TYPE</th>
<th>GEOMETRY TYPE</th>
<th>COORDINATES</th>
</tr>
</thead>
<tbody id="mydata">
{%for data in r%}
<tr>
<td>{{data.type}}</td>
<td>{{data.geometry.type}}</td>
<td>{{data.geometry.coordinates}}</td>
</tr>
{%endfor%}
</tbody>
</table>
在提交时在表单操作中获取变量丢失 - javascript当表单由onchange事件提交时,它不会保留get变量view。任何想法为什么会发生这种情况?提交后,这是它进入的网址,index?month=February&year=2014<form action="index?view=list" class="pure-form pure-fo…
选择后显示输入元素 - javascript我有一个表格,其中取决于用户的选择,输入元素是否可见。实际上,用户正在以另一种形式设置已定义的合作伙伴类型,并且如果选中该元素,则允许在该类型的合作伙伴上可见的元素类型1将显示以下元素:<input type="text" id="partner" name="partner" class=&…
JavaScript将PHP中的字符串和整数传递给函数 - javascript我正在尝试将字符串和整数都传递到同一函数中,但是引号引起了问题。我发现错误出在echo $q->info部分,我必须在此代码上使用双引号。有人可以帮我写这个$q->info,但不能获得真正的价值吗?到目前为止,我的代码是<td><a href="javascript:add(<?php echo $q->i…
Telerik单选按钮所需的字段验证器 - javascript如何设置Telerik单选按钮所需的字段验证器?我想在按钮单击“ BtnSave”上设置必填字段验证器吗?请帮忙!<telerik:RadButton ID="radio_male" runat="server" ToggleType="Radio" AutoPostBack="fa…
如何从javascript函数为gridview文本框设置值 - javascript我有以下项目模板的GridView<ItemTemplate> <asp:TextBox ID="txtComments" runat="server" Text='<%# Bind("Comments") %>' </asp:TextBox&…