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).


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.

1 Like

Ah nice. I found this:

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

1 Like

You could make it better!


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:

    def run_query(self,query_id, fmt):
            url = '{}{}/{}/run/{}'.format(,'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 ==
                return r.text if fmt == 'sql' else r.json()

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')
        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!


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