3

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
  • table is a string with HTML so you can use {{ table }} to put it in template. Commented Jul 4, 2019 at 12:43

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.