Code

dashtable.convert_table_spans_to_box_to_text(table: List[List[Any]], spans: Sequence[Sequence[Tuple[int, int]]] | None = None) Dict[Tuple[int, int, int, int], str]

converts tables and spans (data2rst arguments) to data2rst_enhanced argument

>>> _ = convert_table_spans_to_box_to_text
>>> spans = [
...     [ [2, 1], [2, 2] ],
... ]
>>> table = [
...     ["Header 1", "Header 2", "Header 3"],
...     ["body row 1", "column 2", "column 3"],
...     ["body row 2", "Cells may span columns.", ""],
... ]
>>> _(table, spans)
{(0, 0, 0, 0): 'Header 1', (0, 1, 0, 1): 'Header 2', (0, 2, 0, 2): 'Header 3', (1, 0, 1, 0): 'body row 1', (1, 1, 1, 1): 'column 2', (1, 2, 1, 2): 'column 3', (2, 0, 2, 0): 'body row 2', (2, 1, 2, 2): 'Cells may span columns.'}
dashtable.data2md(table)

Creates a markdown table. The first row will be headers.

Parameters:

table (list of lists of str) – A list of rows containing strings. If any of these strings consist of multiple lines, they will be converted to single line because markdown tables do not support multiline cells.

Returns:

The markdown formatted string

Return type:

str

Example

>>> table_data = [
...     ["Species", "Coolness"],
...     ["Dog", "Awesome"],
...     ["Cat", "Meh"],
... ]
>>> print(data2md(table_data))
| Species | Coolness |
|---------|----------|
|   Dog   | Awesome  |
|   Cat   |   Meh    |
dashtable.data2rst(table: ~typing.List[~typing.List[~typing.Any]], spans: ~typing.Sequence[~typing.Sequence[~typing.Tuple[int, int]]] | None = None, use_headers: bool = True, center_cells: bool = False, center_headers: bool = False, candidates_mask_creator: ~typing.Callable[[~numpy.ndarray, ~typing.Tuple[int, int, int, int]], ~numpy.ndarray] = <function get_candidates_mask_v2>, checked_mask_creator: ~typing.Callable[[~numpy.ndarray, ~typing.Callable[[~numpy.ndarray, ~typing.Tuple[int, int, int, int]], ~numpy.ndarray]], ~numpy.ndarray] = <function get_checked_mask_v2>)

Convert a list of lists of str into a reStructuredText Grid Table

Parameters:
  • table (list of lists of str) –

  • spans (list of lists of lists of int, optional) –

    These are [row, column] pairs of cells that are merged in the table. Rows and columns start in the top left of the table.For example:

    +--------+--------+
    | [0, 0] | [0, 1] |
    +--------+--------+
    | [1, 0] | [1, 1] |
    +--------+--------+
    

  • use_headers (bool, optional) – Whether or not the first row of table data will become headers.

  • center_cells (bool, optional) – Whether or not cells will be centered

  • center_headers (bool, optional) – Whether or not headers will be centered

  • candidates_mask_creator (implementation of CANDIDATES_MASK_CREATOR (optimization purposes)) –

  • checked_mask_creator (implementation of CHECKED_MASK_CREATOR (optimization purposes)) –

Returns:

The grid table string

Return type:

str

Example

>>> spans = [
...     [ [3, 1], [4, 1] ],
...     [ [3, 2], [4, 2] ],
...     [ [2, 1], [2, 2] ],
... ]
>>> table = [
...     ["Header 1", "Header 2", "Header 3"],
...     ["body row 1", "column 2", "column 3"],
...     ["body row 2", "Cells may span columns.", ""],
...     ["body row 3", "Cells may\nspan rows.", "- Cells\n- contain\n- blocks."],
...     ["body row 4", "", ""],
... ]
>>> print(data2rst(table, spans))
+------------+-------------+-----------+
| Header 1   | Header 2    | Header 3  |
+============+=============+===========+
| body row 1 | column 2    | column 3  |
+------------+-------------+-----------+
| body row 2 | Cells may span columns. |
+------------+-------------+-----------+
| body row 3 | Cells may   | - Cells   |
+------------+ span rows.  | - contain |
| body row 4 |             | - blocks. |
+------------+-------------+-----------+
dashtable.data2rst_enhanced(cell_box_to_text: Dict[Tuple[int, int, int, int], str], missing_cell_value: str = ' ') str
produces almost same results as data2rst but much faster and robust

because it uses completely different algorithm; preferred for using before html conversion; now this function is not customized to centerize texts and so on and maybe its not necessary

Parameters:
  • cell_box_to_text (dictionary { (row start, col start, row end, col end) -> cell string value };) –

    row/col ends must be included (use cuts [start; end] instead of [start;end) )

    and be integers >= 0; if all starts > 0 then the coordinates will be shifted to start from 0;

    it is not mandatory to provide all table cells including empty because they will be created automatically

  • missing_cell_value (string value for missing cells) –

Return type:

table view in rst format

>>> def _run(cells: Iterable[CELL_LOCATION]):
...     cells = {b: str(b) for b in cells}
...     assert cells
...     print(data2rst_enhanced(cells))

Simplest case:

>>> _run([(0, 0, 0, 0), (1, 1, 1, 1)])
+--------------+--------------+
| (0, 0, 0, 0) |              |
+--------------+--------------+
|              | (1, 1, 1, 1) |
+--------------+--------------+

More heavy case:

>>> _run([(0, 0, 0, 0), (1, 1, 1, 1), (0, 1, 0, 2), (1, 0, 2, 0)])
+--------------+------------------+
| (0, 0, 0, 0) | (0, 1, 0, 2)     |
+--------------+--------------+---+
| (1, 0, 2, 0) | (1, 1, 1, 1) |   |
|              +--------------+---+
|              |              |   |
+--------------+--------------+---+

This case cannot be performed by data2rst:

>>> _run([(0, 0, 0, 0), (1, 2, 1, 2), (0, 1, 0, 2), (1, 0, 1, 1)])
+--------------+----------------+
| (0, 0, 0, 0) | (0, 1, 0, 2)   |
+--------------+-+--------------+
| (1, 0, 1, 1)   | (1, 2, 1, 2) |
+----------------+--------------+

Usual test:

>>> spans = [
...     [ [3, 1], [4, 1] ],
...     [ [3, 2], [4, 2] ],
...     [ [2, 1], [2, 2] ],
... ]
>>> table = [
...     ["Header 1", "Header 2", "Header 3"],
...     ["body row 1", "column 2", "column 3"],
...     ["body row 2", "Cells may span columns.", ""],
...     ["body row 3", "Cells may\nspan rows.", "- Cells\n- contain\n- blocks."],
...     ["body row 4", "", ""],
... ]
>>> print(data2rst_enhanced(convert_table_spans_to_box_to_text(table, spans)))
+------------+------------+------------+
| Header 1   | Header 2   | Header 3   |
+------------+------------+------------+
| body row 1 | column 2   | column 3   |
+------------+------------+------------+
| body row 2 | Cells may span columns. |
+------------+------------+------------+
| body row 3 | Cells may  | - Cells    |
+------------+ span rows. | - contain  |
| body row 4 |            | - blocks.  |
+------------+------------+------------+
dashtable.data2rst_v2(table: List[List[Any]], spans: Sequence[Sequence[Tuple[int, int]]] | None = None, missing_cell_value: str = ' ') str

uses data2rst_enhanced for usual data2rst arguments

dashtable.data2simplerst(table: List[List[str]], spans: List[List[Tuple[int, int]]] | None = None, use_headers: bool = True, headers_row: int = 0)

Convert table data to a simple rst table

Parameters:
  • table (list of lists of str) – A table of strings.

  • spans (list of lists of lists of int) – A list of spans. A span is a list of [Row, Column] pairs of table cells that are joined together.

  • use_headers (bool, optional) – Whether or not to include headers in the table. A header is a cell that is underlined with “=”

  • headers_row (int) – The row that will be the headers. In a simple rst table, the headers do not need to be at the top.

Returns:

The simple rst table

Return type:

str

Example

>>> table = [
...     ["Inputs", "", "Output"],
...     ["A", "B", "A or B"],
...     ["False", "False", "False"],
...     ["True", "False", "True"],
...     ["False", "True", "True"],
...     ["True", "True", "True"],
... ]
>>> spans = [
...     [ [0, 0], [0, 1] ]
... ]
>>> print(data2simplerst(table, spans, headers_row=1).strip())
======  =====  ======
   Inputs      Output
-------------  ------
  A       B    A or B
======  =====  ======
False   False  False
 True   False   True
False   True    True
 True   True    True
======  =====  ======
dashtable.grid2data(text)

Convert Grid table to data (the kind used by Dashtable)

Parameters:

text (str) – The text must be a valid rst table

Returns:

  • table (list of lists of str)

  • spans (list of lists of lists of int) – A span is a list of [row, column] pairs that define a group of combined table cells

  • use_headers (bool) – Whether or not the table was using headers

Notes

This function requires docutils.

Example

>>> text = '''
... +------------+------------+-----------+
... | Header 1   | Header 2   | Header 3  |
... +============+============+===========+
... | body row 1 | column 2   | column 3  |
... +------------+------------+-----------+
... | body row 2 | Cells may span columns.|
... +------------+------------+-----------+
... | body row 3 | Cells may  | - Cells   |
... +------------+ span rows. | - contain |
... | body row 4 |            | - blocks. |
... +------------+------------+-----------+
... '''
>>> import dashtable
>>> table, spans, use_headers = dashtable.grid2data(text)
>>> from pprint import pprint
>>> pprint(table)
[['Header 1', 'Header 2', 'Header 3'],
 ['body row 1', 'column 2', 'column 3'],
 ['body row 2', 'Cells may span columns.', ''],
 ['body row 3', 'Cells may\nspan rows.', '- Cells\n- contain\n- blocks.'],
 ['body row 4', '', '']]
>>> print(spans)
[[[2, 1], [2, 2]], [[3, 1], [4, 1]], [[3, 2], [4, 2]]]
>>> print(use_headers)
True
dashtable.html2data(html_string: str) Tuple[List[List[str]], Sequence[Sequence[Tuple[int, int]]], bool]

Convert an html table to a data table and spans.

Parameters:

html_string (str) – The string containing the html table

Returns:

  • table (list of lists of str)

  • spans (list of lists of lists of int) – A span is a list of [row, column] pairs that define what cells are merged in a table.

  • use_headers (bool)

dashtable.html2md(html_string)

Convert a string or html file to a markdown table string.

Parameters:

html_string (str) – Either the html string, or the filepath to the html

Returns:

The html table converted to a Markdown table

Return type:

str

Notes

This function requires BeautifulSoup to work.

Example

>>> html_text = '''
... <table>
...     <tr>
...         <th>
...             Header 1
...         </th>
...         <th>
...             Header 2
...         </th>
...         <th>
...             Header 3
...         </th>
...     <tr>
...         <td>
...             <p>This is a paragraph</p>
...         </td>
...         <td>
...             Just text
...         </td>
...         <td>
...             Hot dog
...         </td>
...     </tr>
... </table>
... '''
>>> import dashtable
>>> print(dashtable.html2md(html_text))
|      Header 1       | Header 2  | Header 3 |
|---------------------|-----------|----------|
| This is a paragraph | Just text | Hot dog  |
dashtable.html2rst(html_string: str, force_headers: bool = False, center_cells: bool = False, center_headers: bool = False) str

Convert a string or html file to an rst table string.

Parameters:
  • html_string (str) – Either the html string, or the filepath to the html

  • force_headers (bool) – Make the first row become headers, whether or not they are headers in the html file.

  • center_cells (bool) – Whether or not to center the contents of the cells

  • center_headers (bool) – Whether or not to center the contents of the header cells

Returns:

The html table converted to an rst grid table

Return type:

str

Notes

This function requires BeautifulSoup to work.

Example

>>> html_text = '''
... <table>
...     <tr>
...         <th>
...             Header 1
...         </th>
...         <th>
...             Header 2
...         </th>
...         <th>
...             Header 3
...         </th>
...     <tr>
...         <td>
...             <p>This is a paragraph</p>
...         </td>
...         <td>
...             <ul>
...                 <li>List item 1</li>
...                 <li>List item 2</li>
...             </ul>
...         </td>
...         <td>
...             <ol>
...                 <li>Ordered 1</li>
...                 <li>Ordered 2</li>
...             </ol>
...         </td>
...     </tr>
... </table>
... '''
>>> import dashtable
>>> print(dashtable.html2rst(html_text))
+---------------------+---------------+--------------+
| Header 1            | Header 2      | Header 3     |
+=====================+===============+==============+
| This is a paragraph | * List item 1 | 1. Ordered 1 |
|                     | * List item 2 | 2. Ordered 2 |
+---------------------+---------------+--------------+
dashtable.simple2data(text)

Convert a simple table to data (the kind used by DashTable)

Parameters:

text (str) – A valid simple rst table

Returns:

  • table (list of lists of str)

  • spans (list of lists of lists of int) – A span is a [row, column] pair that defines a group of merged cells in the table. In a simple rst table, spans can only be colspans.

  • use_headers (bool) – Whether or not this table uses headers

  • headers_row (int) – The row where headers are located

Notes

This function requires docutils.

Example

>>> html_text = '''
... ======  =====  ======
...    Inputs      Output
... -------------  ------
...   A       B    A or B
... ======  =====  ======
... False   False  False
...  True   False   True
... False   True    True
...  True   True    True
... ======  =====  ======
... '''
>>> from dashtable import simple2data
>>> table, spans, use_headers, headers_row = simple2data(html_text)
>>> from pprint import pprint
>>> pprint(table)
[['Inputs', 'Output', ''],
 ['A', 'B', 'A or B'],
 ['False', 'False', 'False'],
 ['True', 'False', 'True'],
 ['False', 'True', 'True'],
 ['True', 'True', 'True']]
>>> print(spans)
[[[0, 0], [0, 1]]]
>>> print(use_headers)
True
>>> print(headers_row)
1
class dashtable.dashutils.CutsResizer(cuts: ndarray | Sequence[Tuple[int | float, int | float]])

Simple class provides operations to resize cuts sequences with keeping their relational intersections

ensure_min_length(index: int, value: int | float)

ensures whether the whole dimension for index have at least this length

>>> c = CutsResizer([(1, 3), (4, 5), (3, 8), (6, 7)])
>>> c.ensure_min_length(1, value=3); c
[(1, 3), (4, 6), (3, 9), (7, 8)]
>>> c.ensure_min_length(-1, value=5); c
[(1, 3), (4, 6), (3, 12), (7, 11)]
>>> c.ensure_min_length(0, 4); c
[(1, 4), (5, 7), (4, 13), (8, 12)]
ensure_min_lens(lens: ndarray | Sequence[int | float])

vectorized (fast) version of ensure_min_length for each cut

>>> c = CutsResizer([(1, 3), (4, 5), (3, 8), (6, 7)])
>>> c.ensure_min_lens([4, 3, 1, 1]); c
[(1, 4), (5, 7), (4, 10), (8, 9)]
>>> c.ensure_min_lens([4, 3, 1, 6]); c
[(1, 4), (5, 7), (4, 14), (8, 13)]
>>> c.ensure_min_lens([4, 4, 1, 6]); c
[(1, 4), (5, 8), (4, 15), (9, 14)]