|
9 | 9 | from ..config import find_existing_config_path |
10 | 10 |
|
11 | 11 |
|
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 |
13 | 13 |
|
14 | 14 |
|
15 | 15 | # --- Datasources --- |
|
56 | 56 | "views": t.uint64(), |
57 | 57 | }, |
58 | 58 | }) |
| 59 | +''' |
59 | 60 |
|
60 | 61 |
|
61 | | -# --- Client --- |
| 62 | +def _client_template(resources_import_path: str) -> str: |
| 63 | + return f'''import os |
62 | 64 |
|
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() |
67 | 117 | ''' |
68 | 118 |
|
69 | 119 |
|
@@ -133,7 +183,17 @@ def run_init(options: InitOptions | dict[str, Any] | None = None) -> InitResult: |
133 | 183 | folder = (src / "lib") if src.is_dir() else (cwd / "lib") |
134 | 184 |
|
135 | 185 | 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 | + |
136 | 194 | _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) |
137 | 197 |
|
138 | 198 | # 3. Add the resources file to tinybird.config.json include list |
139 | 199 | config_path = find_existing_config_path(str(cwd)) |
|
0 commit comments