Skip to main content

Description

This module aims to generate Grafana dashboards 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 Case

According 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. Also provisioning can 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-ed

    • The database can get corruption.

    • There can arise issues during upgrades.

  • manageable via GIT

  • roll-out dashboards and panels over different installations

  • roll-out changes easily over all panels and dashboards

    • change the foreground color over all panels and all dashboards

    • change a data source over all panels and all dashboards

    • add a timezone offset in all queries

  • have a unified look and feel

  • have a compact notation (json files are very verbose and lengthy)

Solution: Config in code

  • Grafana configuration in Python

    • set of Python dataclasses

    • supporting multiple panel types

    • supporting multiple data sources

    • supporting other configurations (alarming, …)

  • User functions

    • implement some communication via the Grafana API

    • download a dashboard from a Grafana installation as json

    • upload a dashboard to a Grafana installation as json

    • convert a python script to uploadable json

    • convert a json to a python script

the grafanacode tool

As 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 the attrs library. If you install it via pip (see below), these dependencies will be installed together with the tool,

Usage

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

Take 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 type grafanacode.PropGridPos. Or when you from grafanacode import *, you simply type PropGridPos. 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.

Installation

grafanacode is just a Python package, so:

pip install grafanacode

Support

This library is under development. So breaking changes are always possible.

grafanacode works with Python 3.9 and 3.10.

Development setup

Every Python development system will do, but on Windows OS, I prefer Anaconda.

To install Anaconda:

  • Download Anaconda from https://www.anaconda.com/; chose windows installer

  • Double click to install; Accept all defaults (personally I install this at another location).

  • Start Anaconda

  • Click on Anaconda Navigator

  • Setup a virtual environment; I called this grafanacode

  • Click on environments

  • Click on create at the bottom

  • Fill in the popup; chose Python 3

  • Select environment

  • Start a Command prompt
    • Click on the green play button

    • Chose open terminal

    • In the command prompt, between brackets, you can see the virtual environment you are in.

  • Install dependencies

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

Generate dashboards

Take a look at the example scripts.

Metadata

Release files for grafanacode 0.5.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grafanacode 0.5.7
File Size Uploaded
grafanacode-0.5.7.tar.gz 59.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for grafanacode 0.5.7
File Interpreter ABI Platform
grafanacode-0.5.7-py3-none-any.whl Python 3 none any Details

Total release size: 151.3 kB

Release files / grafanacode-0.5.7.tar.gz

Download URL grafanacode-0.5.7.tar.gz
Size 59.5 kB
Tags Source
SHA-256 checksum
How to use checksums
74824ff669e4460885dbf3ce3d56afbfb34e412bf103eab118254dccca366e60
BLAKE2b-256 checksum
How to use checksums
05b1cb9f739dd14d02e0a4480907e9a56e15d125ed65d731d4bd44cb74358dae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.15

Release files / grafanacode-0.5.7-py3-none-any.whl

Download URL grafanacode-0.5.7-py3-none-any.whl
Size 91.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d48304a1e268bae5007e0f4388706455bd13af57f1227621abfadefb0f305b2
BLAKE2b-256 checksum
How to use checksums
366cd4c75fba03f5576dd7491f811b2c01af3aff96e23a3403eecc26f4b2aaa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.15

Release history Release notifications | RSS feed

This release

0.5.7 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page