dosubot[bot] commented on issue #37487:
URL: https://github.com/apache/superset/issues/37487#issuecomment-3805633737

   <!-- Answer -->
   Here are the files and code sections you need to customize export filenames 
in Superset:
   
   **1. Chart exports (CSV/XLSX/ZIP):**
   - File: `superset/charts/data/api.py`
   - Method: `_send_chart_response`
   - By default, this method calls `generate_download_headers("csv")` (or 
`"xlsx"`, `"zip"`) with no filename, so you get a timestamp-based name.  
   - To use the chart name and ID, modify the call like:
     ```python
     from werkzeug.utils import secure_filename
     chart_name = 
secure_filename(result["query_context"].form_data.get("slice_name", "chart"))
     chart_id = result["query_context"].form_data.get("slice_id", "id")
     filename = f"{chart_name}_{chart_id}"
     headers = generate_download_headers("csv", filename)
     ```
     Do similar for XLSX and ZIP exports.  
     
[Reference](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/charts/data/api.py#L346-L411)
   
   **2. Dashboard exports (YAML ZIP):**
   - File: `superset/dashboards/api.py`
   - Method: `export`
   - The ZIP filename is set by:
     ```python
     timestamp = datetime.now().strftime("%Y%m%dT%H%M%S")
     root = f"dashboard_export_{timestamp}"
     filename = f"{root}.zip"
     ```
   - Change this to use dashboard titles and IDs. For example:
     ```python
     from superset.utils.file import get_filename
     dashboards = [DashboardDAO.find_by_id(did) for did in requested_ids]
     name_tokens = [get_filename(d.dashboard_title, d.id) for d in dashboards 
if d]
     root = "_".join(name_tokens)
     filename = f"{root}.zip"
     ```
     
[Reference](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/dashboards/api.py#L1002-L1023)
   
   **3. Download header helper:**
   - File: `superset/views/base.py`
   - Function: `generate_download_headers`
   - This function just takes your filename and extension and sets the HTTP 
header. It does not sanitize or deduplicate, so do that before calling it.
     
[Reference](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/views/base.py#L141-L147)
   
   **Checklist:**
   - Extract chart/dashboard name and ID/UUID in the export method.
   - Sanitize the name (use `secure_filename` or similar).
   - Pass the sanitized filename to `generate_download_headers` (charts) or as 
`download_name` in `send_file` (dashboards).
   - Test with special characters and non-ASCII names.
   
   Let me know if you need a more detailed code sample for a specific export 
type.
   
   
   <!-- Dosu Comment Footer -->
   *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*
   
   ---
   <sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/3364c7b1-fe3d-40c0-ab1d-c9dee9ab7c07?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/3364c7b1-fe3d-40c0-ab1d-c9dee9ab7c07?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/3364c7b1-fe3d-40c0-ab1d-c9dee9ab7c07?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/3364c7b1-fe3d-40c0-ab1d-c9dee9ab7c07?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/3364c7b1-fe3d-40c0-ab1d-c9dee9ab7c07?feedback_type=hallucination)
 | [Report 
🐛](https://app.dosu.dev/response-feedback/3364c7b1-fe3d-40c0-ab1d-c9dee9ab7c07?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/3364c7b1-fe3d-40c0-ab1d-c9dee9ab7c07?feedback_type=other)</sup>
   
   [![Chat with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github)&nbsp;[![Open
 in 
Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=Here%20are%20the%20files%20and%20code%20sections%20you%20need%20to%20customize%20export%20filenames%20in%20Superset%3A%0A%0A%2A%2A1.%20Chart%20exports%20%28CSV/XLSX/ZIP%29%3A%2A%2A%0A-%20File%3A%20%60superset/charts/data/api.py%60%0A-%20Method%3A%20%60_send_chart_response%60%0A-%20By%20default%2C%20this%20method%20calls%20%60generate_download_headers%28%22csv%22%29%60%20%28or%20%60%22xlsx%22%60%2C%20%60%22zip%22%60%29%20with%20no%20filename%2C%20so%20you%20get%20a%20timestamp-based%20name.%20%20%0A-%20To%20use%20the%20chart%20name%20and%20ID%2C%20modify%20the%20call%20like%3A%0A%20%20%60%60%60python%0A%20%20from%20werkzeug.utils%20import%20secure_filename%0A%20%20chart_name%20%3D%20secure_filename%28result%5B%22query_context%22%5D.form_data.get%28%
 
22slice_name%22%2C%20%22chart%22%29%29%0A%20%20chart_id%20%3D%20result%5B%22query_context%22%5D.form_data.get%28%22slice_id%22%2C%20%22id%22%29%0A%20%20filename%20%3D%20f%22%7Bchart_name%7D_%7Bchart_id%7D%22%0A%20%20headers%20%3D%20generate_download_headers%28%22csv%22%2C%20filename%29%0A%20%20%60%60%60%0A%20%20Do%20similar%20for%20XLSX%20and%20ZIP%20exports.%20%20%0A%20%20%5BReference%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/charts/data/api.py%23L346-L411%29%0A%0A%2A%2A2.%20Dashboard%20exports%20%28YAML%20ZIP%29%3A%2A%2A%0A-%20File%3A%20%60superset/dashboards/api.py%60%0A-%20Method%3A%20%60export%60%0A-%20The%20ZIP%20filename%20is%20set%20by%3A%0A%20%20%60%60%60python%0A%20%20timestamp%20%3D%20datetime.now%28%29.strftime%28%22%25Y%25m%25dT%25H%25M%25S%22%29%0A%20%20root%20%3D%20f%22dashboard_export_%7Btimestamp%7D%22%0A%20%20filename%20%3D%20f%22%7Broot%7D.zip%22%0A%20%20%60%60%60%0A-%20Change%20this%20to%20use%20dashboard%20t
 
itles%20and%20IDs.%20For%20example%3A%0A%20%20%60%60%60python%0A%20%20from%20superset.utils.file%20import%20get_filename%0A%20%20dashboards%20%3D%20%5BDashboardDAO.find_by_id%28did%29%20for%20did%20in%20requested_ids%5D%0A%20%20name_tokens%20%3D%20%5Bget_filename%28d.dashboard_title%2C%20d.id%29%20for%20d%20in%20dashboards%20if%20d%5D%0A%20%20root%20%3D%20%22_%22.join%28name_tokens%29%0A%20%20filename%20%3D%20f%22%7Broot%7D.zip%22%0A%20%20%60%60%60%0A%20%20%5BReference%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/dashboards/api.py%23L1002-L1023%29%0A%0A%2A%2A3.%20Download%20header%20helper%3A%2A%2A%0A-%20File%3A%20%60superset/views/base.py%60%0A-%20Function%3A%20%60generate_download_headers%60%0A-%20This%20function%20just%20takes%20your%20filename%20and%20extension%20and%20sets%20the%20HTTP%20header.%20It%20does%20not%20sanitize%20or%20deduplicate%2C%20so%20do%20that%20before%20calling%20it.%0A%20%20%5BReference%5D%28https%3A//gith
 
ub.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/views/base.py%23L141-L147%29%0A%0A%2A%2AChecklist%3A%2A%2A%0A-%20Extract%20chart/dashboard%20name%20and%20ID/UUID%20in%20the%20export%20method.%0A-%20Sanitize%20the%20name%20%28use%20%60secure_filename%60%20or%20similar%29.%0A-%20Pass%20the%20sanitized%20filename%20to%20%60generate_download_headers%60%20%28charts%29%20or%20as%20%60download_name%60%20in%20%60send_file%60%20%28dashboards%29.%0A-%20Test%20with%20special%20characters%20and%20non-ASCII%20names.%0A%0ALet%20me%20know%20if%20you%20need%20a%20more%20detailed%20code%20sample%20for%20a%20specific%20export%20type.)&nbsp;[![Join
 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share
 on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/issues/37487)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to