3 回答

TA贡献1802条经验 获得超5个赞
如果有人觉得这有帮助。我选择了一个替代方案,因为我需要更多的自定义,包括在表中添加执行操作的按钮的能力。我也真的不喜欢标准表格格式,因为恕我直言,它非常难看。
...
df = pd.DataFrame({'Patient Name': ["Some name", "Another name"],
"Patient ID": [123, 456],
"Misc Data Point": [8, 53]})
...
# link_column is the column that I want to add a button to
return render_template("patient_list.html", column_names=df.columns.values, row_data=list(df.values.tolist()),
link_column="Patient ID", zip=zip)
HTML 代码:这将任何 DF 动态转换为可定制的 HTML 表
<table>
<tr>
{% for col in column_names %}
<th>{{col}}</th>
{% endfor %}
</tr>
{% for row in row_data %}
<tr>
{% for col, row_ in zip(column_names, row) %}
{% if col == link_column %}
<td>
<button type="submit" value={{ row_ }} name="person_id" form="patient_form" class="patient_button">
{{ row_ }}
</button>
</td>
{% else %}
<td>{{row_}}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
CSS 代码
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
它表现得非常好,看起来比.to_html输出好得多。
添加回答
举报