weaveworks / grafanalib

Python library for building Grafana dashboards
Apache License 2.0
1.88k stars 315 forks source link

Generate dashboard json from inside Python #538

Closed vsoch closed 2 years ago

vsoch commented 2 years ago

Hi! I'm new to this project and Grafana, and I'm wondering if it's possible to reproduce:

generate-dashboard -o frontend.json example.dashboard.py

But from within Python. My use case is wanting to start with my data file, and have my own client to output the json file. E.g.,:

$ python myscript.py transform my-custom-input.cali
# generates the final dashboard.json

Instead of needing to do:

$ python myscript.py transform my-custom-input.cali -> write temporary python file --> generate-dashboard

E.g., I can hack sys.argv and write my generated python file to a temporary location first (this bit)

...
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(generate_dashboard_script())

But it would be really nice to just generate that code, and then the json from the same script. Is this possible (and maybe I'm missing the docs for it somewhere?) thank you!

JamesGibo commented 2 years ago

Yeah, there is an example of doing just that here:

https://github.com/weaveworks/grafanalib/blob/main/grafanalib/tests/examples/example.upload-dashboard.py

vsoch commented 2 years ago

Perfect, thank you!