Skip to content

Commit 0c1dad4

Browse files
committed
Add basic example program about how to import a dashboard
1 parent ac86da2 commit 0c1dad4

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Unreleased
66
- Improved Python API authentication when using URL-based connectivity,
77
by respecting the `credential` keyword argument
8+
- Added basic example program about how to import a dashboard
89

910
## 0.4.0 (2024-10-16)
1011
- Fixed folder argument issue

examples/dashboard_import.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
## About
3+
4+
Upload a Grafana dashboard file in JSON format to the Grafana server.
5+
6+
## Walkthrough
7+
8+
1. Start Grafana
9+
10+
docker run --rm -it --publish=3000:3000 --env='GF_SECURITY_ADMIN_PASSWORD=admin' grafana/grafana:11.5.2
11+
12+
2. Import dashboard
13+
14+
wget https://github.com/simonprickett/mongodb-hotel-jobs/raw/refs/heads/main/grafana_dashboard.json
15+
python examples/dashboard_import.py grafana_dashboard.json
16+
17+
3. Visit Grafana
18+
19+
http://admin:admin@localhost:3000/
20+
Log in using admin:admin.
21+
"""
22+
23+
import json
24+
import sys
25+
from pathlib import Path
26+
27+
from grafana_import.grafana import Grafana
28+
29+
30+
def main():
31+
32+
# Read single positional CLI argument.
33+
dashboard_path = Path(sys.argv[1])
34+
35+
# Load dashboard JSON from filesystem.
36+
dashboard = json.loads(dashboard_path.read_text())
37+
38+
# Import dashboard JSON to Grafana.
39+
# Note: Adjust parameters to match your Grafana.
40+
# You can use many variants to authenticate with its API.
41+
gio = Grafana(url="http://localhost:3000", credential=("admin", "admin"))
42+
outcome = gio.import_dashboard(dashboard)
43+
if outcome:
44+
print("Grafana dashboard imported successfully")
45+
else:
46+
print("Grafana dashboard import failed")
47+
48+
49+
if __name__ == "__main__":
50+
main()

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ lint.extend-ignore = [
3636
]
3737

3838
lint.per-file-ignores."grafana_import/cli.py" = [
39-
# `print` found
40-
"T201",
39+
"T201", # `print` found
40+
]
41+
42+
lint.per-file-ignores."examples/*" = [
43+
"T201", # `print` found
4144
]
4245

4346
# ===================

0 commit comments

Comments
 (0)