Code Monkey home page Code Monkey logo

Comments (7)

dosubot avatar dosubot commented on August 28, 2024 2

To resolve the issue of special characters appearing when exporting Arabic data as a CSV from SQL Lab after upgrading to Superset version 4.0.1, you need to ensure that the CSV export is using the correct encoding. The df_to_escaped_csv function should specify the encoding as 'utf-8'.

Here is the relevant part of the code with the necessary modification:

csv_data = df_to_escaped_csv(df, index=False, encoding='utf-8', **config["CSV_EXPORT"])

This ensures that the CSV file is encoded in UTF-8, which should handle Arabic characters correctly. The df_to_escaped_csv function is defined in the superset/utils/csv.py file and is responsible for escaping special characters and exporting the DataFrame to a CSV format [1][2].

To continue talking to Dosu, mention @dosu.

from superset.

dosubot avatar dosubot commented on August 28, 2024 2

You should add the given code snippet inside the SqlResultExportCommand class, specifically in the run method, after the DataFrame df is created and before any further processing is done on it. Here is the modified method:

class SqlResultExportCommand(BaseCommand):
    # ... other methods ...

    def run(
        self,
    ) -> SqlExportResult:
        self.validate()
        blob = None
        if results_backend and self._query.results_key:
            logger.info(
                "Fetching CSV from results backend [%s]", self._query.results_key
            )
            blob = results_backend.get(self._query.results_key)
        if blob:
            logger.info("Decompressing")
            payload = utils.zlib_decompress(
                blob, decode=not results_backend_use_msgpack
            )
            obj = _deserialize_results_payload(
                payload, self._query, cast(bool, results_backend_use_msgpack)
            )
            df = pd.DataFrame(
                data=obj["data"],
                dtype=object,
                columns=[c["name"] for c in obj["columns"]],
            )

            logger.info("Using pandas to convert to CSV")
        else:
            logger.info("Running a query to turn into CSV")
            if self._query.select_sql:
                sql = self._query.select_sql
                limit = None
            else:
                sql = self._query.executed_sql
                limit = ParsedQuery(
                    sql,
                    engine=self._query.database.db_engine_spec.engine,
                ).limit
            if limit is not None and self._query.limiting_factor in {
                LimitingFactor.QUERY,
                LimitingFactor.DROPDOWN,
                LimitingFactor.QUERY_AND_DROPDOWN,
            }:
                # remove extra row from `increased_limit`
                limit -= 1
            df = self._query.database.get_df(
                sql,
                self._query.catalog,
                self._query.schema,
            )[:limit]

        # Add the given code snippet here
        csv_data = csv.df_to_escaped_csv(df, index=False, encoding='utf-8', **config["CSV_EXPORT"])

        return {
            "query": self._query,
            "count": len(df.index),
            "data": csv_data,
        }

This ensures that the DataFrame df is converted to an escaped CSV format right after it is created [1].

To continue talking to Dosu, mention @dosu.

from superset.

dosubot avatar dosubot commented on August 28, 2024 2

The SqlResultExportCommand class is located in the superset/commands/sql_lab/export.py file. You can find and modify the run method in this file to include the necessary code snippet for handling the CSV export with the correct encoding.

To continue talking to Dosu, mention @dosu.

from superset.

Habeeb556 avatar Habeeb556 commented on August 28, 2024 1

The issue has been resolved by downgrading the package with the following command: pip install Werkzeug==2.3.8.

from superset.

Habeeb556 avatar Habeeb556 commented on August 28, 2024

@dosu

Where I should add this part of the code?

csv_data = df_to_escaped_csv(df, index=False, encoding='utf-8', **config["CSV_EXPORT"])

from superset.

Habeeb556 avatar Habeeb556 commented on August 28, 2024

@dosu

Where is this class file path?

from superset.

Habeeb556 avatar Habeeb556 commented on August 28, 2024

This did not solve the problem. I am still facing the same issue with version 4.0.1, while version 2.1.3 did not have this problem. I am unsure if it is related to the Python version or something else.

from superset.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.