displaying table in html using 'for' loop from render_template()

问题: Using Python Flask app to render a html table with 'for' loop implementation. But the data doesn't render giving a blank <tr><tr>. I am converting a datafram...

问题:

Using Python Flask app to render a html table with 'for' loop implementation. But the data doesn't render giving a blank <tr><tr>.

I am converting a dataframe to html by passing a dff.to_html() from render_template and retrieving the table in html in a 'for' loop to display.

Python code:

return render_template('index.html', table = dff.head(50).to_html())

html code:

         <table>
            <tbody>

            {% for row in table %}
                <tr>
                    <td>{{row.name}}</td>
                    <td>{{row.link_to_detail}}</td>
                    <td>{{row.link}}</td>
                    <td><input name ="get poster"  type="submit" value={{row['id']}}></td>
                </tr>
            {% endfor %}
            </tbody>
        </table>

Actual result is

<tr>
      <td></td>
      <td></td>
      <td></td>
      <td><input name="get poster" type="submit" value=""></td>
</tr>

Expected result :

    <tr>
      <td>name</td>
      <td>link_to_detail</td>
      <td>link</td>
      <td><input name="get poster" type="submit" value=""></td>
    </tr>

'''


回答1:

Do not use dff.head().to_html() which returns an HTML table as a string, but instead dff[:50].values (gives you 2d-array of all columns of the first 50 rows) and then row.0 and row.1 to get the values of the first two columns on each row.


回答2:

dff.head(50).to_html() returns an html table, not a python object.

This html table is actually a string, and you are iterating over characters (row), which do not have any attributes called name, link or link_to_detail...

I'll be able to complete this answer if you post a sample of your dff dataframe.


回答3:

I have never work python but idea is you need to send variables to view.

return render_template('index.html', table = dff.head(50).to_html(),  name='name' ,row.link_to_detail= 'row.link_to_detail' , link ='link')

it can be like this

  • 发表于 2019-07-08 00:30
  • 阅读 ( 287 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除