Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(csv_export): use custom CSV_EXPORT parameters in pd.read_csv for pivot table #30961

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
49 changes: 31 additions & 18 deletions superset/charts/post_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import numpy as np
import pandas as pd
from flask import current_app
from flask_babel import gettext as __

from superset.common.chart_data import ChartDataResultFormat
Expand All @@ -44,6 +45,8 @@
from superset.connectors.sqla.models import BaseDatasource
from superset.models.sql_lab import Query

csv_export_settings = current_app.config.get("CSV_EXPORT")


def get_column_key(label: tuple[str, ...], metrics: list[str]) -> tuple[Any, ...]:
"""
Expand Down Expand Up @@ -327,7 +330,12 @@ def apply_post_process(
if query["result_format"] == ChartDataResultFormat.JSON:
df = pd.DataFrame.from_dict(data)
elif query["result_format"] == ChartDataResultFormat.CSV:
df = pd.read_csv(StringIO(data))
df = pd.read_csv(
StringIO(data),
sep=csv_export_settings.get("sep", ","),
encoding=csv_export_settings.get("encoding", "utf-8"),
decimal=csv_export_settings.get("decimal", "."),
)

# convert all columns to verbose (label) name
if datasource:
Expand All @@ -340,27 +348,32 @@ def apply_post_process(
query["coltypes"] = extract_dataframe_dtypes(processed_df, datasource)
query["rowcount"] = len(processed_df.index)

# Flatten hierarchical columns/index since they are represented as
# `Tuple[str]`. Otherwise encoding to JSON later will fail because
# maps cannot have tuples as their keys in JSON.
processed_df.columns = [
" ".join(str(name) for name in column).strip()
if isinstance(column, tuple)
else column
for column in processed_df.columns
]
processed_df.index = [
" ".join(str(name) for name in index).strip()
if isinstance(index, tuple)
else index
for index in processed_df.index
]

if query["result_format"] == ChartDataResultFormat.JSON:
# Flatten hierarchical columns/index since they are represented as
# `Tuple[str]`. Otherwise encoding to JSON later will fail because
# maps cannot have tuples as their keys in JSON.
processed_df.columns = [
" ".join(str(name) for name in column).strip()
if isinstance(column, tuple)
else column
for column in processed_df.columns
]
processed_df.index = [
" ".join(str(name) for name in index).strip()
if isinstance(index, tuple)
else index
for index in processed_df.index
]
query["data"] = processed_df.to_dict()

elif query["result_format"] == ChartDataResultFormat.CSV:
buf = StringIO()
processed_df.to_csv(buf)
processed_df.to_csv(
buf,
sep=csv_export_settings.get("sep", ","),
encoding=csv_export_settings.get("encoding", "utf-8"),
decimal=csv_export_settings.get("decimal", "."),
)
buf.seek(0)
query["data"] = buf.getvalue()

Expand Down
Loading