An Example of Hierarchic Data

Note: This is a (somewhat ugly) export of an Jupyter notebook that can be found in the “examples” folder.

# This is only necessary to execute this notebook from the examples folder.
import sys
import os
sys.path.insert(0, os.path.abspath('..'))

from tableprinter import Tableprinter

# For displaying the HTML output directly in the notebook
from IPython.display import HTML
# Generate hierarchic data. x1 and x2 are the two x-coordinates. y1, y2 and y3 are three levels of y-coordinates.
data = [
    {'x1': x1, 'x2': x2, 'y1': y1, 'y2': y2, 'y3': y3, 'content': '{}/{}-{}/{}/{}'.format(x1,x2,y1,y2,y3)}
    for x1 in ['a', 'b'] for x2 in [1,2] for y1 in ['x', 'y'] for y2 in ['3', '4'] for y3 in ['i', 'j']
]

# Not all cells have to be present
_ = data.pop(23)
tp = Tableprinter(data, x_dimensions=('x1','x2'), y_dimensions=('y1','y2','y3'))
print(tp.as_unicode())
┏━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┓
┃      ┃           a           ┃           b           ┃
┃      ┃     1     ┃     2     ┃     1     ┃     2     ┃
┣━━━━━━╋━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━━━━┫
│    i │ a/1-x/3/i │ a/2-x/3/i │ b/1-x/3/i │ b/2-x/3/i │
│  3 ──├───────────┼───────────┼───────────┼───────────┤
│    j │ a/1-x/3/j │ a/2-x/3/j │ b/1-x/3/j │ b/2-x/3/j │
│x ────├───────────┼───────────┼───────────┼───────────┤
│    i │ a/1-x/4/i │ a/2-x/4/i │ b/1-x/4/i │ b/2-x/4/i │
│  4 ──├───────────┼───────────┼───────────┼───────────┤
│    j │ a/1-x/4/j │ a/2-x/4/j │ b/1-x/4/j │ b/2-x/4/j │
│──────├───────────┼───────────┼───────────┼───────────┤
│    i │ a/1-y/3/i │ a/2-y/3/i │ b/1-y/3/i │ b/2-y/3/i │
│  3 ──├───────────┼───────────┼───────────┼───────────┤
│    j │ a/1-y/3/j │ a/2-y/3/j │ b/1-y/3/j │ b/2-y/3/j │
│y ────├───────────┼───────────┼───────────┼───────────┤
│    i │ a/1-y/4/i │ a/2-y/4/i │ b/1-y/4/i │ b/2-y/4/i │
│  4 ──├───────────┼───────────┼───────────┼───────────┤
│    j │ a/1-y/4/j │ a/2-y/4/j │    --     │ b/2-y/4/j │
└──────┴───────────┴───────────┴───────────┴───────────┘
HTML(tp.as_html())
a
b
1
2
1
2
x3ia/1-x/3/ia/2-x/3/ib/1-x/3/ib/2-x/3/i
ja/1-x/3/ja/2-x/3/jb/1-x/3/jb/2-x/3/j
4ia/1-x/4/ia/2-x/4/ib/1-x/4/ib/2-x/4/i
ja/1-x/4/ja/2-x/4/jb/1-x/4/jb/2-x/4/j
y3ia/1-y/3/ia/2-y/3/ib/1-y/3/ib/2-y/3/i
ja/1-y/3/ja/2-y/3/jb/1-y/3/jb/2-y/3/j
4ia/1-y/4/ia/2-y/4/ib/1-y/4/ib/2-y/4/i
ja/1-y/4/ja/2-y/4/j--b/2-y/4/j

Note: The produced HTML sets various class attributes and can therefore be arbitrarily styled.

print(tp.as_latex())
begin{tabular}{ rrrcccc }
toprule
multicolumn{ 3 }{c}{  } & multicolumn{ 2 }{c}{ a } & multicolumn{ 2 }{c}{ b } \
cmidrule(lr){ 4 - 5 } cmidrule(lr){ 6 - 7 }
multicolumn{ 3 }{c}{  } & 1& 2& 1& 2\

midrule
multirow{ 4 }{*}{x} & multirow{ 2 }{*}{3} & multirow{ 1 }{*}{i} & a/1-x/3/i & a/2-x/3/i & b/1-x/3/i & b/2-x/3/i\
 &  & multirow{ 1 }{*}{j} & a/1-x/3/j & a/2-x/3/j & b/1-x/3/j & b/2-x/3/j\
cmidrule(lr){ 2 - 3 } \
 & multirow{ 2 }{*}{4} & multirow{ 1 }{*}{i} & a/1-x/4/i & a/2-x/4/i & b/1-x/4/i & b/2-x/4/i\
 &  & multirow{ 1 }{*}{j} & a/1-x/4/j & a/2-x/4/j & b/1-x/4/j & b/2-x/4/j\
cmidrule(lr){ 1 - 3 } \
multirow{ 4 }{*}{y} & multirow{ 2 }{*}{3} & multirow{ 1 }{*}{i} & a/1-y/3/i & a/2-y/3/i & b/1-y/3/i & b/2-y/3/i\
 &  & multirow{ 1 }{*}{j} & a/1-y/3/j & a/2-y/3/j & b/1-y/3/j & b/2-y/3/j\
cmidrule(lr){ 2 - 3 } \
 & multirow{ 2 }{*}{4} & multirow{ 1 }{*}{i} & a/1-y/4/i & a/2-y/4/i & b/1-y/4/i & b/2-y/4/i\
 &  & multirow{ 1 }{*}{j} & a/1-y/4/j & a/2-y/4/j & -- & b/2-y/4/j\

bottomrule
end{tabular}