Refresh Grafana Dashboard triggered by Python-Code

Hello together,
I would like to achieve that the refresh of my grafana dashboard is triggered by python code. This way I can make sure that always the latest data is displayed on the dashboard without setting a very short automatic refresh interval of for example one second.

My idea was to look for a corresponding API endpoint from Grafana, which I can then address from the Python script. Unfortunately I could not find a suitable endpoint for this.

ChatGPT suggested me the following code:

import requests

def refresh_dashboard(api_key, dashboard_uid):
    headers = {
        'Authorization': f'Bearer {api_key}',
        'Content-Type': 'application/json'
    }

    url = f'http://your-grafana-instance/api/dashboards/uid/{dashboard_uid}/refresh'
    response = requests.post(url, headers=headers)

    if response.status_code == 200:
        print('Dashboard refresh successful')
    else:
        print(f'Dashboard refresh failed. Status code: {response.status_code}')

# Example usage
api_key = 'your-api-key'
dashboard_uid = 'your-dashboard-uid'

refresh_dashboard(api_key, dashboard_uid)

However, the URL used is not so included in the Swagger documentation of grafana, so I doubt the existence of the endpoint.

However, the suggested code works when using as URL an endpoint specified in the Swagger documentation.

Has anyone ever had a similar use case and a suitable solution suggestion for it?

Grafana Version 9.5.1
Docker 23.0.2 on Ubuntu 22.04.2 LTS

That’s correct. Why do you trust ChatGPT and not documentation?

Hello jangaraj,
Thank you for the reply!
“Trust” is literally the wrong word. It was just another method in finding a solution.
Please focus on the main problem, if there are ways to trigger the refresh of a grafana dashboard externally for instance via python code.

I would say your idea (to have some “refresh” API) is naive. There is Grafana Live, which is used to refresh dashboard, when there is Dashboard change notifications.

BUT: it is not very well documented, because it is mainly internal feature. So you will have to dig in Grafana source code to understand event format, subscriptions, …
It is also not a simple HTTP request, but websocket is used, so you will need more advance knowledge.

I gave you idea, where to look and what to explore.

Generally, I wouldn’t do that. Your main problem is how to get a near realtime data to the dashboard, not how to refresh dashboard. So your solution should be “metric streaming” to the dashboard, e.g. New in Grafana 8.0: Streaming real-time events and data to dashboards | Grafana Labs