我使用python Flask render_template为我的python应用的路由返回一个html页面,在html中我想使用bootstrap-table-filter-control,就像bootstrap中描述的那样。例子. 然而,所给出的例子似乎是使用本地json文件中的一个表。下面是例子中给出的代码。
<link href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css" rel="stylesheet">
<script src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/extensions/filter-control/bootstrap-table-filter-control.min.js"></script>
<table
id="table"
data-url="json/data1.json" ## this looks like the code to get the table's data
data-filter-control="true"
data-show-search-clear-button="true">
<thead>
<tr>
<th data-field="id">ID</th>
<th data-field="name" data-filter-control="input">Item Name</th>
<th data-field="price" data-filter-control="select">Item Price</th>
</tr>
</thead>
</table>
<script>
$(function() {
$('#table').bootstrapTable()
})
</script>
我如何替换
data-url="jsondata1.json"
用我自己的表从main.py应用中传递过来的Python数据框架?我尝试了下面的html,但没有成功。
{% for table in tables %} <table id="table" data = {{ table|safe }} data-filter-control="true" data-show-search-clear-button="true" class="table-striped"> <thead> <tr> <th data-field="columnA" data-filter-control="input">column A</th> <th data-field="columnB" data-filter-control="select">columnB</th> <th data-field="columnC" data-filter-control="select">columnC</th> </tr> </thead> <tbody> <!-- I have tried to put {{ table|safe }} here but doesn't work --> </tbody> </table> {% endfor %}
我的python路径是这样的:
@app.route('/')
def hello():
return render_template('yield.html', tables=[df3.to_html(classes='data', header="true")])
其中df3是数据框,我想在yield.html中以表格形式显示,并使用bootstrap过滤控件。
经过一些返璞归真的一步步测试,我找到了自己的解决方案,现在工作得很好。我的yield.html看起来是这样的,启用了bootstrap-table过滤控件、搜索和排序。
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css">
<link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.16.0/extensions/filter-control/bootstrap-table-filter-control.css">
</head>
<body>
<div class="container">
<table id="table"
data-toggle="table"
data-filter-control="true"
data-show-search-clear-button="true"
data-sortable="true"
classes="table-sm"
data-pagination="true"
data-show-columns="true"
data-show-columns-toggle-all="true"
class="table-responsive">
<thead>
<tr>
<!--special characters and spaces not allowed in data-field name-->
<th data-field="columnA" data-filter-control="input" data-sortable="true">column A</th>
<th data-field="columnB" data-filter-control="select" data-sortable="true">column B</th>
<th data-field="columnC" data-filter-control="input" data-sortable="true">column C</th>
</tr>
</thead>
<tbody>
{% for row in tableA %}
<tr>
<!--special characters and spaces not allowed in data-field name-->
<td>{{ row.columnA }}</td>
<td>{{ row.columnB }}</td>
<td>{{ row.columnC }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/extensions/filter-control/bootstrap-table-filter-control.min.js"></script>
</body>
</html>
值得注意的是,我没有在main.py应用中调用由to_html制作的整个表格(dataframe到完整的html表格,包括表头和行标签),而是要特别指定表头(在ad标签内),每个列的参数都要说清楚,表头的数据字段名不允许有空格和特殊字符,然后用{% for row in tableA %}与相应的列名拉入数据行。
在我的python应用中,我的数据来自于数据框,我需要将其转换为python的数据类型,特别是像列表一样的python字典,用 data = df.to_dict('records')
当我渲染模板的时候,我可以通过使用 return render_template('yield.html', tableA = data)
我的python应用路线是这样的。
@app.route('/')
def hello():
data = df.to_dict('records')
return render_template('yield.html', tableA = data)