How to expose the MAPBOX_API_KEY to a frontend plugin? #39780
-
|
Hello, This is the Viz class I used: class TrafficFlowViz(BaseViz):
viz_type = "custom-map"
def get_data(self, df):
return {
"data": df.to_dict(orient="records"),
"mapboxApiKey": config["MAPBOX_API_KEY"],
}The key matches the one set in MainPreset.js as well, but I do not know what to do futher, my transformProps.ts in the plugin does not register it at all. import { ChartProps } from "@superset-ui/core";
export default function transformProps(chartProps: ChartProps) {
const { width, height, queriesData } = chartProps;
const mapboxApiKey = queriesData?.[0]?.mapboxApiKey;
console.log("QUERIES DATA:", chartProps);
return {
data: queriesData[0]?.data ?? [],
width,
height,
mapboxApiKey,
};
}Is there a newer / better / working way on how to handle this with superset v6.0.0. Surely hard coding it in the frontend of the plugin is not the desired solution. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
|
Hi @jf-codestryke! I'm Dosu and I’m helping the superset team. @jf-codestryke The issue is likely that your custom Viz class isn't actually being used in the data flow. In Superset v6, if your plugin is using the newer chart data API ( Here's how the built-in mapbox plugins do it: the key gets injected by the backend Viz class into the data payload, and then the frontend accesses it at That said, the real problem is likely that your Viz class isn't being called. There are two alternative approaches that may work better for you: Option 1: Use In your FRONTEND_CONF_KEYS = [
"MAPBOX_API_KEY",
]Then in your plugin's import { getBootstrapData } from 'src/utils/getBootstrapData';
export default function transformProps(chartProps: ChartProps) {
const { width, height, queriesData } = chartProps;
const bootstrapData = getBootstrapData();
const mapboxApiKey = bootstrapData.common?.conf?.MAPBOX_API_KEY;
return {
data: queriesData[0]?.data ?? [],
width,
height,
mapboxApiKey,
};
}Option 2: Use # superset_config.py
def custom_bootstrap_overrides(bootstrap_data):
return {
"mapbox_api_key": app.config["MAPBOX_API_KEY"],
}
COMMON_BOOTSTRAP_OVERRIDES_FUNC = custom_bootstrap_overridesThen access The bootstrap data approach avoids needing a custom Viz class entirely and is the most practical path for a custom plugin where you control To reply, just mention @dosu. Docs are dead. Just use Dosu. |
Beta Was this translation helpful? Give feedback.
I apologize for the misleading suggestion earlier — I dug deeper and found that
FRONTEND_CONF_KEYSis actually a hardcoded tuple insuperset/views/base.py[1], not a configurable setting insuperset_config.py. Setting it in your config will have no effect since the code reads the tuple directly fromviews/base.py.Here's what should actually work — use
COMMON_BOOTSTRAP_OVERRIDES_FUNCinstead. This is a configurable hook insuperset_config.pythat lets you inject custom values into the bootstrap data [2] [3].In your
docker/pythonpath_dev/superset_config.py:Make sure
MAPBOX_API_KEYis also set (eithe…