Python SDK for Tinybird
Project description
Tinybird Python SDK
SDK around Tinybird APIs.
If you want to manage Workspaces, Data Sources and Pipes you might be looking for the tinybird-cli.
The SDK is meant to programatically ingest NDJSON data or send any request to an API instance.
Ingest to a Tinybird DataSource
from tb.datasource import Datasource
with Datasource(datasource_name, token) as ds:
ds << {'key': 'value', 'key1': 'value1'}
You can also use the async version:
from tb.a.datasource import AsyncDatasource
async with AsyncDatasource(datasource_name, token, api_url='https://api.us-east.tinybird.co') as ds:
await ds << {'key': 'value', 'key1': 'value1'}
Notes:
- The
Datasourceobject does some in-memory buffering and uses the events API. - It only supports
ndjsondata - It automatically handles Rate Limits
Ingest using an API instance
from tb.a.api import AsyncAPI
async with AsyncAPI(token, api_url) as api:
await api.post('datasources',
params={
'name': 'datasource_name',
'mode': 'append',
'format': 'ndjson',
'url': 'https://storage.googleapis.com/davidm-wadus/events.ndjson',
}
)
- It automatically handles Rate Limits
- Works with any Tinybird API
- The
post,get,sendmethods signatures are equivalent to the requests library.
Logging from your Python module to a Tinybird Data Source
import logging
from tb.logger import TinybirdLoggingHandler
from dotenv import load_dotenv
load_dotenv()
TB_API_URL = os.getenv("TB_API_URL")
TB_WRITE_TOKEN = os.getenv("TB_WRITE_TOKEN")
logger = logging.getLogger('your-logger-name')
handler = TinybirdLoggingHandler(TB_API_URL, TB_WRITE_TOKEN, 'your-app-name')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
Each time you call the logger an event to the tb_logs DataSource in your Workspace is sent.
To configure the DataSource name initialize the TinybirdLogginHandler like this:
handler = TinybirdLoggingHandler(TB_API_URL, TB_WRITE_TOKEN, 'your-app-name', ds_name="your_tb_ds_name")
Non-blocking logging
If you want to avoid blocking the main thread you can use a queue to send the logs to a different thread.
import logging
from multiprocessing import Queue
from tb.logger import TinybirdLoggingQueueHandler
from dotenv import load_dotenv
load_dotenv()
TB_API_URL = os.getenv("TB_API_URL")
TB_WRITE_TOKEN = os.getenv("TB_WRITE_TOKEN")
logger = logging.getLogger('your-logger-name')
handler = TinybirdLoggingQueueHandler(Queue(-1), TB_API_URL, TB_WRITE_TOKEN, 'your-app-name', ds_name="your_tb_ds_name")
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
Logging from Litellm to a Tinybird Data Source
Install the ai extra:
pip install tinybird-python-sdk[ai]
Then use the following handler:
from tb.litellm.handler import TinybirdLitellmHandler
customHandler = TinybirdLitellmHandler(
api_url="https://api.us-east.aws.tinybird.co",
tinybird_token=os.getenv("TINYBIRD_TOKEN"),
datasource_name="litellm"
)
litellm.callbacks = [customHandler]
response = await acompletion(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hi 👋 - i'm openai"}],
stream=True
)
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 tinybird_python_sdk-0.2.2.tar.gz.
File metadata
- Download URL: tinybird_python_sdk-0.2.2.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e79d5ad32925216839a855baee7f0f3c6383dcf0d40ce4ae786ef1f487651d9e
|
|
| MD5 |
430db40ca28e1c6a4ff7d3bbbecf521d
|
|
| BLAKE2b-256 |
efae049b285e157006b3613672750ccd743306922114afc624c86dcbe08ec21c
|
File details
Details for the file tinybird_python_sdk-0.2.2-py3-none-any.whl.
File metadata
- Download URL: tinybird_python_sdk-0.2.2-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78057fffc691fa4623a46de858cace243d7cfd2d2d26ebe6d20f4c9c28c6dcfd
|
|
| MD5 |
410f67254a12cd02a3b2caf21158e37e
|
|
| BLAKE2b-256 |
6c39b904aafaf61200e8da1bc9aef8b3c51e7920a5470eb21d884dc8abbb0776
|