首页 > 解决方案 > 如何将格式化数据从 MongoDB 发布到 HTML 页面?

问题描述

我正在考虑使用 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 输出的帮助。

标签: pythonhtmlmongodbflask

解决方案


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

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

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


推荐阅读