Skip to content

Commit

Permalink
12 html output (#13)
Browse files Browse the repository at this point in the history
* #12 constructs simple HTML table

* no row or column spans

* test with py3.6 now

* #12 docs
  • Loading branch information
prjemian committed Apr 3, 2018
1 parent 3e477f6 commit 9c1c858
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 13 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -10,6 +10,7 @@ sudo: false
python:
- "2.7"
- "3.5"
- "3.6"

install:
- pip install -r requirements.txt
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.rst
Expand Up @@ -7,6 +7,11 @@ Change History
Production
**********

:2018.4.0:

* `#12 <https://github.com/prjemian/pyRestTable/issues/12>`_
provide HTML table output format

:2017.2.0:

* `#9 <https://github.com/prjemian/pyRestTable/issues/9>`_
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -10,7 +10,7 @@ that does not span any cells (no rowspans or columnspans).

:author: Pete R. Jemian
:email: prjemian@gmail.com
:copyright: 2014-2017, Pete R. Jemian
:copyright: 2014-2018, Pete R. Jemian
:license: Creative Commons Attribution 4.0 International Public License (see *LICENSE.txt*)
:docs: http://pyRestTable.readthedocs.io
:URL: https://github.com/prjemian/pyRestTable
Expand Down
51 changes: 51 additions & 0 deletions docs/source/examples/html.rst
@@ -0,0 +1,51 @@
*html*
######

:see: https://www.w3schools.com/html/html_tables.asp

These python commands:

.. code-block:: python
:linenos:
import pyRestTable
t = pyRestTable.Table()
t.labels = ('one', 'two', 'three' )
t.rows.append( ['1,1', '1,2', '1,3',] )
t.rows.append( ['2,1', '2,2', '2,3',] )
t.rows.append( ['3,1', '3,2', '3,3',] )
t.rows.append( ['4,1', '4,2', '4,3',] )
print(t.reST(fmt='html'))
build this table in HTML source code:

.. code-block:: guess
:linenos:
<table>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
</tr>
<tr>
<td>1,1</td>
<td>1,2</td>
<td>1,3</td>
</tr>
<tr>
<td>2,1</td>
<td>2,2</td>
<td>2,3</td>
</tr>
<tr>
<td>3,1</td>
<td>3,2</td>
<td>3,3</td>
</tr>
<tr>
<td>4,1</td>
<td>4,2</td>
<td>4,3</td>
</tr>
</table>
1 change: 1 addition & 0 deletions docs/source/examples/index.rst
Expand Up @@ -12,5 +12,6 @@ Examples are provided to demonstrate usage.
plain
grid
list-table
html
example2
cansas
48 changes: 46 additions & 2 deletions src/pyRestTable/rest_table.py
Expand Up @@ -4,7 +4,7 @@
#-----------------------------------------------------------------------------
# :author: Pete R. Jemian
# :email: prjemian@gmail.com
# :copyright: (c) 2014-2017, Pete R. Jemian
# :copyright: (c) 2014-2018, Pete R. Jemian
#
# Distributed under the terms of the Creative Commons Attribution 4.0 International Public License.
#
Expand All @@ -24,6 +24,14 @@
:meth:`setTabularColumns` set `use_tabular_columns` & `alignment` attributes
:meth:`reST` render the table in reST format
=========================== ============================================================
.. autosummary::
~Table
~example_minimal
~example_basic
~example_complicated
"""


Expand All @@ -32,7 +40,8 @@ def _prepare_results_(t):
s += t.reST(fmt='plain') + '\n'
s += t.reST(fmt='simple') + '\n'
s += t.reST(fmt='grid') + '\n'
s += t.reST(fmt='list-table')
s += t.reST(fmt='list-table') + '\n'
s += t.reST(fmt='html')
return s


Expand Down Expand Up @@ -86,6 +95,27 @@ class Table(object):
http://sphinx-doc.org/markup/misc.html?highlight=tabularcolumns#directive-tabularcolumns
:param bool longtable: with `use_tabular_columns`,
if True, add Sphinx `:longtable:` directive
MAIN METHODS
.. autosummary::
~addLabel
~addRow
~reST
SUPPORTING METHODS
.. autosummary::
~setLongTable
~setTabularColumns
~plain_table
~simple_table
~grid_table
~list_table
~html_table
"""

def __init__(self):
Expand Down Expand Up @@ -150,6 +180,7 @@ def reST(self, indentation = '', fmt = 'simple'):
'complex': self.grid_table, # alias for `grid`, do not deprecate
'grid': self.grid_table,
'list-table': self.list_table,
'html': self.html_table,
}[fmt](indentation)

def plain_table(self, indentation = ''):
Expand Down Expand Up @@ -278,6 +309,19 @@ def multiline(cell, prefix, indentation, fmt):

return '\n'.join(rest)

def html_table(self, indentation = ''):
"""render the table in *HTML*"""
html = "<table>\n"
html += ' <tr>\n' # start the labels
html += "".join([" <th>{}</th>\n".format(k) for k in self.labels]) # labels
html += ' </tr>\n' # end the labels
for row in self.rows:
html += ' <tr>\n' # start each row
html += "".join([" <td>{}</td>\n".format(k) for k in row]) # each row
html += ' </tr>\n' # end each row
html += '</table>' # end of table
return html

def _row(self, row, fmt, indentation = ''):
"""
Given a list of entry nodes in this table row,
Expand Down
64 changes: 54 additions & 10 deletions tests/test_results.py
Expand Up @@ -80,14 +80,25 @@
- 2\
'''

MINIMAL_HTML_RESULT = '''\
<table>
<tr>
<th>x</th>
<th>y</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>\
'''


EXAMPLE_MINIMAL_RESULT = MINIMAL_PLAIN_RESULT
EXAMPLE_MINIMAL_RESULT += '\n'
EXAMPLE_MINIMAL_RESULT += MINIMAL_SIMPLE_RESULT
EXAMPLE_MINIMAL_RESULT += '\n'
EXAMPLE_MINIMAL_RESULT += MINIMAL_GRID_RESULT
EXAMPLE_MINIMAL_RESULT += '\n'
EXAMPLE_MINIMAL_RESULT += MINIMAL_LISTTABLE_RESULT
EXAMPLE_MINIMAL_RESULT = MINIMAL_PLAIN_RESULT + '\n'
EXAMPLE_MINIMAL_RESULT += MINIMAL_SIMPLE_RESULT + '\n'
EXAMPLE_MINIMAL_RESULT += MINIMAL_GRID_RESULT + '\n'
EXAMPLE_MINIMAL_RESULT += MINIMAL_LISTTABLE_RESULT + '\n'
EXAMPLE_MINIMAL_RESULT += MINIMAL_HTML_RESULT


EXAMPLE_BASIC_RESULT = '''\
Expand Down Expand Up @@ -136,7 +147,36 @@
- 3,3
* - 4,1
- 4,2
- 4,3'''
- 4,3
\
<table>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
</tr>
<tr>
<td>1,1</td>
<td>1,2</td>
<td>1,3</td>
</tr>
<tr>
<td>2,1</td>
<td>2,2</td>
<td>2,3</td>
</tr>
<tr>
<td>3,1</td>
<td>3,2</td>
<td>3,3</td>
</tr>
<tr>
<td>4,1</td>
<td>4,2</td>
<td>4,3</td>
</tr>
</table>\
'''


EXAMPLE_COMPLICATED_RESULT = '''
Expand Down Expand Up @@ -170,7 +210,7 @@ def setUp(self):

def apply_test(self, table, reference_text, style='simple'):
text = table.reST(fmt=style)
self.assertTrue(text == reference_text)
self.assertTrue(text.strip() == reference_text.strip())

def population_table(self):
t = pyRestTable.Table()
Expand Down Expand Up @@ -231,10 +271,14 @@ def test_minimal_grid(self):
def test_minimal_listtable(self):
self.apply_test(pyRestTable.rest_table.example_minimal(), MINIMAL_LISTTABLE_RESULT, 'list-table')

def test_minimal_htmltable(self):
table = pyRestTable.rest_table.example_minimal()
self.apply_test(table, MINIMAL_HTML_RESULT, 'html')

def test_example_basic(self):
t = pyRestTable.rest_table.example_basic()
s = pyRestTable.rest_table._prepare_results_(t)
self.assertEqual(s, EXAMPLE_BASIC_RESULT)
self.assertEqual(s.strip(), EXAMPLE_BASIC_RESULT.strip())

def test_example_complicated(self):
t = pyRestTable.rest_table.example_complicated()
Expand Down

0 comments on commit 9c1c858

Please sign in to comment.