Looker's python_SDK is generated by swagger, which generates an up to date set of API classes and functions based on a JSON file that you can get from your Looker instance. However, there are a number of problems with the SDK that can be hard to troubleshoot. Here is a quick guide of the most common problems.
The Problem
- If you make a call that returns a Query() object, such as
query_api.query()ordashboard_api.dashboard_dashboard_elements()you will find that there are problems with some of the attributes:- Dynamic fields (table calculations) should be saved as JSON, but instead appear as a list of strings:
- e.g.
['[', '{', '"', 't', 'a', 'b', 'l', 'e', '_', 'c', 'a', 'l', 'c', 'u', 'l', 'a', 't', 'i', 'o', 'n', '"', ':', ..., ']'
- e.g.
- Vis_config should be saved as JSON, but instead the values are interpreted as strings:
- e.g.
[{"some_key":"True", "other_key": "['list', 'items']"}instead of[{"some_key":True, "other_key": ['list', 'items']}
- e.g.
- Dynamic fields (table calculations) should be saved as JSON, but instead appear as a list of strings:
The fix
- The simplest fix is to change how the SDK interprets these objects.
- Go to
python_sdk/looker/models/query.pyand changeself.swagger_types(lines 39-67):- Change it to
'vis_config': 'object'(line 55) and'dynamic_fields': 'object'(line 59)
- Change it to
- Note there is a chance this may cause unexpected behaviour elsewhere in the SDK
- Note changes made to the SDK will be overwritten next time you regenerate the swagger code
- You may not wish to change the SDK itself, but instead convert these objects in your own code:
Manually changing dynamic fields
- First concatenate all of the strings:
new_string = ''.join([s for s in list_of_strings]) - Then parse it into json using
json.loads(new_string) - Note that this won't work on
NoneTypeobjects, so only apply thisif dynamic_fields is not None
Manually changing vis_config
- Iterate through the vis config dict and tell python to correctly convert any dict or list that has been saved as a string
- The fastest way to do this is to tell python to literally evaluate the strings, converting them to objects
- Note
evalcan be dangerous and should only be used on code you know is not malicious. You can also usefrom ast import literal_evalwhich is safer
for k, v in vis_config_object.items():
if len(v) > 0 and (v[0] in ['[', '{'] or v in ['True', 'False'] or str.isdigit(v)):
v = eval(v)
2. Allow requests without verifying SSL certificates (e.g. making API calls to a locally-hosted version of looker for testing)
IMPORTANT! This poses a security risk. Only use this if you are making calls to a local looker server that you manage yourself and trust
- Go to
python_sdk/looker/configuration.pyand change line 93 fromself.verify_ssl = Truetoself.verify_ssl = False