Download query results via Python (e.g. redash-toolbelt) or R


I’m very new to redash and Python, having done most work in R, so apologies if I’m asking a silly question. The company we’re collaborating with is hosting its data on their own redash server, into which I can login, view results of queries etc., and download .csv files etc. I don’t need to upload any data, or change the structure of any of the queries that are already there, though if essential I can do this as I can login with sufficient rights. However, I want to be able to download query results data directly into R or Python with a script, so that I don’t need to login via Chrome etc. I can use the following very simple Python script for example to get the SQL queries:

import click
import requests
from redash_toolbelt.client import Redash
def doquery(redash_url, api_key):
redash = Redash(redash_url, api_key)
queries = redash.queries()

This can be put into a Python script and run, and it correctly returns (amongst other things) the three SQL queries on the redash server. It can also be called successfully from inside R using the reticulate package. Again, the 3 SQL queries are shown correctly in R.

What I’ve been battling with over the last few days, without success, is how to download the results of those queries. I’ve tried to adapt the code in in redash-toolbelt (sorry - as this is my first post I’m blocked from the number of links I can add).

but without success. I will admit that I am a little confused by some settings in this script; presumably the query_id is the unique query number that is viewable in the redash API dropdown menu, in my case 258, 259 and 260.

The redash_toolbelt script also has on line 44 the statement:

params = {‘some_parameter’: 1}

but I have no idea what to put in this. Some online comments also mention a SOURCE_ID number which is not the same as a QUERY_ID number. I have found that this is 12 for all three of our SQL queries, but it doesn’t seem to have helped me progress.

Other options I’ve seen online are quite different, e.g. using curl to GET the data. So I’ve attempted calls such as:

curl --request GET --url “url_here” --header “Authorization: Key xxxXXX”

or slight variations on above, typically receiving error messages

Again, many apologies for asking such basic questions, but I’ve struggled for several days without success trying to resolve this. My question is not as clear as it should be as it keeps saying I’ve more than 2 links in it.
Thank you.