Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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>

'''

like image 547
Sid Avatar asked Jul 26 '26 00:07

Sid


1 Answers

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.

like image 103
olinox14 Avatar answered Jul 28 '26 14:07

olinox14