Skip to main content

A client for dbt's Semantic Layer

Project description

dbt Semantic Layer SDK for Python

🧪 This library is still experimental and it's not feature complete yet.

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)

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbt_sl_sdk-0.3.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

dbt_sl_sdk-0.3.0-py3-none-any.whl (35.5 kB view details)

Uploaded Python 3

File details

Details for the file dbt_sl_sdk-0.3.0.tar.gz.

File metadata

  • Download URL: dbt_sl_sdk-0.3.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for dbt_sl_sdk-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d7b3e3fb609f04135f0e28a355dd425d936b41f04468d2c9976967897c15d937
MD5 ee1beb38a3809acf7b0fc16b221da096
BLAKE2b-256 8c194a30b4d429efac67f069073648a561c9980b826d1dcdadb0eda355c407af

See more details on using hashes here.

File details

Details for the file dbt_sl_sdk-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: dbt_sl_sdk-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 35.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for dbt_sl_sdk-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d86cd5b775786ca6a3a5557e06504a849d32886477509e7de8026a9f6297a399
MD5 e6429554dc0402403fced2a8576ea1f9
BLAKE2b-256 b68a5181fcd70b20b8b39d0c9c837da0333a510a4244ccd013d9b332d41aefde

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page