我想显示一个python数据帧到html页面使用for循环。我不想使用.to_html
函数,因为我想自定义表elements.Is,有什么办法做到这一点吗?
下面是我的python代码:
@app.route('/table',methods=['GET','POST'])
def table():
url = 'https://raw.github.com/pandas-dev/pandas/master/pandas/tests/data/tips.csv'
df_Final = pd.read_csv(url)
df_Final_header = list(df_Final)
df_Final_header_length1 = len(df_Final_header)
df_Final_header_length = list(range(0,df_Final_header_length1))
df_Final_Index_length1 =len(df_Final.index)
df_Final_Index_length = list(range(0,df_Final_Index_length1))
return render_template('table1.html',data=df_Final,headers=df_Final_header,data_index_len=df_Final_Index_length,data_row_len=df_Final_header_length) #.to_html(classes="table table-striped table-bordered table-hover table-condensed"))
if __name__ == '__main__':
app.run(debug=True)
这是我的临时位置:
<html>
<head>
<style>
#customers {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#customers td, #customers th {
border: .5px solid #ddd;
padding: 8px;
}
#customers tr:nth-child(even){background-color: #f2f2f2;}
#customers tr:hover {background-color: #ddd;}
#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #87CEFA;
color: white;
}
</style>
</head>
<body>
<table id="customers">
<tr>
{% for header in headers %}<th>{{ header }}  </th>{% endfor %}
</tr>
{% for i in data_index_len %}
<tr>
{% for j in headers %}<td>{{ data.ix[i,j] }}  </td>
{% endfor %}
</tr>
{% endfor}
</table>
</body>
</html>
在加载/table时,我得到了这个随机错误
jinja2.exceptions.TemplateSyntaxError: unexpected '}'
发布于 2018-07-29 05:26:52
在这里的代码中-
{% for i in data_index_len %}
<tr>
{% for j in headers %}<td>{{ data.ix[i,j] }}  </td>
{% endfor %}
</tr>
{% endfor} <==== you are missing a %
{% endfor %}
应该会修复你的错误
https://stackoverflow.com/questions/51575373
复制相似问题