如何使用Jinja2打印表格(无页面刷新)

时间:2017-04-25 05:25:36

标签: python html flask datatables jinja2

我使用Pandas来获取和转换数据库中的一些数据,现在我想打印得分动态,即没有页面刷新。我已经尝试过:{{ mydata|safe}}我得到了所有信息,但所有内容都不在一起。

我使用Pandas从数据框mydata.to_html())创建HTML表格,我得到类似的结果:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Part_number</th>
      <th>Type_name</th>
      <th>Short_desc</th>
      <th>Price_1</th>
      <th>Price_2</th>
      <th>VAT</th>
      <th>DB_source</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>565_MLNN7_101</td>
      <td>AIR_FILTER</td>
      <td>AIR_FILTER_GREEN</td>
      <td>1.20</td>
      <td>6.30</td>
      <td>23%</td>
      <td>Murexin</td>
    </tr>
    <tr>
      <th>1</th>
      <td>217_NE307_115</td>
      <td>CABLE</td>
      <td>CABLE_POWER_AC</td>
      <td>0.84</td>
      <td>3.20</td>
      <td>23%</td>
      <td>DB_1</td>
    </tr>
    </tr>
  </tbody>
</table>

你知道如何使用Jinja2打印表吗?

2 个答案:

答案 0 :(得分:1)

不确定 这是我的html模板代码:

{% extends "header.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block content %}

<div class="container">
<form class="form-signin" method="POST" action="/search">
    <div class="panel panel-default">
        <div class="panel-heading">Service Part Inquiry</div>
        <div class="panel-body">
{{ form.hidden_tag() }}
{{wtf.form_field(form.FGPN_Search)}}
{{wtf.form_field(form.AssyTypeName_Search)}}
{{wtf.form_field(form.Source_Search)}}

<button type="submit" class="btn btn-primary btn-xsy">Search</button>
 </div>
    </div>
</form>
</div> <!-- /container -->
    {{data|safe}}
    {% endblock %}

以下功能代码。实际上我只检查条件“Source_Search”:“Murexin”,这就是为什么我删除了一些代码(对你来说会更容易理解:)我尊重你的时间):

    @app.route('/search',methods=['GET', 'POST'])
    def search():
        form = SearchForm()
        if request.method == 'POST':
            FGPN_Search = form.FGPN_Search.data
            AssyTypeName_Search = form.AssyTypeName_Search.data
            Source_Search = form.Source_Search.data
        (...)

            elif Source_Search == 'Murexin':
                if len(FGPN_Search)>1:
                    tablePLM=read_sql_query(select1 + "\'" + FGPN_Search + "\'" + select2 + "\'" + AssyTypeName_Search + "\'",CurDB_2)
                    tableSIC = read_sql_query(selectSIC + "\'" + FGPN_Search + "\'",CurDB_1)
                    mydata = pd.merge(tablePLM, tableSIC, on='PM_PARTNUMBER',how='outer')
                    mydata.to_html()        
                    return render_template('search.html', form=form,data=mydata)
            elif Source_Search == 'test':
                return "<h1>test</h1>"
            else:
                flash("Please write anything.")
                return render_template('search.html',form=form)
        return render_template('search.html', form=form)

答案 1 :(得分:0)

您需要的jinja模板是这样的:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Part_number</th>
      <th>Type_name</th>
      <th>Short_desc</th>
      <th>Price_1</th>
      <th>Price_2</th>
      <th>VAT</th>
      <th>DB_source</th>
    </tr>
  </thead>
  <tbody>
      {% for row in mydata %}
          <tr> 
                <th>{{loop.index0}}</th>
                <td>{{row['Part_number']}}</td>
                <td>{{row['Type_name']}}</td>
                <td>{{row['Short_desc']}}</td>
                <td>{{row['Price_1']}}</td>
                <td>{{row['Price_2']}}</td>
                <td>{{row['VAT']}}</td>
                <td>{{row['DB_source']}}</td>
          </tr>
      {% endfor %}
  </tbody>
</table>

检查jinja2 documentation for more details