GitHub user dosubot[bot] added a comment to the discussion: Get dataset 
description and columns description

<!-- Answer -->
To include dataset and column descriptions in the Excel export, you can follow 
these steps:

1. **Retrieve Descriptions**: First, ensure you have access to the dataset and 
column descriptions. These are defined in various parts of the Superset 
codebase, such as in the `superset/datasets/schemas.py` file and the frontend 
interface in 
`superset-frontend/packages/superset-ui-core/src/query/types/Datasource.ts` 
<sup>[[1]](https://github.com/apache/superset/blob/master/superset/datasets/schemas.py)</sup><sup>[[2]](https://github.com/apache/superset/blob/master/superset-frontend/packages/superset-ui-core/src/query/types/Datasource.ts)</sup>.

2. **Modify `core.py`**: Although `core.py` does not directly handle 
descriptions, you can use functions like `get_column_name` and 
`get_column_names` to retrieve column metadata. You might need to extend these 
functions or create new ones to fetch descriptions if they are not already 
included 
<sup>[[3]](https://github.com/apache/superset/blob/master/tests/unit_tests/core_tests.py)</sup>.

3. **Modify `excel.py`**: In `excel.py`, you can modify the `df_to_excel` 
function to add a new sheet for descriptions. Use the `xlsxwriter` library to 
create a workbook and add worksheets. Here's a basic example of how you can add 
a new sheet for descriptions:

```python
import io
import pandas as pd
import xlsxwriter

def df_to_excel_with_descriptions(df: pd.DataFrame, dataset_description: str, 
column_descriptions: dict, **kwargs: Any) -> Any:
    output = io.BytesIO()

    # Quote formulas to prevent malicious injections
    df = quote_formulas(df)

    with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
        # Write the main data to the first sheet
        df.to_excel(writer, sheet_name='Data', **kwargs)

        # Add a new sheet for descriptions
        workbook = writer.book
        description_sheet = workbook.add_worksheet('Descriptions')

        # Write dataset description
        description_sheet.write(0, 0, 'Dataset Description')
        description_sheet.write(0, 1, dataset_description)

        # Write column descriptions
        description_sheet.write(1, 0, 'Column Name')
        description_sheet.write(1, 1, 'Description')
        row = 2
        for column, description in column_descriptions.items():
            description_sheet.write(row, 0, column)
            description_sheet.write(row, 1, description)
            row += 1

    return output.getvalue()
```

In this example, `dataset_description` is a string containing the dataset's 
description, and `column_descriptions` is a dictionary where keys are column 
names and values are their descriptions. You will need to pass these to the 
function when calling it.

By implementing these changes, you can include the dataset and column 
descriptions in a separate sheet in the exported Excel file.


<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*

---
<sup>Help Dosu learn! Give it feedback: [Great 
Response](https://app.dosu.dev/response-feedback/6373ebbc-40a1-40ae-bd70-c40954a1cd48?feedback_type=great_response)
 | [Irrelevant 
Answer](https://app.dosu.dev/response-feedback/6373ebbc-40a1-40ae-bd70-c40954a1cd48?feedback_type=irrelevant_answer)
 | [Incorrect 
Sources](https://app.dosu.dev/response-feedback/6373ebbc-40a1-40ae-bd70-c40954a1cd48?feedback_type=incorrect_sources)
 | [Too 
Verbose](https://app.dosu.dev/response-feedback/6373ebbc-40a1-40ae-bd70-c40954a1cd48?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/6373ebbc-40a1-40ae-bd70-c40954a1cd48?feedback_type=hallucination)
 | [Bug 
Report](https://app.dosu.dev/response-feedback/6373ebbc-40a1-40ae-bd70-c40954a1cd48?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/6373ebbc-40a1-40ae-bd70-c40954a1cd48?feedback_type=other)</sup>


GitHub link: 
https://github.com/apache/superset/discussions/33504#discussioncomment-13186210

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to