首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用for循环在python Flask应用程序中以html格式呈现表格

使用for循环在python Flask应用程序中以html格式呈现表格
EN

Stack Overflow用户
提问于 2018-07-29 05:23:13
回答 1查看 1.1K关注 0票数 -1

我想显示一个python数据帧到html页面使用for循环。我不想使用.to_html函数,因为我想自定义表elements.Is,有什么办法做到这一点吗?

下面是我的python代码:

代码语言:javascript
复制
@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)

这是我的临时位置:

代码语言:javascript
复制
<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 }}&nbsp&nbsp</th>{% endfor %}
    </tr>
{% for i in data_index_len %}
    <tr>
        {% for j in headers %}<td>{{ data.ix[i,j] }}&nbsp&nbsp</td>
        {% endfor %}
    </tr>
{% endfor}
</table>
</body>
</html>

在加载/table时,我得到了这个随机错误

代码语言:javascript
复制
jinja2.exceptions.TemplateSyntaxError: unexpected '}'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-29 05:26:52

在这里的代码中-

代码语言:javascript
复制
{% for i in data_index_len %}
    <tr>
        {% for j in headers %}<td>{{ data.ix[i,j] }}&nbsp&nbsp</td>
        {% endfor %}
    </tr>
{% endfor}  <==== you are missing a %

{% endfor %}应该会修复你的错误

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51575373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档