Created
May 19, 2014 11:35
-
-
Save utaal/f864afab87cc644f2d06 to your computer and use it in GitHub Desktop.
Quick and dirty script to generate a html table from a json array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import print_function | |
| import json | |
| import tempfile | |
| import sys | |
| import os | |
| data = json.load(sys.stdin) | |
| cols = [] | |
| for item in data: | |
| for k, v in item.iteritems(): | |
| if k not in cols: | |
| cols.append(k) | |
| output = tempfile.NamedTemporaryFile(suffix = '.html', delete = False) | |
| def p(text): | |
| print(text, file=output) | |
| p(""" | |
| <style> | |
| * { | |
| font-family: sans-serif; | |
| font-size: 10pt; | |
| } | |
| table { | |
| border-collapse: collapse; | |
| } | |
| td { | |
| padding-left: 5px; | |
| padding-right: 5px; | |
| border: 1px solid #777; | |
| } | |
| </style> | |
| """) | |
| p('<table>') | |
| p('<thead>') | |
| p('<tr>') | |
| for c in cols: | |
| p('<td>{}</td>'.format(c)) | |
| p('</tr>') | |
| p('<tbody>') | |
| for item in data: | |
| p('<tr>') | |
| for k in cols: | |
| p('<td>{}</td>'.format(item[k] if k in item else '')) | |
| p('</tr>') | |
| p('</tbody>') | |
| output.close() | |
| os.system("open {}".format(output.name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment