我正在尝试在我的烧瓶应用程序中显示数据表。这是我的示例,仅显示iris
数据集。
下面是我的app.py
from flask import Flask, render_template
import seaborn as sns
import pandas as pd
iris = sns.load_dataset('iris')
app = Flask(__name__)
@app.route('/')
def example():
return render_template("example.html", value=iris)
if __name__ == '__main__':
app.run(debug=True)
这里是example.html
<!doctype html>
<html>
<head></head>
<body>
<table>
<thead>
<tr>
<th>sepal_length</th>
<th>sepal_width</th>
<th>petal_length</th>
<th>petal_width</th>
<th>species</th>
</tr>
</thead>
<tbody>
{% for row in value %}
<tr>
<td>{{row[0]}}</td>
<td>{{row[1]}}</td>
<td>{{row[2]}}</td>
<td>{{row[3]}}</td>
<td>{{row[4]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
我有这样的桌子:
sepal_length sepal_width petal_length petal_width species
s e p a l
s e p a l
p e t a l
p e t a l
s p e c i
我花了数小时试图找出问题所在。我仍然不知道为什么它不起作用。有谁知道为什么会这样?
而且,烧瓶中是否有办法显示漂亮的数据表,而不是用html编码?如果没有的话,我真的会感到惊讶。
实际上,您将模板文件夹放在错误的路径中,这就是为什么您收到TemplateNotFound错误的原因。您需要将模板文件夹放在此位置“ os.path.abspath('templates')”,并且您已经解决了此问题。
app.py
从烧瓶导入烧瓶,render_template
将seaborn导入为sns
将熊猫作为pd导入
iris = sns.load_dataset('iris')
template_dir = os.path.abspath('templates')
static_dir = os.path.abspath('static')
app = Flask(__ name __,template_folder = template_dir,static_folder = static_dir,)
@ app.route('/')
def example():
return render_template("example.html", value=iris)
如果[__ name __ == '__ main __':
app.run(debug=True)