Flask - 将数据帧显示为 table

Flask - Display datafram as table

我的 Flask 应用程序中有一条路线可以生成字典列表。我试图将该列表显示为 table。但是,当我导航到我的路线时,页面是空白的,我不确定为什么

代码:

app.py

@app.route('/<car_name>')
def generate_display_data(card_name):
    # code...
    df= pd.DataFrame(table)
    return render_template('car_info.html', tables=[df.to_html], header="true")

列表 table 如下所示:

[
 {'id': 6, 'image': 'de.png', 'name': '...', 'Type': '...' },
 {'id': 96, 'image': 'c4.png', 'name': '...', 'Type': '...' },
 {'id': 82, 'image': '4.png', 'name': '...', 'Type': '...' },
 {'id': 98, 'image': '37.png', 'name': '...', 'Type': '...' }
]

car_info.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% for table in tables %}
            {{ table|safe }}
{% endfor %}
</body>
</html>

当我 运行 我的应用程序。我可以看到列表 table 正在正确生成,但我看到一个空白的 html 页面。我错过了什么

您将函数引用 (df.to_html) 传递给 tables 而不是调用函数的结果。

而不是这个

return render_template('car_info.html', tables=[df.to_html], header="true")

这样做

return render_template("car_info.html", tables=[df.to_html()], header="true")