python - 如何将格式化数据从 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 输出的帮助。
解决方案
下面的 HTML 代码解决了这个问题:
<html>
<body>
{% for task_id in tasks %}
<h3>{{task_id}}:{{tasks[task_id]}}</h3>
{% endfor %}
</body>
</html>
在进一步的研究中,我理解 Task_id 基本上是一本字典。
推荐阅读
- android - 什么是 Android 的“中止”错误,为什么 Firebase Crashlytics 没有捕捉到它?
- angular - Angular - 对象可能是“null”:错误 TS7052:元素隐式具有“任何”类型,因为类型为“AbstractControl”
- android - 如何为 Uber-eats android 等菜单选择创建动态复选框组?
- python - Python SocketIO: BadNamespaceError: / is not a connected namespace
- sql - SQL计算相同数字块的长度,而忽略变化数字块
- javascript - 将对象 js 提取发送到 asp.net 并收到错误 System.NullReferenceException:“对象引用未设置为对象的实例。”
- python - 如何导入同时导入其他笔记本的 jupyter 笔记本?
- c# - 从外部 Web API 获取令牌正在返回 system.threading.tasks
- javascript - Highcharts - 热图 - 图例删除白色“破折号”
- php - 预期在 HTML 属性中使用 PHP 短 Echo 标记的 PhpStorm 表达式