Code¶
-
dashtable.
html2rst
(html_string, force_headers=False, center_cells=False, center_headers=False)¶ 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 | #. Ordered 1 | | | - List item 2 | #. Ordered 2 | +---------------------+----------------+--------------+
-
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.
html2data
(html_string)¶ 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.
data2rst
(table, spans=[[[0, 0]]], use_headers=True, center_cells=False, center_headers=False)¶ 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
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 span rows.", "- Cells\n-contain\n-blocks"], ... ["body row 4", "", ""], ... ] >>> print(dashtable.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.
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.
data2simplerst
(table, spans=[[[0, 0]]], use_headers=True, headers_row=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)) ====== ===== ====== 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.
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