【问题标题】:How to post a formatted data from MongoDB to a HTML page?如何将格式化数据从 MongoDB 发布到 HTML 页面?
【发布时间】:2019-08-25 15:31:37
【问题描述】:

我正在考虑使用 HTML 将格式良好的数据发布到网页上。我使用 Flask 和 Mongodb 作为我的数据库在 python 中编写了一个代码。

@app.route('/')
def Results():
    try:
        Project_List_Col = db.ppm_master_db_collection.find({}, 
   {"_id":0})
    return render_template('Results.html',tasks=Project_List_Col)
except Exception as e:
    return dumps({'error': str(e)})

if __name__ == '__main__':  
   app.run(debug = True)

但是,数据以 JSON 格式打印。我希望将其格式化为表格格式。

这是我的 HTML 代码:

<html>
   <body>
     {% for task_id in tasks %}
        <h3>{{task_id}}</h3>
     {% endfor %}
   </body>
</html>

目前的输出类似于:

[{u'total': 9942806, u'_id': {u'd': 1, u'sid': u'c1'}},
 {u'total': 10173832, u'_id': {u'd': 1, u'sid': u'c2'}},
 {u'total': 9567489, u'_id': {u'd': 1, u'sid': u'c3'}}]

但是,我需要它是这样的:

Day     C1    C2    C3    C4
1      123   125  122    254 
2       123   125  122    254
3       123   125  122    254

我需要有关如何获得可读性更好的 UI 输出的帮助。

【问题讨论】:

    标签: python html mongodb flask


    【解决方案1】:

    下面的 HTML 代码解决了这个问题:

    <html>
       <body>
             {% for task_id in tasks %}
                <h3>{{task_id}}:{{tasks[task_id]}}</h3>
             {% endfor %}
       </body>
    </html>
    

    在进一步的研究中,我理解 Task_id 基本上是一本字典。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-17
      • 2012-04-24
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多