【发布时间】:2018-06-15 04:46:24
【问题描述】:
基于此回复:
cursor = db.execute_sql('select * from tweets;')
for row in cursor.fetchall():
print row
cursor = db.execute_sql('select count(*) from tweets;')
res = cursor.fetchone()
print 'Total: ', res[0]
来自:Python Peewee execute_sql() example
如何将其带到flask app,然后显示在网页中?
这对吗:
模型.py
def get_statistics():
cursor = finDB.execute_sql('CALL Allstatistics;')
for row in cursor.fetchall():
return row
app.py
@app.route("/finance")
def finance():
stats = model.get_statistics()
return render_template('/finance.html', stats=stats)
但是如何在表格中显示呢?
【问题讨论】:
-
您想在 html 表格中显示它们吗?请显示统计信息的样子? stats 是针对什么数据类型的??一个列表?字典?
-
在 HTML
中是的,当我直接在 mysql 中运行此存储过程时,我得到 6 列和 22 行:文本 | -55585.73 | -24834.28 | -2069.94 | -895.19 |空
结果的数据结构是什么?我不知道...列表?我显然不是...在我的 peewee 模型中:def get_tags(self): return tag_list.select(),从表中给我输出,然后在 flask app.py 中我只是这样做output = model.get_tags() 然后在应用程序路由中:@app.route(.... output=output...),然后在网页上我可以使用列标题{{ row.header } 循环} 但这些都不适用于 db.execute_sql() 方法。