I’m trying to build my own observable UI for an experimental tool at work. Right now I have the observable runtime working, where I have a module with variables added to it, and they update the DOM etc.
The issue I’m seeing is this: it looks like variables always take either a constant value, or a function, i.e.
myModule.variable(...).define('blah', [], () => 'hello world')
But I want to, just like observablehq, allow the user to create their own cells in the browser. If I give them, for example, textareas to type their code into, what I get out of that is a String, and it doesn’t look like you can pass a string to variable.define and have it parsed into JavaScript. I want to be doing something like this:
myModule.variable(...).define('blah', ['html'], html => "html
Hello world!
")
Similarly, it’s not clear to me how to calculate the dependencies (in the example above, html) based on the string I get back from a textarea. I see that there’s a parser in the observable codebase here https://github.com/observablehq/parser which returns useful things like whether a chunk of code is a generator or is async etc., but it doesn’t look like it does anything about calculating deps, and anyway it’s not returning me JS that I can use.
Sorry if this is obvious, but I’m just wondering what black magic the observable team are doing to turn strings in the cells/variables in the browser into JavaScript. Are you taking the contents of a cell and then dynamically adding it via script tag, wrapped in module export or something?
For some background, basically I’m trying to build a similar UI to observablehq for a tool at work, and because it would have special access to our API’s etc. it would need to be self-hosted. In other words, I know you provide some “Teams” stuff, but we wouldn’t be allowed to use that here. I also realize that this might be part of your secret sauce for observablehq, so I understand if you don’t want to divulge your secrets…
Thanks again for the awesome tool and I appreciate any help in advance!