如何使 bootstrap-table-filter-control 与 Flask、Jinja 和 Dataframe 一起使用

Mn2*_*n2C 4 html python jinja2 flask bootstrap-table

我正在使用 python Flask render_template 返回一个 html 页面作为我的 python 应用程序的路径,并且在 html 中我想使用 bootstrap-table-filter-control,如此处的 bootstrap 示例中所述。然而,给出的示例似乎使用本地 json 文件中的表。这是示例中给出的代码:

<link href="https://unpkg.com/bootstrap-table@1.16.0/dist/bootstrap-table.min.css" rel="stylesheet">

<script src="https://unpkg.com/bootstrap-table@1.16.0/dist/bootstrap-table.min.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.16.0/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>
Run Code Online (Sandbox Code Playgroud)

我该如何更换

数据url =“json/data1.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 %}
Run Code Online (Sandbox Code Playgroud)

我的 python 路线如下所示:

@app.route('/')
def hello():
    return render_template('yield.html',  tables=[df3.to_html(classes='data', header="true")])
Run Code Online (Sandbox Code Playgroud)

其中 df3 是我想在带有引导过滤器控件的yield.html 中显示为表格的数据帧。

Mn2*_*n2C 7

经过一些回归基础的逐步测试后,我找到了自己的解决方案,现在它的效果非常好。我的 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/bootstrap-table@1.16.0/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/bootstrap-table@1.16.0/dist/bootstrap-table.min.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.16.0/dist/extensions/filter-control/bootstrap-table-filter-control.min.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

值得注意的是,我不必在 main.py 应用程序中调用 to_html (用于完成包含标题和行标签的 html 表的数据框)创建的整个表,而是必须使用每列的参数专门指定表头(在 thead 标签内)明确说明,标题的数据字段名称中不允许有空格和特殊字符,然后使用 {% 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)
Run Code Online (Sandbox Code Playgroud)