-
Hello, I'm trying to create dynamic links to data loaders as described in the docs. I'm a bit surprised it works with parameters from the URL, but not with static variables. Markup page: Working codeconst sheet = FileAttachment(
`./data/sheets/sheetId-${observable.params.sheetId}-tabId-${observable.params.tabId}.json`
).json(); display(sheet); Not working codeconst sheetId = "XYZ";
const tabId = "123";
const path = `./data/sheets/sheetId-${sheetId}-tabId-${tabId}.json`;
const sheet2 = FileAttachment(path).json(); display(sheet2); Error message: SyntaxError: FileAttachment requires a single literal string argument (4:15) QuestionWhat is the difference between a string literal composed of URL parameters and and local variables? Maybe an oversight of something else... ? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The difference is static analysis. The Observable markdown parser can detect the former, because it has a unique shape that does not require to run the code to know what the values are. But for the latter, the only way to know the value of the variable is to run the code—and this is out of bounds (and sometimes squarely impossible, say if your code depends on a random number or a date). In other words (and that may be a bit of a stretch of a metaphor), we can "compile" the page, but not "execute" it. (At the end of the process we need to build static files.) |
Beta Was this translation helpful? Give feedback.
The difference is static analysis. The Observable markdown parser can detect the former, because it has a unique shape that does not require to run the code to know what the values are.
But for the latter, the only way to know the value of the variable is to run the code—and this is out of bounds (and sometimes squarely impossible, say if your code depends on a random number or a date).
In other words (and that may be a bit of a stretch of a metaphor), we can "compile" the page, but not "execute" it. (At the end of the process we need to build static files.)