GitHub user dosubot[bot] added a comment to the discussion: How to create a 
custom map using Viz plugin

<!-- Answer -->
To add backend support for your custom visualization plugin in Superset, you 
need to modify the core repository—third-party plugins can't extend backend 
functionality yet (this is planned for the future with SIP-151) 
[[source](https://github.com/apache/superset/issues/31932)].

Here's how you can do it:

1. **Subclass BaseViz**: In `superset/viz.py`, create a new Python class for 
your visualization by subclassing `BaseViz`. Assign it a unique `viz_type` 
string.

2. **Implement get_data**: In your new class, override the `get_data` method to 
include any custom payload you need (like the Mapbox API key). For example:

   ```python
   from superset import config
   from superset.viz import BaseViz

   class CustomMapViz(BaseViz):
       viz_type = "custom_map"

       def get_data(self, df):
           return {
               "data": df.to_dict(orient="records"),
               "mapboxApiKey": config["MAPBOX_API_KEY"],
           }
   ```

3. **Register your viz_type**: Make sure your new backend class is imported and 
registered so Superset recognizes it. Typically, this means adding it to the 
viz registry in `superset/viz.py`.

4. **Connect frontend and backend**: In your frontend plugin (created with 
Yeoman), set the `key` to match your backend `viz_type` (e.g., `"custom_map"`). 
Register it in `superset-frontend/src/visualizations/presets/MainPreset.js`.

This way, when your chart is rendered, the backend will supply the Mapbox API 
key in the payload, and your frontend can use it to initialize the map 
[[source](https://github.com/apache/superset/discussions/34817)].

Until the new plugin architecture lands, these backend changes require 
modifying the Superset core repo. The upcoming extensions system will allow 
backend extensibility from plugins without touching the core 
[[source](https://github.com/apache/superset/issues/31932)].

Let me know if you want a more detailed code walkthrough or have questions 
about deployment!


<!-- 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/ec7beba8-0666-42ea-a580-3f64593b54d8?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/ec7beba8-0666-42ea-a580-3f64593b54d8?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/ec7beba8-0666-42ea-a580-3f64593b54d8?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/ec7beba8-0666-42ea-a580-3f64593b54d8?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/ec7beba8-0666-42ea-a580-3f64593b54d8?feedback_type=hallucination)
 | [Report 
🐛](https://app.dosu.dev/response-feedback/ec7beba8-0666-42ea-a580-3f64593b54d8?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/ec7beba8-0666-42ea-a580-3f64593b54d8?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=To%20add%20backend%20support%20for%20your%20custom%20visualization%20plugin%20in%20Superset%2C%20you%20need%20to%20modify%20the%20core%20repository%E2%80%94third-party%20plugins%20can%27t%20extend%20backend%20functionality%20yet%20%28this%20is%20planned%20for%20the%20future%20with%20SIP-151%29%20%5B%5Bsource%5D%28https%3A//github.com/apache/superset/issues/31932%29%5D.%0A%0AHere%27s%20how%20you%20can%20do%20it%3A%0A%0A1.%20%2A%2ASubclass%20BaseViz%2A%2A%3A%20In%20%60superset/viz.py%60%2C%20create%20a%20new%20Python%20class%20for%20your%20visualization%20by%20subclassing%20%60BaseViz%60.%20Assign%20it%20a%20unique%20%60viz_type%60%20string.%0A%0A2.%20%2A%2AImplement%20get_data%2A%2A%3A%20In%20your%20new%20class%2C%20override%20the%20%60get_data%60%20me
 
thod%20to%20include%20any%20custom%20payload%20you%20need%20%28like%20the%20Mapbox%20API%20key%29.%20For%20example%3A%0A%0A%20%20%20%60%60%60python%0A%20%20%20from%20superset%20import%20config%0A%20%20%20from%20superset.viz%20import%20BaseViz%0A%0A%20%20%20class%20CustomMapViz%28BaseViz%29%3A%0A%20%20%20%20%20%20%20viz_type%20%3D%20%22custom_map%22%0A%0A%20%20%20%20%20%20%20def%20get_data%28self%2C%20df%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22data%22%3A%20df.to_dict%28orient%3D%22records%22%29%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22mapboxApiKey%22%3A%20config%5B%22MAPBOX_API_KEY%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%60%60%60%0A%0A3.%20%2A%2ARegister%20your%20viz_type%2A%2A%3A%20Make%20sure%20your%20new%20backend%20class%20is%20imported%20and%20registered%20so%20Superset%20recognizes%20it.%20Typically%2C%20this%20means%20adding%20it%20to%20the%20viz%20registry%20in%20%60superset/viz.py
 
%60.%0A%0A4.%20%2A%2AConnect%20frontend%20and%20backend%2A%2A%3A%20In%20your%20frontend%20plugin%20%28created%20with%20Yeoman%29%2C%20set%20the%20%60key%60%20to%20match%20your%20backend%20%60viz_type%60%20%28e.g.%2C%20%60%22custom_map%22%60%29.%20Register%20it%20in%20%60superset-frontend/src/visualizations/presets/MainPreset.js%60.%0A%0AThis%20way%2C%20when%20your%20chart%20is%20rendered%2C%20the%20backend%20will%20supply%20the%20Mapbox%20API%20key%20in%20the%20payload%2C%20and%20your%20frontend%20can%20use%20it%20to%20initialize%20the%20map%20%5B%5Bsource%5D%28https%3A//github.com/apache/superset/discussions/34817%29%5D.%0A%0AUntil%20the%20new%20plugin%20architecture%20lands%2C%20these%20backend%20changes%20require%20modifying%20the%20Superset%20core%20repo.%20The%20upcoming%20extensions%20system%20will%20allow%20backend%20extensibility%20from%20plugins%20without%20touching%20the%20core%20%5B%5Bsource%5D%28https%3A//github.com/apache/superset/issues/31932%29%5D.%0A%0ALet%20me%20kno
 
w%20if%20you%20want%20a%20more%20detailed%20code%20walkthrough%20or%20have%20questions%20about%20deployment%21)&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/discussions/36591)

GitHub link: 
https://github.com/apache/superset/discussions/36591#discussioncomment-15241512

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