Thank you for your response. This is my intention:
I have a python web application and also an iframe to embed redash dashboard.
What I need to do is to run some python code in charge of sending a POST request to redash API so that the Query Results are updated depending on attached parameters in the POST request and also update the dashboard so that the user can see an updated dashboard embedded in an iframe.
I tried to apply the code published by redash (redash_toolbelt) with some modifications but I was not capable of updating Query Result and Dashboard with any of existing (work in progress) code examples.
Cool. It sounds like you’re basically just using redash_toolbelt to refresh parameterized queries on a schedule, since the built-in scheduler can’t do this. If that’s the case, can you share the code you used (anonymized of course) that didn’t work?
This is the code extracted from redash_toolbelt with some new lines of code added by me.
def refresh_dashboard(baseurl, apikey, slug):
client = Redash(baseurl, apikey)
todays_dates = get_frontend_vals()
queries_dict = get_queries_on_dashboard(client, slug)
# loop through each query and its JSON data
for idx, qry in queries_dict.items():
# Trying to replace existing query with new value for "user"
qry['query'] = qry['query'].replace('SomeUserName', 'lemon')
#print(qry['query'])
params = {
p.get("name"): fill_dynamic_val(todays_dates, p)
for p in qry["options"].get("parameters", [])
}
# Trying to set custom datetime values
params['mydate']['start'] = '2020-11-08 11:44:00'
params['mydate']['end'] = '2020-11-08 12:00:00'
print("params:", params)
# Pass max_age to ensure a new result is provided.
body = {"parameters": params, "max_age": 0}
r = client._post(f"api/queries/{idx}/results", json=body)
print(f"Query: {idx} -- Code {r.status_code}")
print(r.text)
Are you not using a parameter for the user name? It looks like this code directly substitutes SomeUserName in the query text. This is of course fine. Just making sure I understand.
That is correct. Only datetime is making use of query parameters so that user can set them at will when viewing the dashboard. The username does not make use of query parameters because I do not want the user to set that field manually in the dashboard. That is why I am trying to replace “SomeUsername” with the required value
First up, the response you receive back doesn’t indicate an error. It looks like the queries are executing normally. So that’s not a problem.
But I still think there are two missing pieces here:
When you substitute SomeUserName into the query, you aren’t saving the query again. So you are kicking off a new execution. But there’s no way for the dashboard to look up the value. Redash looks up query results using a hash of the value. So you need to first save the query with the new username hard-coded, then execute it.
Then when the dashboard fetches results for some query ID, there will be a cached result.
If you are doing this on a per-user basis then you’ll need a tool other than Redash, since Redash won’t work for embedded analytics.
Since there is an API call like ‘api/dashboards/{}’, which expects “properties” to be passed and the id of the dashboard, I understand it is possible to update the dashboard using the input fields defined in query result, right?
It is not clear to me what is the format of “properties”