Is there a way to dynamically load dataset into observable framework based on client input? For example, I have like 100 files in a folder. I want to load a specific file based on the text input from client and apply all visualization on that.
Hi there,
You cannot pass a variable into the FileAttachment function, but you can achieve this by loading all the files then filtering for the specific one you need. For example, you could have a dataset that looks something like:
const files = [
{id: 1, file: FileAttachment("./data/file-one.csv").csv()},
{id: 2, file: FileAttachment("./data/file-two.csv").csv()},
{id: 3, file: FileAttachment("./data/file-three.csv").csv()},
]
Then an input could filter for that specific file and use it for the subsequent visualizations.
Depending on what the files contain, you could also use the SQL front matter to load them all, then filter with a SQL code block for the one you need, based again on an input. This would be faster if youāre dealing with larger datasets.
Iām pretty sure that maybe better would be
const files = [
{id: 1, file: FileAttachment("./data/file-one.csv")},
{id: 2, file: FileAttachment("./data/file-two.csv")},
{id: 3, file: FileAttachment("./data/file-three.csv")},
]
Then, you might get the data via something like
let data = await files[0].file.csv();
Iām thinking this approach would avoid the data transfer until itās actually needed.
Is this something that will be considered in the future? It would be very helpful for dynamic calls on parameterized APIs (eg., get ā myapi.com/{param} ).
I understand the alternative in the meantime is to use .fetch(), but āparameterized FileAttachmentsā seems much better
@javpascal Thereās an open issue:
Thanks @mootari! Following on Github now