Package: grafanacode
DescriptionThis module aims to generateGrafanadashboards from Python scripts.
The module is tested with Grafana version 9. Older versions of Grafana might have another API and another JSON structure and are thus unsupported.Use CaseAccording to their website, Grafana is a web-based application to query, visualize, alert on, and understand your data no matter where it’s stored.
With Grafana you can create, explore, and share all of your data through beautiful, flexible dashboards.The configuration is done through a GUI, which is very user friendly.
Developing a dashboard can be done via trial and error or on progressive insight.
The configurations are stored in a Sqlite3 database.
There is a basic version control and the dashboards and panels are portable via json.
The json of a dashboard or a panel can be inspected via the GUI.
Grafana has a web API that can be used to interact with the installation.
Alsoprovisioningcan be used.
Then the generated config files will have to be stored at the appropriate location.But, sometimes we want more…a robust configuration that can be backup-edThe database can get corruption.There can arise issues during upgrades.manageable via GITroll-out dashboards and panels over different installationsroll-out changes easily over all panels and dashboardschange the foreground color over all panels and all dashboardschange a data source over all panels and all dashboardsadd a timezone offset in all querieshave a unified look and feelhave a compact notation (json files are very verbose and lengthy)Solution: Config in codeGrafana configuration in Pythonset of Python dataclassessupporting multiple panel typessupporting multiple data sourcessupporting other configurations (alarming, …)User functionsimplement some communication via the Grafana APIdownload a dashboard from a Grafana installation as jsonupload a dashboard to a Grafana installation as jsonconvert a python script to uploadable jsonconvert a json to a python scriptthe grafanacode toolAs already said, the configuration will be done in Python.
Although this is all relative simple code, some knowledge of python will be useful.
We will use Python 3 with some additional libraries. Under the hood we make extensive use of theattrs library.
If you install it viapip(see below), these dependencies will be installed together with the tool,UsageThis module contains a number of functionalities:Save one or more Grafana dashboards.Export one or more Grafana dashboards to Python code (*).Generate one or more Grafana dashboards.Upload one or more Grafana dashboards to a running system.(*) The export is not complete. Some additional work needs to be done before the code can run.
But the script contains already most of the codeTake a look at the included examples for more info.IMPORTANT NOTE:In order to reduce typing there is only one namespace: grafanacode.
So when in the documentation there is:grafanacode.plugins.panel_base.PropGridPos, you have to typegrafanacode.PropGridPos.
Or when youfrom grafanacode import *, you simply typePropGridPos.
I know this is strongly disadvised. Nevertheless, in this case, importing the grafanacode objects in your namespace implies little risk. On the other side, t makes the code much more compact and readable, which is one of the main aims of this module.Installationgrafanacode is just a Python package, so:pip install grafanacodeSupportThis library is under development.
So breaking changes are always possible.grafanacode works with Python 3.9 and 3.10.Development setupEvery Python development system will do, but on Windows OS, I prefer Anaconda.To install Anaconda:Download Anaconda fromhttps://www.anaconda.com/; chose windows installerDouble click to install; Accept all defaults (personally I install this at another location).Start AnacondaClick on Anaconda NavigatorSetup a virtual environment; I called thisgrafanacodeClick onenvironmentsClick oncreateat the bottomFill in the popup; chosePython 3Select environmentStart a Command promptClick on the green play buttonChoseopen terminalIn the command prompt, between brackets, you can see the virtual environment you are in.Install dependenciespip install attrs
pip install requests
pip install sphinx-rtd-theme
pip install sphinx-toolbox
pip install pprinter
pip install pylint
pip install docutils==0.16
pip install hypothesisGenerate dashboardsTake a look at the example scripts.