observablehq / framework

A static site generator for data apps, dashboards, reports, and more. Observable Framework combines JavaScript on the front-end for interactive graphics with any language on the back-end for data analysis.
https://observablehq.com/framework/
ISC License
2.57k stars 123 forks source link

referencing parameter values in sql front matter #1653

Closed c-koster closed 2 months ago

c-koster commented 2 months ago

Hi all - I've been following #245 and #1523 and have just implemented some pages with parameterized routes. I am however having a persistent issue with page loading speed.

Using the example of product sales from the docs, suppose I have a [product].md page which defines a parameterized route to look at sales measures for a particular product id. The product file is loaded in the front matter, and the product parameter is passed into the sql statement:

sql:
    product_sales: ./data/product_sales.csv
SELECT
DATE(sale_date) AS sale_day,
SUM(quantity) AS total_quantity_sold,
SUM(total_amount) AS total_sales_amount
FROM product_sales
WHERE product_id = ${observable.params.product}
GROUP BY DATE(sale_date)
ORDER BY sale_day

However, if the number of products in product_sales.csv is large, it seems like it would be better to split up each product into its own file, and load only that file as a table. I know this is now possible for passing parameters to FileAttachment but what if there were something similar for the sql front matter? e.g.:

sql:
    product_sales: ./data/product_sales_{observable.params.product}.csv

Otherwise, how do you suggest working with SQL in parameterized pages? I'm pretty sure I can do everything I need with data loaders, but it is very nice to work with the sql blocks inside the page, particularly as I've got multiple charts that rely on slightly different slices of the same data.