Add support for ploytle table as list of lists (not just pandas DataFrame)

This commit is contained in:
allegroai 2020-11-01 15:59:35 +02:00
parent c630ba3ea3
commit 665bfc5ca8

View File

@ -477,7 +477,7 @@ def create_plotly_table(table_plot, title, series, layout_config=None):
""" """
Create a basic Plotly table json style to be sent Create a basic Plotly table json style to be sent
:param table_plot: the output table in pandas.DataFrame structure :param table_plot: the output table in pandas.DataFrame structure or list of rows (list) in a table
:param title: Title (AKA metric) :param title: Title (AKA metric)
:type title: str :type title: str
:param series: Series (AKA variant) :param series: Series (AKA variant)
@ -485,17 +485,21 @@ def create_plotly_table(table_plot, title, series, layout_config=None):
:param layout_config: additional configuration layout :param layout_config: additional configuration layout
:return: dict with plotly data. :return: dict with plotly data.
""" """
if not pd: if table_plot and isinstance(table_plot, (list, tuple)) and table_plot[0] and isinstance(table_plot[0], (list, tuple)):
raise UsageError( headers_values = table_plot[0]
"pandas is required in order to support reporting tables using CSV or a URL, " cells_values = [list(i) for i in zip(*table_plot[1:])]
"please install the pandas python package" else:
) if not pd:
index_added = not isinstance(table_plot.index, pd.RangeIndex) raise UsageError(
headers_values = list([col] for col in table_plot.columns) "pandas is required in order to support reporting tables using CSV or a URL, "
cells_values = table_plot.T.values.tolist() "please install the pandas python package"
if index_added: )
headers_values.insert(0, table_plot.index.name or "") index_added = not isinstance(table_plot.index, pd.RangeIndex)
cells_values.insert(0, table_plot.index.values.tolist()) headers_values = list([col] for col in table_plot.columns)
cells_values = table_plot.T.values.tolist()
if index_added:
headers_values.insert(0, table_plot.index.name or "")
cells_values.insert(0, table_plot.index.values.tolist())
ret = { ret = {
"data": [{ "data": [{