Skip to content

Commit 45cfb6e

Browse files
gnzjgoampcode-com
andcommitted
Add client.py and main.py templates to init scaffolding
- Split tinybird_resources.py (definitions only) from client.py (Tinybird client) - Generate main.py with dotenv loading, ingest, and query examples - Import paths computed dynamically from --folder argument - Existing files are not overwritten unless --force is used Amp-Thread-ID: https://ampcode.com/threads/T-019d1a2d-e5a7-77fb-b97e-e855f84a34a4 Co-authored-by: Amp <amp@ampcode.com>
1 parent 1f02601 commit 45cfb6e

1 file changed

Lines changed: 66 additions & 6 deletions

File tree

  • src/tinybird_sdk/cli/commands

src/tinybird_sdk/cli/commands/init.py

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ..config import find_existing_config_path
1010

1111

12-
RESOURCES_TEMPLATE = '''from tinybird_sdk import Tinybird, define_datasource, define_endpoint, engine, node, p, t
12+
RESOURCES_TEMPLATE = '''from tinybird_sdk import define_datasource, define_endpoint, engine, node, p, t
1313
1414
1515
# --- Datasources ---
@@ -56,14 +56,64 @@
5656
"views": t.uint64(),
5757
},
5858
})
59+
'''
5960

6061

61-
# --- Client ---
62+
def _client_template(resources_import_path: str) -> str:
63+
return f'''import os
6264
63-
tinybird = Tinybird({
64-
"datasources": {"page_views": page_views},
65-
"pipes": {"top_pages": top_pages},
66-
})
65+
from tinybird_sdk import Tinybird
66+
from {resources_import_path} import page_views, top_pages
67+
68+
tinybird = Tinybird(
69+
{{
70+
"datasources": {{"page_views": page_views}},
71+
"pipes": {{"top_pages": top_pages}},
72+
"base_url": os.getenv("TINYBIRD_API_URL", "https://api.tinybird.co"),
73+
"token": os.getenv("TINYBIRD_TOKEN"),
74+
}}
75+
)
76+
'''
77+
78+
79+
def _main_template(client_import_path: str) -> str:
80+
return f'''from datetime import datetime, timezone
81+
82+
from dotenv import load_dotenv
83+
84+
85+
def main():
86+
load_dotenv(".env.local")
87+
88+
from {client_import_path} import tinybird
89+
90+
now = datetime.now(timezone.utc).isoformat(timespec="milliseconds")
91+
92+
# Ingest data using the Events API
93+
tinybird.page_views.ingest(
94+
{{
95+
"timestamp": now,
96+
"session_id": "abc123",
97+
"pathname": "/home",
98+
"referrer": "https://google.com",
99+
}}
100+
)
101+
102+
# Query the endpoint
103+
result = tinybird.top_pages.query(
104+
{{
105+
"start_date": "2026-01-01 00:00:00",
106+
"end_date": now,
107+
"limit": 5,
108+
}}
109+
)
110+
111+
for row in result["data"]:
112+
print(row["pathname"], row["views"])
113+
114+
115+
if __name__ == "__main__":
116+
main()
67117
'''
68118

69119

@@ -133,7 +183,17 @@ def run_init(options: InitOptions | dict[str, Any] | None = None) -> InitResult:
133183
folder = (src / "lib") if src.is_dir() else (cwd / "lib")
134184

135185
resources_path = folder / "tinybird_resources.py"
186+
client_path = folder / "client.py"
187+
main_path = cwd / "main.py"
188+
189+
# Compute import paths based on folder relative to cwd
190+
relative_folder = str(folder.relative_to(cwd)).replace(os.sep, ".")
191+
resources_import = f"{relative_folder}.tinybird_resources"
192+
client_import = f"{relative_folder}.client"
193+
136194
_write_file(resources_path, RESOURCES_TEMPLATE, normalized.force)
195+
_write_file(client_path, _client_template(resources_import), normalized.force)
196+
_write_file(main_path, _main_template(client_import), normalized.force)
137197

138198
# 3. Add the resources file to tinybird.config.json include list
139199
config_path = find_existing_config_path(str(cwd))

0 commit comments

Comments
 (0)