feat: apply d3NumberFormat to table reports (#17336)
This commit is contained in:
parent
4e9f812dab
commit
03a2c6ee8a
|
|
@ -262,9 +262,28 @@ def pivot_table(df: pd.DataFrame, form_data: Dict[str, Any]) -> pd.DataFrame:
|
|||
)
|
||||
|
||||
|
||||
def table(df: pd.DataFrame, form_data: Dict[str, Any]) -> pd.DataFrame:
|
||||
"""
|
||||
Table.
|
||||
"""
|
||||
# apply `d3NumberFormat` to columns, if present
|
||||
column_config = form_data.get("column_config", {})
|
||||
for column, config in column_config.items():
|
||||
if "d3NumberFormat" in config:
|
||||
format_ = "{:" + config["d3NumberFormat"] + "}"
|
||||
try:
|
||||
df[column] = df[column].apply(format_.format)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
# if we can't format the column for any reason, send as is
|
||||
pass
|
||||
|
||||
return df
|
||||
|
||||
|
||||
post_processors = {
|
||||
"pivot_table": pivot_table,
|
||||
"pivot_table_v2": pivot_table_v2,
|
||||
"table": table,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
import pandas as pd
|
||||
|
||||
from superset.charts.post_processing import pivot_df
|
||||
from superset.charts.post_processing import pivot_df, table
|
||||
|
||||
|
||||
def test_pivot_df_no_cols_no_rows_single_metric():
|
||||
|
|
@ -684,7 +684,6 @@ def test_pivot_df_complex():
|
|||
show_columns_total=True,
|
||||
apply_metrics_on_rows=True,
|
||||
)
|
||||
print(pivoted.to_markdown())
|
||||
assert (
|
||||
pivoted.to_markdown()
|
||||
== """
|
||||
|
|
@ -729,3 +728,56 @@ def test_pivot_df_complex():
|
|||
| ('Total (Sum as Fraction of Columns)', '') | 1 | 1 | 1 | 1 |
|
||||
""".strip()
|
||||
)
|
||||
|
||||
|
||||
def test_table():
|
||||
"""
|
||||
Test that the table reports honor `d3NumberFormat`.
|
||||
"""
|
||||
df = pd.DataFrame.from_dict({"count": {0: 80679663}})
|
||||
form_data = {
|
||||
"adhoc_filters": [
|
||||
{
|
||||
"clause": "WHERE",
|
||||
"comparator": "NULL",
|
||||
"expressionType": "SIMPLE",
|
||||
"filterOptionName": "filter_ameaka2efjv_rfv1et5nwng",
|
||||
"isExtra": False,
|
||||
"isNew": False,
|
||||
"operator": "!=",
|
||||
"sqlExpression": None,
|
||||
"subject": "lang_at_home",
|
||||
}
|
||||
],
|
||||
"all_columns": [],
|
||||
"color_pn": True,
|
||||
"column_config": {"count": {"d3NumberFormat": ",d"}},
|
||||
"conditional_formatting": [],
|
||||
"datasource": "8__table",
|
||||
"extra_form_data": {},
|
||||
"granularity_sqla": "time_start",
|
||||
"groupby": ["lang_at_home"],
|
||||
"metrics": ["count"],
|
||||
"order_by_cols": [],
|
||||
"order_desc": True,
|
||||
"percent_metrics": [],
|
||||
"query_mode": "aggregate",
|
||||
"row_limit": "15",
|
||||
"server_page_length": 10,
|
||||
"show_cell_bars": True,
|
||||
"table_timestamp_format": "smart_date",
|
||||
"time_grain_sqla": "P1D",
|
||||
"time_range": "No filter",
|
||||
"time_range_endpoints": ["inclusive", "exclusive"],
|
||||
"url_params": {},
|
||||
"viz_type": "table",
|
||||
}
|
||||
formatted = table(df, form_data)
|
||||
assert (
|
||||
formatted.to_markdown()
|
||||
== """
|
||||
| | count |
|
||||
|---:|:-----------|
|
||||
| 0 | 80,679,663 |
|
||||
""".strip()
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in New Issue