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).
Hello Zuperzee! I have a problem like this:
I wanna use HTML Graphic in Webaccess Scada, but Version of Grafana in Webaccess is V6.2 so i can’t use plugin HTML Graphic. How can i update Grafana in Webaccess or hack to HTML Graphic can run in Webaccess? Please help me! Thanks!
First, consider upgrading your grafana. If you can by yourself, referrer to the docs on Upgrade Grafana | Grafana documentation . But you mentioned the “webaccess” in a way that it seems like a proprietary tool… Then if that’s the case, you should ask your vendor for support to upgrade as you need.
---- PROCEED AT YOUR OWN RISK ----
IT’S PROBABLY ISN’T GOING TO WORK, BUT… Second, if your job depends on it, I’d think about installing the Grafana 6.2 in a test environment, downloading the *.zip of HTML Graphic from github and hacking it’s “src/plugin.json” to make it “installable” on 6.2 and trying to install and see if it works, then after confirming that it worked as intended, make a backup of your current grafana in production and applying it.
---- PROCEED AT YOUR OWN RISK ----
There’s some things you’ll need to consider:
1st: a minor Plug-in for a bigger software would be developed using some underlying tools of the software. So, the HTML Graphic plug-in depends on features that are on your grafana from 8.2 and these features would be not available.
2nd: HTML Graphic is a Community Plug-in and isn’t directly supported from grafana, if you wish to sponsor or ask the author to support an older version of Grafana, you should ask here, on plugin’s author issues site: Issues · gapitio/gapit-htmlgraphics-panel · GitHub
I hope you consider upgrading your gradana, and if it’s not possible then consider not using the desired plug-in.
I’ll close this topic since it’s really old, and there’s not much more to say on the case, and the theme could generate a lot of unwanted/unnecessary discussion.