Redash

Overview

The Redash class allows you to interact with a Redash server to fetch fresh or cached query results as a Parsons Table.

Note

Authentication

The Redash API has two types of API keys: User API keys which are found on user profile pages, and Query API keys which are found on query pages. The Redash class supports fetching fresh queries with a User API Key, and cached queries with a Query API Key.

Quickstart

When instantiating the Redash class, you must provide the base URL for your Redash instance, either as the environmental variable REDASH_BASE_URL or as a keyword argument.

For fresh queries, a User API Key is also required, and can be specified with either the environmental variable REDASH_USER_API_KEY or a keyword argument.

from parsons import Redash

# Instantiate class with the base URL and User API key as keyword arguments
redash = Redash(base_url='www.example.com', user_api_key='my_user_api_key')

# Fetch fresh query results
redash.load_to_table(query_id=1001, params={'datelimit': '2020-01-01'})

To fetch cached queries, you must provide a Query API Key, either as the environmental variable REDASH_QUERY_API_KEY or as a keyword argument. You do not need a User API Key to fetch a cached query.

Note that if you specify a Query API Key when loading a table, the method will fetch cached results even if you provided a User API Key when instantiating the class.

# Pass a Query API Key to fetch cached results
redash.load_to_table(query_api_key='my_query_api_key', query_id=1001)

API

class parsons.redash.redash.Redash(base_url=None, user_api_key=None, pause_time=3, timeout=0, verify=True)[source]

Instantiate Redash Class

Parameters:
  • base_url – str The base url for your redash instance (excluding the final /)

  • user_api_key – str The user API key found in the User’s profile screen

  • int (pause_time) – Specify time between polling for refreshed queries (Defaults to 3 seconds)

  • verify – bool For https requests, should the certificate be verified (Defaults to True)

Returns:

Redash Class

get_data_source(data_source_id)[source]

Get a data source.

Parameters:

data_source_id – int or str ID of data source.

Returns:

Data source json object

update_data_source(data_source_id, name, type, dbName, host, password, port, user)[source]

Update a data source.

Parameters:
  • data_source_id – str or int ID of data source.

  • name – str Name of data source.

  • type – str Type of data source.

  • dbName – str Database name of data source.

  • host – str Host of data source.

  • password – str Password of data source.

  • port – int or str Port of data source.

  • user – str Username of data source.

get_fresh_query_results(query_id=None, params=None)[source]

Make a fresh query result and get back the CSV http response object back with the CSV string in result.content

Parameters:
  • query_id – str or int The query id of the query

  • params – dict If there are values for the redash query parameters (described https://redash.io/help/user-guide/querying/query-parameters e.g. “{{datelimit}}” in the query), then this is a dict that will pass the parameters in the POST. We add the p_ prefix for parameters, so if your query had ?p_datelimit=…. in the url, you should just set ‘datelimit’ in params here. If you set this with REDASH_QUERY_PARAMS environment variable instead of passing the values, then you must include the p_ prefixes and it should be a single url-encoded string as you would see it in the URL bar.

Returns:

Table Class

get_cached_query_results(query_id=None, query_api_key=None)[source]

Get the results from a cached query result and get back the CSV http response object back with the CSV string in result.content

Parameters:
  • query_id – str or int The query id of the query

  • query_api_key – str If you did not supply a user_api_key on the Redash object, then you can supply a query_api_key to get cached results back anonymously.

Returns:

Table Class

classmethod load_to_table(refresh=True, **kwargs)[source]

Fast classmethod makes the appropriate query type (refresh or cached) based on which arguments are supplied.

Parameters:
  • base_url – str The base url for your redash instance (excluding the final /)

  • query_id – str or int The query id of the query

  • user_api_key – str The user API key found in the User’s profile screen required for refresh queries

  • query_api_key – str If you did not supply a user_api_key on the Redash object, then you can supply a query_api_key to get cached results back anonymously.

  • int (pause_time) – Specify time between polling for refreshed queries (Defaults to 3 seconds)

  • verify – bool For https requests, should the certificate be verified (Defaults to True)

  • refresh – bool Refresh results or cached. (Defaults to True unless a query_api_key IS supplied)

  • params – dict For refresh queries, if there are parameters in the query, then this is a dict that will pass the parameters in the POST. We add the p_ prefix for parameters, so if your query had ?p_datelimit=…. in the url, you should just set ‘datelimit’ in params here.

Returns:

Table Class