If I understand it correctly, the documentation describes the use case for someone who developes a plugin. My use case is that I just use an existing plugin. In the HTML plugin mentioned above, I can execute HTML and javascript code.
If I execute the Typescript import statement from the API reference mentioned above, I get following error:
html_ctrl.js:196 SyntaxError: Cannot use import statement outside a module
at Function (<anonymous>)
at HTMLCtrl.setHandleMetricFunction (html_ctrl.js:228)
at HTMLCtrl.eval (html_ctrl.js:193)
at EditSession.EventEmitter._signal (index.js:3865)
at EditSession.onChange (index.js:9805)
at Document.EventEmitter._signal (index.js:3865)
at Document.applyDelta (index.js:7955)
at Document.insertMergedLines (index.js:7853)
at Document.insert (index.js:7781)
at EditSession.insert (index.js:10322)
I was able to make it work but not in a fancy way, since the whole page is refreshed.
In my case I have a variable called “silo” and I want to asign the value “tc_temp” when a button is pressed.
I use the HTML Graphics plugin which makes this very easy according to the doc’s (describing an example by updating a text-variable when pressing an html button).
However, I’m not sure if and how this works with other types of grafana variables.
Thanks @neshorg but we are looking for the opposite way round: how to update the variable value from the panel code to Grafana environment. Not get the new variable value from Grafana environment to the panel
Hello @donatod, I think you might’ve missed the rest of the example from the doc’s.
The example provides 2 ways of interacting with a grafana variable by which you can decide how to alter the javascript code and use it any way you want.
If you look at the GIF and the javascript code you should see that the example is showing:
how to read a value from the grafana variable
And also how to change the environment variable directly from within the panel by clicking the html button.
Unless I’m mistaken, you’re looking for the example functions “updateGrafanaVariable” and “buttonElt.onclick” from their code:
const VARIABLE_NAME = 'testVariable';
const buttonElt = htmlNode.querySelector('button');
/*
Update a grafana variable
More information in the grafana docs
https://grafana.com/docs/grafana/latest/developers/plugins/add-support-for-variables/
*/
function updateGrafanaVariable(variableName, value) {
getLocationSrv().update({
query: {
[`var-${variableName}`]: value,
},
partial: true, // partial: true makes the update only affect the query parameters listed in query, and leaves the other query parameters unchanged.
replace: true, // replace: true tells Grafana to update the current URL state, rather than creating a new history entry.
});
}
function getGrafanaVariableValue(variableName) {
return getTemplateSrv().replace(`$${variableName}`);
}
function updateButtonText() {
buttonElt.textContent = `${VARIABLE_NAME}'s current value is: ${getGrafanaVariableValue(VARIABLE_NAME)}`;
}
buttonElt.onclick = function () {
updateGrafanaVariable(VARIABLE_NAME, getGrafanaVariableValue(VARIABLE_NAME) == 'b' ? 'a' : 'b');
};
/*
When the variable changes panelupdate will trigger.
The panelupdate is used to update the button text so the text is the same as the variable.
*/
htmlNode.addEventListener('panelupdate', () => {
updateButtonText();
});
Currently gapit-htmlgraphics-panel doesn’t work with Grafana v8.3.4 and upwards. There is a fix for this, but Grafana hasn’t published the new gapit-htmlgraphics-panel release yet. You can manually download the latest release from github with
Additional ways to download the plugin can be found on Installation | HTMLGraphics (Remember to switch from latest to specific version since latest is the latest release Grafana has released of the plugin, and not the actual latest release).