def test_tohtml_caption(): # exercise function table = (('foo', 'bar'), ('a', 1), ('b', (1, 2))) f = NamedTemporaryFile(delete=False) tohtml(table, f.name, caption='my table', lineterminator='\n') # check what it did with open(f.name, 'rb') as o: actual = o.read() expect = """<table class='petl'> <caption>my table</caption> <thead> <tr> <th>foo</th> <th>bar</th> </tr> </thead> <tbody> <tr> <td>a</td> <td style='text-align: right'>1</td> </tr> <tr> <td>b</td> <td>(1, 2)</td> </tr> </tbody> </table> """ eq_(expect, actual)
def test_tohtml(): # exercise function table = (('foo', 'bar'), ('a', 1), ('b', (1, 2))) f = NamedTemporaryFile(delete=False) tohtml(table, f.name) # check what it did with open(f.name, 'rb') as o: actual = o.read() expect = """<table> <thead> <tr> <th>foo</th> <th>bar</th> </tr> </thead> <tbody> <tr> <td>'a'</td> <td style='text-align: right'>1</td> </tr> <tr> <td>'b'</td> <td>(1, 2)</td> </tr> </tbody> </table> """ eq_(expect, actual)
def test_tohtml_caption(): # exercise function table = (('foo', 'bar'), ('a', 1), ('b', (1, 2))) f = NamedTemporaryFile(delete=False) tohtml(table, f.name, caption='my table', lineterminator='\n') # check what it did with open(f.name, 'rb') as o: actual = o.read() expect = """<table> <caption>my table</caption> <thead> <tr> <th>foo</th> <th>bar</th> </tr> </thead> <tbody> <tr> <td>a</td> <td style='text-align: right'>1</td> </tr> <tr> <td>b</td> <td>(1, 2)</td> </tr> </tbody> </table> """ eq_(expect, actual)
def test_tohtml(): # exercise function table = (('foo', 'bar'), ('a', 1), ('b', 2)) f = NamedTemporaryFile(delete=False) tohtml(table, f.name) # check what it did with open(f.name, 'rb') as o: actual = o.read() expect = """<table> <thead> <tr> <th>foo</th> <th>bar</th> </tr> </thead> <tbody> <tr> <td>a</td> <td style='text-align: right'>1</td> </tr> <tr> <td>b</td> <td style='text-align: right'>2</td> </tr> </tbody> </table> """ eq_(expect, actual)
def get_load_result(nameFile): table1 = etl.fromjson('./static/data/tabalaElegidaCalculadora.json') tocsv(table1, './exelFiles/' + str(nameFile) + '.csv') etl.tohtml(table1, './exelFiles/' + str(nameFile) + '.html', caption=str(nameFile)) return jsonify(True)
def test_stdoutsource_unicode(): tbl = [('foo', 'bar'), (u'Արամ Խաչատրյան', 1), (u'Johann Strauß', 2)] etl.tocsv(tbl, StdoutSource(), encoding='utf-8') etl.tohtml(tbl, StdoutSource(), encoding='utf-8') etl.topickle(tbl, StdoutSource())
def to_html(self, local_path=None, encoding=None, errors='strict', index_header=False, caption=None, tr_style=None, td_styles=None, truncate=None): """ Outputs table to html. .. warning:: If a file already exists at the given location, it will be overwritten. `Args:` local_path: str The path to write the html locally. If not specified, a temporary file will be created and returned, and that file will be removed automatically when the script is done running. encoding: str The encoding type for `csv.writer() <https://docs.python.org/2/library/csv.html#csv.writer/>`_ errors: str Raise an Error if encountered index_header: boolean Prepend index to column names; Defaults to False. caption: str A caption to include with the html table. tr_style: str or callable Style to be applied to the table row. td_styles: str, dict or callable Styles to be applied to the table cells. truncate: int Length of cell data. `Returns:` str The path of the new file """ if not local_path: local_path = files.create_temp_file(suffix=".html") petl.tohtml(self.table, source=local_path, encoding=encoding, errors=errors, caption=caption, index_header=index_header, tr_style=tr_style, td_styles=td_styles, truncate=truncate) return local_path
from __future__ import division, print_function, absolute_import # tohtml() ########## import petl as etl table1 = [['foo', 'bar'], ['a', 1], ['b', 2], ['c', 2]] etl.tohtml(table1, 'example.html', caption='example table') print(open('example.html').read())
def test_stdoutsource(): tbl = [('foo', 'bar'), ('a', 1), ('b', 2)] etl.tocsv(tbl, StdoutSource(), encoding='ascii') etl.tohtml(tbl, StdoutSource(), encoding='ascii') etl.topickle(tbl, StdoutSource())
def get_html_data(csv_data): petl.tohtml(csv_data, HTML_TABLE_FILE) with open(HTML_TABLE_FILE, 'r') as f: html_data = f.read() return html_data