Save image from Grafana using python

I am trying to save image from grafana dashboard using python.

I have created API key and copied link from direct link rendered image but some how it doesn’t working. I have googled to try to solve the issue but I am kind of stuck.

I have tried few different ways.

First

import urllib.request
url = 'http://<IP>:<PORT>/render/.....'
urllib.request.urlretrieve(url, 'test.png')

this worked but the saved image was invalid(nothing showed when I open the image).

Next I have created the API key from the Grafana and used it.

import imgkit
imgkit.from_url(curl "http://<IP>:<port>/render....'-H 'Authorization:Bearer <API Key>' >'test.png')

and above code gave me syntaxError:invalid syntax

I am not sure how to use mixture of API key and the url. so can some one please give me ideas on how to export the image?
If there is alternative way to save image from panel, please let me know

thanks

@eiran

I have read your post but failed to export the image from Grafana.
Do you have ideas what I have missed out?

Thanks for your help in advance.

Hi gays, you should put the api key (access token) in the header.

Here is tested example code.

import urllib
import urllib.request
headers = [
     ('Authorization', 'Bearer eyJrIjoiSUJaamF1b2dURHVBUW1vY3FlbTA5QUFNOXZxcGhOSWwiLCJuIjoidmlld2VyIiwiaWQiOjF9')
]
opener = urllib.request.build_opener()
opener.addheaders = headers
urllib.request.install_opener(opener)
urllib.request.urlretrieve("http://grafana.staged-by-discourse.com/render/d-solo/8YpnSwPnk/walk?orgId=1&from=1647852941196&to=1647874541196&panelId=2&width=1000&height=500&tz=Asia%2FShanghai", "test.png")

This topic was automatically closed after 365 days. New replies are no longer allowed.