A client for dbt's Semantic Layer
Maintained. Last release 6 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity.
pip install dbt-sl-sdkdbt Semantic Layer SDK for Python
A library for easily accessing dbt's Semantic Layer via Python.
Installation
To install the SDK, you'll need to specify optional dependencies depending on whether you want to use it synchronously (backed by requests) or via asyncio (backed by aiohttp).
# Sync installation
pip install "dbt-sl-sdk[sync]"
# Async installation
pip install "dbt-sl-sdk[async]"
Usage
To run operations against the Semantic Layer APIs, just instantiate a SemanticLayerClient with your specific connection parameters (learn more):
from dbtsl import SemanticLayerClient
client = SemanticLayerClient(
environment_id=123,
auth_token="<your-semantic-layer-api-token>",
host="semantic-layer.cloud.getdbt.com",
)
# query the first metric by `metric_time`
def main():
with client.session():
metrics = client.metrics()
table = client.query(
metrics=[metrics[0].name],
group_by=["metric_time"],
)
print(table)
main()
Note that all method calls that will reach out to the APIs need to be within a client.session() context manager. By using a session, the client can connect to the APIs only once, and reuse the same connection between API calls.
asyncio
If you're using asyncio, import AsyncSemanticLayerClient from dbtsl.asyncio. The APIs of SemanticLayerClient and AsyncSemanticLayerClient are the same. The only difference is that the asyncio version has async methods which need to be awaited.
That same sync example can be converted into asyncio code like so:
import asyncio
from dbtsl.asyncio import AsyncSemanticLayerClient
client = AsyncSemanticLayerClient(
environment_id=123,
auth_token="<your-semantic-layer-api-token>",
host="semantic-layer.cloud.getdbt.com",
)
async def main():
async with client.session():
metrics = await client.metrics()
table = await client.query(
metrics=[metrics[0].name],
group_by=["metric_time"],
)
print(table)
asyncio.run(main())