Interactively select file attachments

Let’s say I have multiple file attachments named

  • test-a.csv
  • test-b.csv

And I would like to pick one interactively. Something like this doesn’t work:

vSet = Inputs.select(["a", "b"], {label: "Set", multiple: false});
fileName = `test-${vSet.value}.csv`;
data = FileAttachment(fileName).csv({typed: true});

SyntaxError: FileAttachment requires a single literal string argument

Here is an example document: Interactive file selection / workbook | Observable

Thanks!

This is a frequent question. Here’s what the docs say:

https://observablehq.com/framework/files#static-analysis

1 Like

This works.

files = [
  { label: "a", file: FileAttachment("test-a.csv") },
  { label: "b", file: FileAttachment("test-b.csv") },
];

viewof v = Inputs.select(["a","b"], {label: "Select a set", multiple: false});

files[files.findIndex(file => file.label === v)].file.csv({typed: true});
1 Like