2

I have this python list

result =[('921', 36638, None, None, 'ron', '28-SEP', 'platform'),
('921', 36637, None, None, 'john', '28-SEP', 'platform')]

The values in this list are dynamic. However the table header is always static.

Table header: 
Issue   Vers    created_on  Build   Author  Commit  Source_Name

I want to change this python output and make into a HTML table.

This is my work so far

z = import result
s =open(z)

table=['<htm><body><table border="1">']
for line in s.splitlines():
    if not line.strip():
        continue
    table.append(r'<tr><td>{}</td><td>{}</td></tr>'.format(*line.split('--- ')))

table.append('</table></body></html>')
print ''.join(table)    

I am confused as to put the static header.Thanks

1 Answer 1

3

this may be a case for a jinja template:

from jinja2 import Template

t = Template('''
<html>
  <body>
    <table border="1">

      <tr>
      {%- for col in header %}
        <td>{{col}}</td>
      {%- endfor %}
      </tr>

      {% for row in rows -%}
      <tr>
      {%- for col in row %}
        <td>{{col if col is not none else '' }}</td> 
      {%- endfor %}
      </tr>
      {% endfor %}

    </table>
  </body>
</html>
''')


header = 'Issue Vers created_on Build Author Commit Source_Name'.split()
rows = [('921', 36638, None, None, 'ron', '28-SEP', 'platform'),
        ('921', 36637, None, None, 'john', '28-SEP', 'platform')]

strg = t.render(header=header, rows=rows)

if you do not want the Nones printed in your table, you can replace {{col}} with {{col if col is not none else '' }}.

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

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.