Update.
This commit is contained in:
		@@ -7,7 +7,15 @@ from collections import Iterable
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class element:
 | 
					class element:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    """ Class representing a latex element. """
 | 
					    """ Class representing a latex element.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    An element is a latex element (e.g. \\figure), with a parent and some
 | 
				
			||||||
 | 
					    childrens. An element can have no parent and 0 childrens.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    By default, all childrens of an element must be instances of element
 | 
				
			||||||
 | 
					    themselves. To avoid this behavior, `raw` element can be used. `raw`
 | 
				
			||||||
 | 
					    elements do not wrap their children in `wrapper_element` when they
 | 
				
			||||||
 | 
					    are no subclasses of `element`. """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Joiner for childrens
 | 
					    # Joiner for childrens
 | 
				
			||||||
    sep = '\n'
 | 
					    sep = '\n'
 | 
				
			||||||
@@ -15,8 +23,20 @@ class element:
 | 
				
			|||||||
    # Automatically add label after element if present
 | 
					    # Automatically add label after element if present
 | 
				
			||||||
    autolabel = True
 | 
					    autolabel = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, parent=None, childrens=[], label=None):
 | 
					    # Do not wrap non-element in wrapper_element
 | 
				
			||||||
 | 
					    raw = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __init__(self, parent=None, childrens=[], label=None, raw=False):
 | 
				
			||||||
 | 
					        """ Create a new element with given parameters.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Parameters:
 | 
				
			||||||
 | 
					          - parent Parent of the element (another element).
 | 
				
			||||||
 | 
					          - childrens Childrens of the element - Single element or list of
 | 
				
			||||||
 | 
					            objects (element or not).
 | 
				
			||||||
 | 
					          - label Label of the element.
 | 
				
			||||||
 | 
					          - raw Raw childrens (see description of `element`). """
 | 
				
			||||||
        self.parent = parent
 | 
					        self.parent = parent
 | 
				
			||||||
 | 
					        self.raw = raw
 | 
				
			||||||
        if childrens is None:
 | 
					        if childrens is None:
 | 
				
			||||||
            childrens = []
 | 
					            childrens = []
 | 
				
			||||||
        if not isinstance(childrens, Iterable):
 | 
					        if not isinstance(childrens, Iterable):
 | 
				
			||||||
@@ -29,6 +49,7 @@ class element:
 | 
				
			|||||||
        if not isinstance(childrens, Iterable):
 | 
					        if not isinstance(childrens, Iterable):
 | 
				
			||||||
            childrens = [childrens]
 | 
					            childrens = [childrens]
 | 
				
			||||||
        for child in childrens:
 | 
					        for child in childrens:
 | 
				
			||||||
 | 
					            if not self.raw:
 | 
				
			||||||
                if not isinstance(child, element):
 | 
					                if not isinstance(child, element):
 | 
				
			||||||
                    child = wrapper_element(child, self)
 | 
					                    child = wrapper_element(child, self)
 | 
				
			||||||
                try:
 | 
					                try:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,9 @@
 | 
				
			|||||||
from .element import element
 | 
					from .element import element
 | 
				
			||||||
from .tabular import tabular
 | 
					from .tabular import tabular
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import itertools
 | 
				
			||||||
 | 
					import pandas as pd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class table(element):
 | 
					class table(element):
 | 
				
			||||||
    """ Class representing a latex figure. """
 | 
					    """ Class representing a latex figure. """
 | 
				
			||||||
@@ -47,14 +50,43 @@ class table(element):
 | 
				
			|||||||
        })
 | 
					        })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def multicolumn(n, s=''):
 | 
				
			||||||
 | 
					    if n == 1:
 | 
				
			||||||
 | 
					        return s
 | 
				
			||||||
 | 
					    return '\\multicolumn{' + str(n) + '}{c}{' + s + '}'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class tabledf(table):
 | 
					class tabledf(table):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, df, header=None, **kargs):
 | 
					    def __init__(self, df, header=None, multilevels=False, fmt=None, **kargs):
 | 
				
			||||||
        super().__init__(**kargs)
 | 
					        super().__init__(**kargs)
 | 
				
			||||||
        if header is None:
 | 
					        if header is None:
 | 
				
			||||||
            header = df.columns
 | 
					            header = self.create_header(df)
 | 
				
			||||||
        tab = tabular(columns='r' * len(header))
 | 
					            multilevels = True
 | 
				
			||||||
 | 
					            df = df.reset_index()
 | 
				
			||||||
 | 
					        if multilevels:
 | 
				
			||||||
 | 
					            ncols = len(header[-1])
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            ncols = len(header)
 | 
				
			||||||
 | 
					        tab = tabular(columns='r' * ncols)
 | 
				
			||||||
 | 
					        if multilevels:
 | 
				
			||||||
 | 
					            tab.addrows(header)
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
            tab.addrow(header)
 | 
					            tab.addrow(header)
 | 
				
			||||||
        tab.addhline()
 | 
					        tab.addhline()
 | 
				
			||||||
        tab.addrows(df.as_matrix())
 | 
					        tab.addrows(df.as_matrix(), fmt=fmt)
 | 
				
			||||||
        self.add(tab)
 | 
					        self.add(tab)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def create_header(self, df):
 | 
				
			||||||
 | 
					        cols = df.columns
 | 
				
			||||||
 | 
					        if not isinstance(cols, pd.MultiIndex):
 | 
				
			||||||
 | 
					            return df.columns
 | 
				
			||||||
 | 
					        idx = df.index.names
 | 
				
			||||||
 | 
					        header = []
 | 
				
			||||||
 | 
					        for levels, labels in zip(cols.levels, cols.labels):
 | 
				
			||||||
 | 
					            tmp = [multicolumn(len(idx))]
 | 
				
			||||||
 | 
					            for i, g in itertools.groupby(labels):
 | 
				
			||||||
 | 
					                tmp.append(multicolumn(len(list(g)), levels[i]))
 | 
				
			||||||
 | 
					            header.append(tmp)
 | 
				
			||||||
 | 
					        header[-1] = idx + header[-1][1:]  # Update last row
 | 
				
			||||||
 | 
					        return header
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,17 +8,23 @@ class row_element(element):
 | 
				
			|||||||
    """ Class representing a row of a tabular element. """
 | 
					    """ Class representing a row of a tabular element. """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, columns, parent, label=None,
 | 
					    def __init__(self, columns, parent, label=None,
 | 
				
			||||||
                 wrapper=False, endline=False):
 | 
					                 wrapper=False, endline=False, fmt=None):
 | 
				
			||||||
        super().__init__(parent, columns, label=label)
 | 
					        super().__init__(parent, columns, label=label, raw=(fmt is not None))
 | 
				
			||||||
        self.wrapper = wrapper
 | 
					        self.wrapper = wrapper
 | 
				
			||||||
        self.endline = endline
 | 
					        self.endline = endline
 | 
				
			||||||
 | 
					        self.__fmt = fmt
 | 
				
			||||||
 | 
					        if self.__fmt:
 | 
				
			||||||
 | 
					            self.raw = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def content(self):
 | 
					    def content(self):
 | 
				
			||||||
        wp = str
 | 
					        row = self.childrens
 | 
				
			||||||
 | 
					        if self.__fmt:
 | 
				
			||||||
 | 
					            row = map(self.__fmt, self.childrens)
 | 
				
			||||||
        if self.wrapper:
 | 
					        if self.wrapper:
 | 
				
			||||||
            cw = 1/len(self.childrens)
 | 
					            cw = 1/len(self.childrens)
 | 
				
			||||||
            wp = lambda c: str(self.wrapper(c, width=cw))
 | 
					            wp = lambda c: self.wrapper(c, width=cw)
 | 
				
			||||||
        out = ' & '.join(map(wp, self.childrens))
 | 
					            row = map(wp, row)
 | 
				
			||||||
 | 
					        out = ' & '.join(map(str, row))
 | 
				
			||||||
        if self.endline:
 | 
					        if self.endline:
 | 
				
			||||||
            out += '\\\\'
 | 
					            out += '\\\\'
 | 
				
			||||||
        return out
 | 
					        return out
 | 
				
			||||||
@@ -63,7 +69,7 @@ class tabular(element):
 | 
				
			|||||||
        """ Add a hline to the current tabular element. """
 | 
					        """ Add a hline to the current tabular element. """
 | 
				
			||||||
        self.addrow(hline_element(self), False)
 | 
					        self.addrow(hline_element(self), False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def addrow(self, row, wrap=None):
 | 
					    def addrow(self, row, wrap=None, fmt=None):
 | 
				
			||||||
        """ Add a row to the tabular.
 | 
					        """ Add a row to the tabular.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Parameters:
 | 
					        Parameters:
 | 
				
			||||||
@@ -71,21 +77,20 @@ class tabular(element):
 | 
				
			|||||||
          - wrap Can contains a wrapper element (typically a resizebox) for
 | 
					          - wrap Can contains a wrapper element (typically a resizebox) for
 | 
				
			||||||
            the cell of the row.
 | 
					            the cell of the row.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        if not isinstance(row, row_element):
 | 
					 | 
				
			||||||
            row = row_element(row, parent=self)
 | 
					 | 
				
			||||||
        if wrap is None:
 | 
					        if wrap is None:
 | 
				
			||||||
            wrap = self.autowrap
 | 
					            wrap = self.autowrap
 | 
				
			||||||
        row.wrapper = wrap
 | 
					        if not isinstance(row, row_element):
 | 
				
			||||||
 | 
					            row = row_element(row, fmt=fmt, wrapper=wrap, parent=self)
 | 
				
			||||||
        if self.childrens:
 | 
					        if self.childrens:
 | 
				
			||||||
            last = self.childrens[-1]
 | 
					            last = self.childrens[-1]
 | 
				
			||||||
            if isinstance(last, row_element):
 | 
					            if isinstance(last, row_element):
 | 
				
			||||||
                last.endline = True
 | 
					                last.endline = True
 | 
				
			||||||
        self.add(row)
 | 
					        self.add(row)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def addrows(self, rows):
 | 
					    def addrows(self, rows, **kargs):
 | 
				
			||||||
        """ Add multiple rows to the tabular (see addrow). """
 | 
					        """ Add multiple rows to the tabular (see addrow). """
 | 
				
			||||||
        for row in rows:
 | 
					        for row in rows:
 | 
				
			||||||
            self.addrow(row)
 | 
					            self.addrow(row, **kargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def content(self):
 | 
					    def content(self):
 | 
				
			||||||
        return self.fmt().format('content', {
 | 
					        return self.fmt().format('content', {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user