A client for dbt's Semantic Layer
Project description
dbt 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())
Integrating with dataframe libraries
By design, the SDK returns all query data as pyarrow tables. If you wish to use the data with libraries like pandas or polars, you need to manually download them and convert the data into their format.
If you're using pandas:
# ... initialize client
arrow_table = client.query(...)
pandas_df = arrow_table.to_pandas()
If you're using polars:
import polars as pl
# ... initialize client
arrow_table = client.query(...)
polars_df = pl.from_arrow(arrow_table)
Lazy loading
By default, the SDK will eagerly request for lists of nested objects. For example, in the list of Metric returned by client.metrics(), each metric will contain the list of its dimensions, entities and measures. This is convenient in most cases, but can make your returned data really large in case your project is really large, which can slow things down.
It is possible to set the client to lazy=True, which will make it skip populating nested object lists unless you explicitly load ask for it on a per-model basis. Check our lazy loading example to learn more.
More examples
Check out our usage examples to learn more.
Disabling telemetry
By default, dbt the SDK sends some platform-related information to dbt Labs. If you'd like to opt out, do
from dbtsl.env import PLATFORM
PLATFORM.anonymous = True
# ... initialize client
Contributing
If you're interested in contributing to this project, check out our contribution guidelines.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dbt_sl_sdk-0.11.0.tar.gz.
File metadata
- Download URL: dbt_sl_sdk-0.11.0.tar.gz
- Upload date:
- Size: 28.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b25716276d03aee1228d1a28e2932f63ffd4356878072c4106f6e1664ad680e8
|
|
| MD5 |
f81bf41b02116cca5831fc5b98f34d5c
|
|
| BLAKE2b-256 |
0d3f0ed1e5a2c1f4b880ba536179397107ffbbdd9f11080a37fb4302258e8738
|
Provenance
The following attestation bundles were made for dbt_sl_sdk-0.11.0.tar.gz:
Publisher:
publish.yaml on dbt-labs/semantic-layer-sdk-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dbt_sl_sdk-0.11.0.tar.gz -
Subject digest:
b25716276d03aee1228d1a28e2932f63ffd4356878072c4106f6e1664ad680e8 - Sigstore transparency entry: 200653783
- Sigstore integration time:
-
Permalink:
dbt-labs/semantic-layer-sdk-python@6c61837d8409f392bda0fc57e4471484032f19ec -
Branch / Tag:
refs/tags/0.11.0 - Owner: https://github.com/dbt-labs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@6c61837d8409f392bda0fc57e4471484032f19ec -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file dbt_sl_sdk-0.11.0-py3-none-any.whl.
File metadata
- Download URL: dbt_sl_sdk-0.11.0-py3-none-any.whl
- Upload date:
- Size: 46.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bba027b38def0f64e390b0d0e0894ed41b5b0921b04edef2f5e22f555b415400
|
|
| MD5 |
a1555d06b996865dc0eba746ecd3f375
|
|
| BLAKE2b-256 |
1d7a10ca303a8004089bcb69914b3a5ace943da25611417a1613fad47a40eed2
|
Provenance
The following attestation bundles were made for dbt_sl_sdk-0.11.0-py3-none-any.whl:
Publisher:
publish.yaml on dbt-labs/semantic-layer-sdk-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dbt_sl_sdk-0.11.0-py3-none-any.whl -
Subject digest:
bba027b38def0f64e390b0d0e0894ed41b5b0921b04edef2f5e22f555b415400 - Sigstore transparency entry: 200653785
- Sigstore integration time:
-
Permalink:
dbt-labs/semantic-layer-sdk-python@6c61837d8409f392bda0fc57e4471484032f19ec -
Branch / Tag:
refs/tags/0.11.0 - Owner: https://github.com/dbt-labs
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@6c61837d8409f392bda0fc57e4471484032f19ec -
Trigger Event:
workflow_dispatch
-
Statement type: