weaveworks / grafanalib

Python library for building Grafana dashboards
Apache License 2.0
1.86k stars 309 forks source link

Cannot find generate-dashboard command on windows !! Need help #564

Closed gallavarapu closed 1 year ago

gallavarapu commented 1 year ago

Hi ,

I am relatively new to grafana and this UI related stuff.

Could you please help me what is the command generate-dashboard

I installed grafanalib library using python pip command, but unable to generate the json file using below command

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

i am using windows machine. Please help

JordonBibian commented 1 year ago

You can try using my code if you'd like:

import json
import requests
import os

# Variables for the input .py template and the output .json dashboard.

py_in = 'generated.dashboard.py'
dashboard_out = 'test.json'

# Dashboard file to be uploaded.

dashboard_file = "test.json"

# Variables for dashboard options, these are all example variables.

dashboard_id = 123 # This must change for each dashboard.
dashboard_uid = "9z-cCf24k" # This must change for each dashboard.
dashboard_title = "Project Dashboard" # A title is required to upload the dashboard. The title in dashboard.py will be overwritten.

# The folder variables below are hardcoded to be a project folder in my instance. 

dashboard_folder_id = 10 # Grafana does not use this ID, but it is required to have one. Can be any value.
dashboard_folder_uid = "nX5xZBh4z"

# Generate the dashboard from the input dashboard.py file.

os.system("generate-dashboard -o {} {}".format(dashboard_out, py_in))

# Set API endpoint and headers.

url = "http://admin:admin@localhost:3000/api/dashboards/db"

headers = {
    "Accept": "application/json",
    "Content-Type": "application/json"
    # "Authorization": "example" Will have to be added.
}

# Read the .json file and load it into a Python dictionary.

with open("{}".format(dashboard_file)) as dashboard_json:
    dashboard_data = json.load(dashboard_json)

# Add dashboard options to the data

dashboard_data["dashboard"] = {
    # "id": dashboard_id,
    # "uid": dashboard_uid,
    "title": "{}".format(dashboard_title),
    "tags": [],
    "timezone": "browser",
    # "schemaVersion": dashboard_schema_version, # Defined in dashboard.py 
    "version": 2,
    "panels": dashboard_data["panels"]
}

dashboard_data["overwrite"] = True
dashboard_data["folderID"] = dashboard_folder_id
dashboard_data["folderUid"] = dashboard_folder_uid

test_json = json.dumps(dashboard_data, indent = 4)

# Print the dashboard information, including the json code that makes up the dashboard.

# for i in dashboard_data:
#     print(i)
#     print(dashboard_data[i])

# Make POST request, store response, and print result.

response = requests.post(url, headers=headers, json=dashboard_data)

if response.status_code == 200:
    print("Dashboard successfully uploaded!")
else:
    print("Error uploading dashboard: {}".format(response.text))

Please be sure to read the comments to know what to change. You'll likely have to change the folder uid/id, you can just use 0 to upload to the 'general' folder. Please let me know if it's still not working.

Have you tried 'pip3 install grafanalib'? or 'pip install grafanalib'.

JDuskey commented 1 year ago

@gallavarapu Did the above code work for you? If not, what issues are you still facing?

If you did get this working, can this issue be closed?

JamesGibo commented 1 year ago

As there has been no update on this issue I am going to assume the issue has been fixed and close the issue, please reopen if this is not the case.