0

I have a list in python, how to pass it to HTML table in Python.

list1 = [['Career', 'school', 5, 'A'], ['Career', 'higher', 4, 'A'], ['Career', 'college', 3, 'A'], ['Edu', 'Blr', 20, 'A']]

 html =  """\<html><head><style>table, th, td {border: 1px solid black;border-collapse: collapse;}th, td {padding: 5px;text-align: left;}</style></head><table style="width:30%"><tr><th>Category</th><th>Sub-Category</th><th>Sessions</th><th>Org_name</th></tr><tr># The list should print here </tr></table></body></html> """

The output should be in table like

Category|Sub-Category|Sessions|Org_name Career |School |5 |A Career |Higher |4 |A Career |College |3 |A Edu |Blr |20 |A

Please help me.

2
  • Are you using Jinja template? Commented May 22, 2018 at 9:44
  • @Rakesh: No, its direct html in python with other data tables which contains variable passed. Commented May 22, 2018 at 10:14

4 Answers 4

1

Here is a simple solution without any third-party libraries:

list1 = [['Career', 'school', 5, 'A'], ['Career', 'higher', 4, 'A'], ['Career', 'college', 3, 'A'], ['Edu', 'Blr', 20, 'A']]
headers = ['Category', 'Sub-Category', 'Sessions', 'Org_name']
style = """
 td, th {
      border: 1px solid black;
    }
"""
template = """
<html>
  <style>
    {}
  </style>
  <body> 
    <table>   
    {}
    </table>
  </body>
</html>
""".format(style, '\t<tr>'+'\n'.join('\t\t<th>{}</th>'.format(i) for i in headers)+'\n\t</tr>'+'\n'.join('\t<tr>'+'\n'.join('\t\t<td>{}</td>'.format(c) for c in i)+'\n\t</tr>' for i in list1[1:])) 
with open('test_file.html', 'w') as f:
  f.write(template)

When opening test_file.html in a browser, the result below is produced:

enter image description here

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

Comments

0

do like this:

{% for lists in list1 %}
    <tr>
    {%for li in lists %}
    <td>{{li}}</td>
    {% endfor %}
    </tr>
{% endfor %}

5 Comments

did you check this? @Praveen Reddy
its coming as invalid syntax on the first line. The code should be used inside the HTML
and when you use render_template you should pass the list1 to html file
yes , i have used inside HTML. but its showing as invalid syntax on the first line
yes i am passing list1 to html file but for syntax is correct ?. Please help me.
0

If you're just looking for a string of HTML inside your Python program, easiest way I can think of is using Pandas and its to_html method:

import pandas as pd
df = pd.DataFrame(list1)
df.columns = ['Category', 'Sub-Category', 'Sessions', 'Org_name']
html = df.to_html(index=False)

Comments

0
df=pd.DataFrame(list1,columns=["Category","Sub-Category","Sessions","Org_name"])

print(df.to_html()) #it will print html tags for the df

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Category</th>
      <th>Sub-Category</th>
      <th>Sessions</th>
      <th>Org_name</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>Career</td>
      <td>school</td>
      <td>5</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>Career</td>
      <td>higher</td>
      <td>4</td>
      <td>A</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Career</td>
      <td>college</td>
      <td>3</td>
      <td>A</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Edu</td>
      <td>Blr</td>
      <td>20</td>
      <td>A</td>
    </tr>
  </tbody>
</table>

5 Comments

The list is dynamic. The list comes from SQL query. I just gave sample list. the list may increase or decrease as well.
get the sql table colums and replace the column values
Let me know how to do it using for loops iteration please.
you can create a empty dataframe df=pd.DataFrame() then loop like for row in rows:df.append(row,ignore_index=True)
Please approve the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.