Cloud files and unique names

Hi! I’m trying to generate reports for a few different cases, all with the same data structure. The data exists in Google Drive at for example /Cases/[case#]/data.csv. I can pull that in just fine, but when I add the data.csv for the second or third case, Observable seems to reuse the file from the first case.

When I look at an older notebook, which defaulted to FileAttachment() instead of those tables, I can see that it references google://somefile. So maybe Observable invents its own, internal representation for each file?

Is this by design? How can I work around it?

Ideally, I’d be able to create a dropdown selector for each of the cases and use its value to read the correct file, generating the report for whichever case I’m curious about.

Thanks!

Working around this, I rewrote the output parts to prefix each file with the case ID. That works but it’s a bit messy in Observable’s UI, as the long names are cut off and you have no idea which file is which. I had to toggle sort by name in the file selector and in Google Drive and then count my way through to see which files had been imported and which hadn’t.

That’s when I discovered the next bump in the road: FileAttachment requires a single literal string argument.

So even if I can now pull the individual files in, there’s no way for me to reference them and actually use them in the code.

So I’m back to square one. Now I’m thinking I’ll just rewrite the whole thing again, to try and dump all the data in one humongous file. That should work but it kind of sucks that I won’t be able to rerun the analyses for each individual case.

I’m afraid I have difficulty visualizing the scenario that you’re describing. Could you perhaps share a (redacted) screenshot and/or example code?

Thanks for the reply! Let me simplify as an example. :slight_smile:

In Google Drive, I have three directories: Case1, Case2, and Case3. Inside each directory I have a file called data.json.

When I add the cloud file Case1/data.json to my notebook, it works as expected. But when I go to add Case2/data.json, it won’t let me add another file with the same name, so instead Observable reuses the first file.

Does that make sense? That was the first problem.

The second one was when I instead split the files up so that they were named Case1_data.json, Case2_data.json, Case3_data.json, all within the same directory. That let me read each file just file.

However, I wanted to use a select input to pick which case I was interested in and then read its specific data file, for the reporting. However, FileAttachment doesn’t let you use dynamic filenames; they need to be literal strings.

Hope that clarifies it! Either way, all of this seem to be a design decision of Observable, so I’ve instead worked my way around it by dumping all data into one big JSON file.

Thanks for the details!

I’m fairly certain that this is a bug, and I’ve filed an internal issue for it. Unfortunately it’s unlikely that it will be fixed soon.

That is indeed by design because the references are analyzed statically. The workaround is to list all FileAttachments explicitly, e.g.

viewof file = Inputs.select([
  FileAttachment("google://doc"),
  FileAttachment("google://doc 2"),
], {
  label: "Select a file",
  format: d => d.name.split("//")[1],
})
file.text()

2 Likes