About a month ago, I released an open-source Python library called lkml
that parses LookML into JSON or Python dictionaries. lkml
is a fast, robustly tested parser that can be imported in Python or run directly from the command line.
I’m excited to announce that I’ve added additional functionality to lkml
in v0.2.0 that does the reverse: generate LookML from Python objects. Now, lkml
is a closed loop for working with LookML in Python: you can load LookML to a dictionary, modify it with code, and dump it back to LookML without any manual editing.
A couple ideas for how this could be used:
- Keeping Looker in sync with external data dictionaries by automatically syncing changes to the
description
field in Looker - Programmatically generating dimensions and measures when new columns are added to a database table
Installation
You can install lkml
with pip.
pip install lkml
Approach
Rather than building a highly specific object-oriented architecture where LookML fields are generated from Python classes (e.g. View
, Explore
, Dimension
, etc.), I took a simpler approach. lkml
follows a few concise rules to convert Python objects like dictionaries and lists to various chunks of LookML.
For example, a dictionary like this.
{
"dimension": {
"type": "number",
"label": "Unit Price",
"sql": "${TABLE}.price",
"name": "price"
}
}
Is converted to a LookML block like this.
The benefit of this approach is that you won’t have to wait for me to update or fix the library to support new features in LookML. lkml
supports all LookML at time of writing and has been tested on over 160K lines of public LookML from GitHub.
The tradeoff of this approach is that it is possible to generate invalid LookML if you structure the input data incorrectly, so I’ve written thorough documentation about how lkml
represents LookML in Python.
Check out the project on GitHub for details and documentation on how to parse and generate LookML in Python with lkml
. Contributions and issues are welcome! If you build anything interesting, please send me a link!