Jupyter notebooks + Looker

A colleague suggested this as it’d be good to have e.g. data scientists leveraging Looker models directly for their work. I’m not 100% what would be required but I guess some Python code with some helper functions to help get at data from Looker via the API, making use of the LookML model(s).

3 Likes

We have a fairly simple example of this and (I cant find it right now) there is a very similar version I came across on public github as well.

Ah nice. I found this: https://github.com/llooker/python_connection/blob/master/API%20Call%20Template%20-%20Jupyter%20Notebooks.ipynb

So I guess my hack is done already? :laughing:

1 Like

You could make it better!

2 Likes

If you need any Python skills I am here! :slight_smile:

1 Like

Personally I usually prefer to get the SQL from Looker instead of query results, so I modified the Python LookerApi to allow me to do this:

lookerapi.py

...
    def run_query(self,query_id, fmt):
            url = '{}{}/{}/run/{}'.format(self.host,'queries',query_id, fmt)
            # print(url)
            params = {}
            print(" --- running query --- ")
            r = self.session.get(url,params=params)
            print(f'STATUS: {r.status_code}')
            if r.status_code == requests.codes.ok:
                return r.text if fmt == 'sql' else r.json()
...

myfile.py

looker = LookerApi(host=my_host, token=my_token, secret=my_secret)

def get_query_sql(model='yourmodel', explore='yourexplore', fields=[], filters={}, id_=None):

    if id_ is not None:
        r = looker.run_query(id_, 'sql')
    else:
        q = looker.create_query({'model': model, 'view': explore, 'fields': fields, 'filters': filters})
        r = looker.run_query(q['id'], 'sql')
        
    if r is None:
        print('No SQL generated!')

    return r

Hope this helps @simon_onfido!

2 Likes

Matt, how do you ensure the model reflects the observations reported on in the wider company?
My thinking is that if you go directly to the data via SQL, you’re missing the opportunity to leverage the business metrics that have been defined and used by all other stakeholders in the business.
Having to ensure your definition criteria aligns with the other business functions outside of Looker could lead to future misalignment in reporting/goals?

1 Like

Hey @Steve that’s why I use the LookerApi, so I can use all of the business metrics defined in looker. For example, if you had a metric called orders.gross_sales you can pass this in the fields parameter and all of the logic for the gross sales metric will be applied.

1 Like