Skip to main content

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)

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.8.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dbt_sl_sdk-0.8.0-py3-none-any.whl (39.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dbt_sl_sdk-0.8.0.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for dbt_sl_sdk-0.8.0.tar.gz
Algorithm Hash digest
SHA256 23d9c828ba30bce5bed7f65e67242b34dfaf11f946fcafc3a6316ae5dc44f5ab
MD5 82b6d4f1d8718e5922f00ec1fa1bc60b
BLAKE2b-256 11e9bb9bb5f30e1e51c80c079d031682bca628d78e0ee979252a9d6cd60e2d42

See more details on using hashes here.

Provenance

The following attestation bundles were made for dbt_sl_sdk-0.8.0.tar.gz:

Publisher: publish.yaml on dbt-labs/semantic-layer-sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: dbt_sl_sdk-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 39.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for dbt_sl_sdk-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 198c6c3a51e0897ca0ba167a5e62614af7ed88298e5dd232f5ace7c31ee6ce0c
MD5 6f9ddfd596930fb6c5d731c813957262
BLAKE2b-256 55db028c261bf7d3728ae1dfc6311b4b9fa8f86d7cea105624a46386314478ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for dbt_sl_sdk-0.8.0-py3-none-any.whl:

Publisher: publish.yaml on dbt-labs/semantic-layer-sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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