Exemplo n.º 1
0
 def html(self,cell_args={},**kwargs):
     """return in HTML format"""
     res=''
     for i,v in enumerate(self.data):
         cell=Cell(v,self.align[i],self.fmt[i],self.tag[i],self.style[i])
         res+=cell.html(**cell_args)
     return tag('tr',res,**kwargs)
Exemplo n.º 2
0
    def html(self, head=None, foot=None, **kwargs):
        """HTML representation of table

        :param head: optional column headers, .titles by default
        :param foot: optional column footers, .footer by default
        :param style: style for the table
        :param colstyle: list of dict of style attributes for each column
        :param kwargs: optional parameters passed along to tag('table'...
            except:
            * start=optional start row
            * stop=optional end row
            used to display a subset of lines. in this case rows with '...' cells
            are displayed before and/or after the lines
        :return: string HTML representation of table
        """

        def TR(data, align=None, fmt=None, tag=None, style={}):
            res = ''
            row = Row(data=data, align=align, fmt=fmt, tag=tag, style=style)
            res += row.html()+'\n'
            return res

        def THEAD(data, fmt=None, style={}):
            res = "<thead>\n"
            res += TR(data=data, fmt=fmt, tag='th', style=style)
            res += "</thead>\n"
            return res

        def TFOOT(data, fmt=None, style={}):
            res = "<tfoot>\n"
            res += TR(data=data, fmt=fmt, tag='th', style=style)
            res += "</tfoot>\n"
            return res

        res = ''

        colstyle = kwargs.pop('colstyle', None)

        if head is None:
            head = self.titles
        if head:
            res += THEAD(head)
        start = kwargs.pop('start', 0)
        stop = kwargs.pop('stop', len(self))
        if start != 0:
            res += TR(['...']*self.ncols(), style=colstyle)
        for row in self[start:stop]:
            res += TR(row, style=colstyle)
        if stop != -1 and stop < len(self):
            res += TR(['...']*self.ncols(), style=colstyle)
        if foot is None:
            foot = self.footer
        if foot:
            res += TFOOT(foot)

        # kwargs['style'] = kwargs.pop('style', {'table-layout': 'fixed'})  # to prevent wrapping

        return markup.tag('table', res, **kwargs)+'\n'
Exemplo n.º 3
0
 def html(self,**kwargs):
     """:return: string HTML formatted cell
     * if data is int, default align="right"
     * if data is float, default align="right" and fmt='%0.2f'
     * if data is :class:`~datetime.timedelta`, align = "right" and formatting is done by :func:`datetime2.strftimedelta`
     """
     args={}
     args.update(kwargs) #copy needed to avoid side effects
     v=self.data
     a=args.get('align',self.align)
     f=self.fmt
     if isinstance(v,int):
         if not a: a="right"
     elif isinstance(v,float):
         if not a: a="right"
         if not f: f='%0.2f'
         v=f%v
         f=None #don't reformat below
     elif isinstance(v,date):
         if not a: a="right"
         if not f: f='%Y-%m-%d'
         v=v.strftime(f)
         f=None #don't reformat below
     elif isinstance(v,timedelta):
         if not a: a="right"
         if not f: f='%H:%M:%S'
         v=strftimedelta(v,f)
         f=None #don't reformat below
         
     attr='' #tag attributes
     if a:
         args['align']=a #side effect warning
         
     if not v or v=='':
         v="&nbsp;" #for IE8
     else:
         v=f%v if f else str(v)
     if self.style:
         args.setdefault('style',self.style)
     return tag(self.tag,v,**args)
Exemplo n.º 4
0
 def html(self,head=None,foot=None,colstyle=None,**kwargs):
     """:return: string HTML representation of table"""
             
     def TR(data,align=None,fmt=None,tag=None,style=None):
         if not isinstance(data[0],list):
             data=[data]
         res=''
         for line in data:
             row=Row(data=line,align=align,fmt=fmt,tag=tag,style=style)
             res+=row.html()+'\n'
         return res
         
     def THEAD(data,fmt=None,style=None):
         res="<thead>\n"
         res+=TR(data=data,fmt=fmt,tag='th',style=style)
         res+="</thead>\n"
         return res
         
     def TFOOT(data,fmt=None,style=None):
         res="<tfoot>\n"
         res+=TR(data=data,fmt=fmt,tag='th',style=style)
         res+="</tfoot>\n"
         return res
     
     res=''
         
     if head is None:
         head=self.titles
     if head is not None:
         res+=THEAD(head)
     for row in self:
         res+=TR(row,style=colstyle)  
     if foot is not None:
         res+=TFOOT(foot)             
     
     return tag('table',res,**kwargs)+'\n'
Exemplo n.º 5
0
 def html(self, cell_args={}, **kwargs):
     """return in HTML format"""
     res = ''
     for cell in self.data:
         res += cell.html(**cell_args)
     return markup.tag('tr', res, **kwargs)
Exemplo n.º 6
0
    def html(self, **kwargs):
        """:return: string HTML formatted cell:

        * if data is int, default align="right"
        * if data is float, default align="right" and fmt='%0.2f'
        * if data is :class:`~datetime.timedelta`, align = "right" and formatting is done by :func:`datetime2.strftimedelta`

        """
        args = {}
        args.update(kwargs)  # copy needed to avoid side effects

        v = self.data
        f = self.fmt

        style = args.get('style', {})
        if isinstance(style, str):
            style = markup.style_str2dict(style)

        # create style dict by merging default Cell style + parameters
        # http://stackoverflow.com/questions/9819602/union-of-dict-objects-in-python
        style = dict(self.style, **style)

        if hasattr(v, '_repr_html_'):
            try:
                v = v._repr_html_()
            except Exception as e:
                v = 'ERROR : %s _repr_html_ failed : %s' % (v, e)

        elif 'text-align' not in style:  # HTML 4 and before
            a = args.pop('align', self.align)
            if isinstance(v, int):
                if not a:
                    a = "right"
            elif isinstance(v, float):
                if not a:
                    a = "right"
                if not f:
                    f = '%0.2f'
                v = f % v
                f = None  # don't reformat below
            elif isinstance(v, date):
                if not a:
                    a = "right"
                if not f:
                    f = '%Y-%m-%d'
                v = v.strftime(f)
                f = None  # don't reformat below
            elif isinstance(v, timedelta):
                if not a:
                    a = "right"
                if not f:
                    f = '%H:%M:%S'
                v = datetime2.strftimedelta(v, f)
                f = None  # don't reformat below

            if a:
                style['text-align'] = a

        if v is None or v == '':
            v = "&nbsp;"  # for IE8
        else:
            v = f % v if f else str(v)

        if v[0] == '$' and v[-1] == '$':  # LaTeX formula : avoid line wrap
            v = '$'+v+'$'

        if style:
            args['style'] = style

        return markup.tag(self.tag, v, **args)