pyltk/pyltk/table.py

61 lines
1.6 KiB
Python

# -*- encoding: utf-8 -*-
from .element import element
from .tabular import tabular
class table(element):
""" Class representing a latex figure. """
templates = {
'caption': '\\caption{{{content}}}',
'content': """\\begin{{table}}[{options}]
{centered}
{inner}
{caption}{label}
\\end{{table}}"""
}
autolabel = False
def __init__(self, childrens=None, parent=None,
caption=None, options='!ht',
centered=True, label=None):
""" Create a table with given options.
Parameters:
- options Options for the table.
- centered Set to true to add a centering command.
"""
super().__init__(parent, childrens, label=label)
self.options = options
self.caption = caption
self.centered = ''
if centered:
self.centered = '\\centering'
def content(self):
caption = ''
if self.caption:
caption = self.fmt().format('caption', {'content': self.caption})
return self.fmt().format('content', {
'options': self.options,
'centered': self.centered,
'caption': caption,
'label': self.format_label(self.label),
'inner': super().content()
})
class tabledf(table):
def __init__(self, df, header=None, **kargs):
super().__init__(**kargs)
if header is None:
header = df.columns
tab = tabular(columns='r' * len(header))
tab.addrow(header)
tab.addhline()
tab.addrows(df.as_matrix())
self.add(tab)