|
43 | 43 | "Database to query. Leave empty to use the database of the page the " |
44 | 44 | "panel is pinned to." |
45 | 45 | ), |
| 46 | + "table": ( |
| 47 | + "Table that {{table}} and {{filtered}} in the SQL refer to when the " |
| 48 | + "panel is not pinned to a table page." |
| 49 | + ), |
46 | 50 | "label": "Short sentence-case label shown above the data.", |
47 | 51 | "prefix": 'Text placed before numbers, for example "$".', |
48 | 52 | "suffix": 'Text placed after numbers, for example " ms".', |
@@ -114,6 +118,7 @@ def _thumb(body): |
114 | 118 | config={ |
115 | 119 | "label": "Total orders", |
116 | 120 | "database": "", |
| 121 | + "table": "", |
117 | 122 | "sql": "select 1284 as value, 1140 as previous", |
118 | 123 | "delta_label": "vs previous period", |
119 | 124 | "up_is_good": True, |
@@ -148,6 +153,7 @@ def _thumb(body): |
148 | 153 | height=96, |
149 | 154 | config={ |
150 | 155 | "database": "", |
| 156 | + "table": "", |
151 | 157 | "sql": ( |
152 | 158 | 'select 1284 as "Orders", 312 as "Customers", ' |
153 | 159 | '58210 as "Revenue", 4.6 as "Avg rating"' |
@@ -183,6 +189,7 @@ def _thumb(body): |
183 | 189 | config={ |
184 | 190 | "label": "Orders per month", |
185 | 191 | "database": "", |
| 192 | + "table": "", |
186 | 193 | "sql": ( |
187 | 194 | "select '2026-01' as x, 80 as value union all select '2026-02', 96 " |
188 | 195 | "union all select '2026-03', 91 union all select '2026-04', 118 " |
@@ -224,6 +231,7 @@ def _thumb(body): |
224 | 231 | config={ |
225 | 232 | "label": "Orders per month", |
226 | 233 | "database": "", |
| 234 | + "table": "", |
227 | 235 | "sql": ( |
228 | 236 | "select '2026-01' as x, 80 as value union all select '2026-02', 96 " |
229 | 237 | "union all select '2026-03', 91 union all select '2026-04', 118 " |
@@ -259,6 +267,7 @@ def _thumb(body): |
259 | 267 | config={ |
260 | 268 | "label": "Orders by status", |
261 | 269 | "database": "", |
| 270 | + "table": "", |
262 | 271 | "sql": ( |
263 | 272 | "select 'Delivered' as label, 840 as value union all " |
264 | 273 | "select 'Shipped', 260 union all select 'Processing', 120 " |
@@ -292,6 +301,7 @@ def _thumb(body): |
292 | 301 | config={ |
293 | 302 | "label": "Latest orders", |
294 | 303 | "database": "", |
| 304 | + "table": "", |
295 | 305 | "sql": ( |
296 | 306 | "select 'Order #1284' as title, 'Ada Lovelace' as subtitle, " |
297 | 307 | "'2026-05-14' as meta union all " |
@@ -323,6 +333,7 @@ def _thumb(body): |
323 | 333 | config={ |
324 | 334 | "label": "Orders this quarter", |
325 | 335 | "database": "", |
| 336 | + "table": "", |
326 | 337 | "sql": "select 1284 as value", |
327 | 338 | "goal": 2000, |
328 | 339 | "caption": "of quarterly goal", |
@@ -357,6 +368,7 @@ def _thumb(body): |
357 | 368 | config={ |
358 | 369 | "label": "Orders per day", |
359 | 370 | "database": "", |
| 371 | + "table": "", |
360 | 372 | "sql": ( |
361 | 373 | "with recursive days(day, n) as (select date('2026-05-31'), 0 " |
362 | 374 | "union all select date(day, '-1 day'), n + 1 from days where n < 180) " |
@@ -475,7 +487,10 @@ def _config_docs(template): |
475 | 487 | if template.sql_shape and "sql" in template.config: |
476 | 488 | lines.append(f"// The query returns: {template.sql_shape}") |
477 | 489 | lines.append( |
478 | | - "// Write {{table}} in the SQL to mean the table the panel is pinned to." |
| 490 | + "// In the SQL, {{table}} means the table the panel is pinned to and" |
| 491 | + ) |
| 492 | + lines.append( |
| 493 | + "// {{filtered}} means that table with the page's current filters applied." |
479 | 494 | ) |
480 | 495 | for key, doc in template.field_docs().items(): |
481 | 496 | if doc: |
@@ -591,7 +606,11 @@ async def suggest_panel_config(datasette, template_id, database=None, table=None |
591 | 606 | info = await describe_table(datasette, database, table) |
592 | 607 | except Exception: |
593 | 608 | return config |
594 | | - t = escape_sqlite(table) |
| 609 | + # {{filtered}} follows the table page's filters when the panel is allowed |
| 610 | + # to see them, and is the whole of CONFIG.table everywhere else |
| 611 | + t = "{{filtered}}" |
| 612 | + if "table" in template.config: |
| 613 | + config["table"] = table |
595 | 614 | title = table[:1].upper() + table[1:] |
596 | 615 | date_col = escape_sqlite(info["date_column"]) if info["date_column"] else None |
597 | 616 | cat_col = ( |
@@ -643,7 +662,9 @@ async def suggest_panel_config(datasette, template_id, database=None, table=None |
643 | 662 | config.update( |
644 | 663 | label=f"Latest {table}", |
645 | 664 | sql=( |
646 | | - "select " + ", ".join(columns) + f"\nfrom {t}\n" |
| 665 | + "select " + ", ".join(columns) |
| 666 | + # rowid is not visible through the {{filtered}} subquery |
| 667 | + + f"\nfrom {t if date_col else '{{table}}'}\n" |
647 | 668 | f"order by {date_col or 'rowid'} desc limit 8" |
648 | 669 | ), |
649 | 670 | ) |
|
0 commit comments