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'}
from tb.datasource import Datasource
with Datasource(datasource_name, token, api_url='https://api.us-east.tinybird.co') as ds:
ds << {'key': 'value', 'key1': 'value1'}
Alternatively you can do:
from tb.datasource import Datasource
ds = Datasource(datasource_name, token)
for json_obj in list_of_json:
ds << json_obj
# just remember to flush the remaining json_obj at the end
ds.flush()
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.api import API
api = API(token, api_url)
api.post('/v0/datasources', params={
'name': 'datasource_name',
'mode': 'append',
'format': 'ndjson',
'url': 'https://storage.googleapis.com/davidm-wadus/events.ndjson',
})
- It automatically handle 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_ADMIN_TOKEN = os.getenv("TB_ADMIN_TOKEN")
logger = logging.getLogger('your-logger-name')
handler = TinybirdLoggingHandler(TB_API_URL, TB_ADMIN_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_ADMIN_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_ADMIN_TOKEN = os.getenv("TB_ADMIN_TOKEN")
logger = logging.getLogger('your-logger-name')
handler = TinybirdLoggingQueueHandler(Queue(-1), TB_API_URL, TB_ADMIN_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)
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.1.6.tar.gz.
File metadata
- Download URL: tinybird_python_sdk-0.1.6.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71d4d27d08ac82c6ea27036be82200610d764e0d43f0ffa35c0efa36597c01b5
|
|
| MD5 |
4aa41cd296b8b1cf43c92cdf071d0ed7
|
|
| BLAKE2b-256 |
c45f8d81e2382d330e8b90761077125fa89fa40be967314b05103362256c779b
|
File details
Details for the file tinybird_python_sdk-0.1.6-py3-none-any.whl.
File metadata
- Download URL: tinybird_python_sdk-0.1.6-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac6ce2f2b9e62263dbb4e29cef41839528ce867ba235d04a1f660729d9708a05
|
|
| MD5 |
fdac00a0a04cd77ecf2c6125c8dd6192
|
|
| BLAKE2b-256 |
ee94ea70536529b0e4489064e56146cafeda049ad2b1f9ec891edaca0be8b7dc
|