import random
import string
n = 10
categories = ['apple', 'orange', 'pear']
table = {
'index': list(range(n)),
'value': [ random.random() for i in range(n) ],
'category': [ random.choice(categories) for i in range(n) ],
}
table
{'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'value': [0.05791388893890004, 0.1361473411698706, 0.4355893879972329, 0.6524423229052798, 0.582591041276253, 0.22242459656741354, 0.5265246580565507, 0.8431390622341968, 0.47263456785163915, 0.04355332045543037], 'category': ['apple', 'apple', 'apple', 'apple', 'apple', 'pear', 'apple', 'pear', 'pear', 'apple']}
If available, the simplest version is perhaps to use pandas.DataFrame.from_dict
:
import pandas
pandas.DataFrame.from_dict(table)
index | value | category | |
---|---|---|---|
0 | 0 | 0.057914 | apple |
1 | 1 | 0.136147 | apple |
2 | 2 | 0.435589 | apple |
3 | 3 | 0.652442 | apple |
4 | 4 | 0.582591 | apple |
5 | 5 | 0.222425 | pear |
6 | 6 | 0.526525 | apple |
7 | 7 | 0.843139 | pear |
8 | 8 | 0.472635 | pear |
9 | 9 | 0.043553 | apple |
But you can also write your own HTML renderer for your table data:
import html
from io import BytesIO
from xml.etree.ElementTree import ElementTree, Element
def html_table(table):
column_labels = list(table)
columns = [table[col] for col in column_labels]
table = Element("table")
# create the header row with column names
row = Element("tr")
table.append(row)
for label in column_labels:
el = Element("th")
row.append(el)
el.text = html.escape(label)
# create each row
for row_items in zip(*columns):
row = Element("tr")
table.append(row)
for item in row_items:
el = Element("td")
row.append(el)
el.text = html.escape(str(item))
buf = BytesIO()
ElementTree(table).write(buf)
return buf.getvalue().decode('utf8')
html_table(table)
'<table><tr><th>index</th><th>value</th><th>category</th></tr><tr><td>0</td><td>0.05791388893890004</td><td>apple</td></tr><tr><td>1</td><td>0.1361473411698706</td><td>apple</td></tr><tr><td>2</td><td>0.4355893879972329</td><td>apple</td></tr><tr><td>3</td><td>0.6524423229052798</td><td>apple</td></tr><tr><td>4</td><td>0.582591041276253</td><td>apple</td></tr><tr><td>5</td><td>0.22242459656741354</td><td>pear</td></tr><tr><td>6</td><td>0.5265246580565507</td><td>apple</td></tr><tr><td>7</td><td>0.8431390622341968</td><td>pear</td></tr><tr><td>8</td><td>0.47263456785163915</td><td>pear</td></tr><tr><td>9</td><td>0.04355332045543037</td><td>apple</td></tr></table>'
Which you can then pass to IPython.display
to see it as a nice table
from IPython.display import display, HTML
display(HTML(html_table(table)))
index | value | category |
---|---|---|
0 | 0.05791388893890004 | apple |
1 | 0.1361473411698706 | apple |
2 | 0.4355893879972329 | apple |
3 | 0.6524423229052798 | apple |
4 | 0.582591041276253 | apple |
5 | 0.22242459656741354 | pear |
6 | 0.5265246580565507 | apple |
7 | 0.8431390622341968 | pear |
8 | 0.47263456785163915 | pear |
9 | 0.04355332045543037 | apple |
You can also register this as a renderer for dicts. If you do that, though, you'll have to implement some detection for whether a dict is likely to be a table, to avoid calling your code on regular dicts.
def render_dict_html_table(table):
# first check if it's probably a table
# if this is a table, all keys are strings and values are lists
# we don't go into more detail on the contents of those lists
# returning None means nothing to do
for key, value in table.items():
if not isinstance(key, str):
return None
if not isinstance(value, list):
return None
# it's probably a table, return the HTML
return html_table(table)
# now we can register our table-formatter as the html-formatter for dicts
from IPython import get_ipython
html_formatter = get_ipython().display_formatter.formatters['text/html']
html_formatter.for_type(dict, render_dict_html_table)
At this point, any dict table will be displayed as a table
table
index | value | category |
---|---|---|
0 | 0.05791388893890004 | apple |
1 | 0.1361473411698706 | apple |
2 | 0.4355893879972329 | apple |
3 | 0.6524423229052798 | apple |
4 | 0.582591041276253 | apple |
5 | 0.22242459656741354 | pear |
6 | 0.5265246580565507 | apple |
7 | 0.8431390622341968 | pear |
8 | 0.47263456785163915 | pear |
9 | 0.04355332045543037 | apple |
But other dicts will be left alone
{'key': 'value'}
{'key': 'value'}