2017-04-25 118 views
1

我使用Pandas從數據庫中獲取和轉換某些數據,現在我想打印動態分數,即不刷新頁面。我已經嘗試過:{{ mydata|safe}},我可以獲得全部信息,但所有信息都不在表格中。如何使用Jinja2打印表格(無頁面刷新)

我用熊貓從數據幀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的打印表格?

+0

你可以分享你的渲染功能和HTML模板? –

回答

0

,你需要的神社模板是這樣的:

<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

+0

感謝您的幫助,但我得到空表:( –

+0

@Symmon在你的第一個例子中,你寫了'mydata',而在第二個'data'中,請確保在複製我的代碼時使用了正確的一個。 ,取決於數據類型,它可能是'{{row ['VAT']}}'或'{{row.vat}}'。 – VMRuiz

+0

此外,您將需要使用Ajax作爲@ sweater-bacon表示更新表格沒有頁面刷新,但是我留下了那部分,因爲你主要在jinja2中遇到麻煩 – VMRuiz

0

當然 這裏是我的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) 
+0

你能解釋一下你在這裏想要做什麼嗎? – user3341078