将python的json对象转换为html表?

时间:2018-04-09 13:30:45

标签: javascript python html html5 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表。你能告诉我我做错了吗?或者你可以告诉我一种让我的事情轻松完成的方法

2 个答案:

答案 0 :(得分:0)

最简单的方法是为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>

答案 1 :(得分:0)

尝试使用此库:

https://pypi.python.org/pypi/json2html

    json2html.convert - The module’s convert method accepts the following arguments:
        Argument    Description
        json    a valid JSON; This can either be a string in valid JSON format or a python object that is either dict-like or list-like at the top level.
        table_attributes    e.g. pass id=”info-table” or class=”bootstrap-class”/data-* to apply these attributes to the generated table
        clubbing    turn on[default]/off clubbing of list with same keys of a dict / Array of objects with same key
        encode  turn on/off[default] encoding of result to escaped html, compatible with any browser
        escape  turn on[default]/off escaping of html tags in text nodes (prevents XSS attacks in case you pass untrusted data to json2html)