GitHub user FrancescoCastaldi added a comment to the discussion: adding custom 
api

To add a custom download button to the chart menu that calls a custom backend 
Python endpoint, follow this architectural flow:

### 1. Create and Register the Backend Endpoint
You do not need to modify Superset's core REST APIs. You can register a custom 
Flask Blueprint directly inside `superset_config.py` using the `BLUEPRINTS` 
list or `FLASK_APP_MUTATOR`:

Create a Python file `custom_api.py` in your Python path:
```python
# custom_api.py
from flask import Blueprint, jsonify, request
from superset.extensions import security_manager

custom_blueprint = Blueprint("custom_api", __name__, 
url_prefix="/api/v1/custom")

@custom_blueprint.route("/export/<int:slice_id>", methods=["POST"])
@security_manager.has_access_api
def custom_chart_export(slice_id: int):
    # Retrieve request parameters or chart metadata
    data = request.get_json() or {}
    
    # Execute your custom Python processing
    # Example: generate custom data payload, trigger export job, etc.
    return jsonify({
        "status": "success",
        "slice_id": slice_id,
        "message": "Custom export executed successfully"
    }), 200
```

Register it in `superset_config.py`:
```python
# superset_config.py
from custom_api import custom_blueprint

BLUEPRINTS = [custom_blueprint]
```

### 2. Add the Menu Item in the Frontend
In the chart dropdown menu component 
(`superset-frontend/src/dashboard/components/SliceHeaderControls/index.tsx`):
1. Add an entry to the `MenuKeys` enum in 
`superset-frontend/src/dashboard/types.ts`:
   ```typescript
   export enum MenuKeys {
     // ... existing keys
     CustomDownload = 'custom_download',
   }
   ```
2. In `SliceHeaderControls/index.tsx`, add the `<Menu.Item>` inside the 
Download submenu:
   ```tsx
   <Menu.Item
     key={MenuKeys.CustomDownload}
     onClick={() => handleCustomDownload(props.slice.slice_id)}
   >
     {t('Custom Download')}
   </Menu.Item>
   ```
3. Use `SupersetClient` to dispatch the request to your backend endpoint:
   ```tsx
   import { SupersetClient, t } from '@superset-ui/core';

   const handleCustomDownload = async (sliceId: number) => {
     try {
       const response = await SupersetClient.post({
         endpoint: `/api/v1/custom/export/${sliceId}`,
         jsonPayload: { export_type: 'custom_format' },
       });
       props.addSuccessToast(t('Custom export completed'));
     } catch (err) {
       props.addDangerToast(t('Failed to process custom download'));
     }
   };
   ```
This clean separation keeps your custom business logic modular and upgrade-safe.


GitHub link: 
https://github.com/apache/superset/discussions/43738#discussioncomment-19164458

----
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