#!/usr/bin/python
from __future__ import division
import itertools
from math import ceil
from cgi import escape
 
class Table:
    def __init__(self, data, per_page = None, header = True, filepattern = 'result_%s.html',escape = True):
        self.set_header = header;
        if header == True:
            self.header = list(data)[0]
            self.content = list(data)[1:]
        else:
            self.header = None
            self.content = list(data)
        self.filepattern,self.per_page,self.escape = filepattern,per_page,escape
    def safe(self,content):
        return content if not self.escape else escape(content)
    def page_num(self):
        return int(ceil(len(self.content)/self.per_page))
    def filename(self, page):
        return self.filepattern % (page,)
    def table(self, page=0, table_attrib = None, tr_attrib = None, td_attrib = None):
        if self.per_page == None:
            page_content = self.content
        else:
            page_content = itertools.islice(self.content, page*self.per_page, (page+1)*self.per_page)
        row_filter = lambda x:"A" if row_filter else y
        yield '<table%s>' % (" "+table_attrib if table_attrib else '',)
        if self.set_header:
            yield '<thead>'
            yield "\t"*1+'<tr%s>' % (" "+tr_attrib if tr_attrib else '',)
            for th in self.header:
                yield "\t"*2+'<th>'+self.safe(unicode(th))+'</th>'
            yield "\t"*1+'</tr>'
            yield '</thead>'
        yield '<tbody>'
        for tr in page_content:
            yield "\t"*1+'<td%s>' % (" "+td_attrib if td_attrib else '',)
            for td in tr:
                yield "\t"*2+'<tr%s>'  % (" "+tr_attrib if tr_attrib else '',) + self.safe(unicode(td))+ '</tr>'
            yield "\t"*1+'</td>'
        yield '</tbody>'
        yield '</table>'
 
class Table_Pure(Table):
    def paginator(self, page):
        yield '<ul class="pure-paginator">'
        if page>0:
            yield "\t"+'<li><a class="pure-button prev" href="%s">&#171;</a></li>' % (self.filename(page-1),)
        pagination_start = 0 if (page-5)<0 else (page-5)
        page_max = self.page_num()
        pagination_end = page_max if (page+5)>page_max else (page+5)
        for i in xrange(pagination_start,pagination_end):
            if i<>page:
                yield "\t"+'<li><a class="pure-button" href="%s">%s</a></li>' % (self.filename(i),i+1,)
            else:
                yield "\t"+'<li><a class="pure-button pure-button-active" href="%s">%s</a></li>' % (self.filename(i),i+1,)
        if (page+1)<page_max:
            yield "\t"+'<li><a class="pure-button next" href="%s">&#187;</a></li>' % (self.filename(page+1),)
        yield '</ul>'
    def html(self, page, title=None, description=None):
        yield '<html>'
        yield '<html>'
        yield '<title>%s</title>' % (title or "Index",)
        yield '<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.4.2/pure-min.css" type="text/css"/>'
        yield '</head>'
        yield '<body>'
        yield '<h1>%s</h1>' % (title or "Index",)
        if description:
            yield description
        yield '<h2>Strona %s z %s<h2>' % (page, self.page_num(),)
        for x in self.paginator(page):
            yield x
        for x in self.table(page,table_attrib='class="pure-table"'):
            yield x
        yield '</body>'
        yield '</html>'
it/python-html-table.txt · ostatnio zmienione: 2014/05/28 22:11 przez naczelnik
Public Domain
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0