🏠 back to Observable

Anyway to get all cells' names on a notebook?

I am confused about how Observable works…, especially how a cell’s name is linked to the script file (I guess by the data-node-id, but the fact that there is autofill for cell names seems to indicate that all cells’ names are store somewhere as a list or something)

Is there a way to access this ‘list’ in a cell?

From within an Observable notebook (ie the code that you write), I don’t think there’s a way to access a “list” of cells that are in scope.

But the runtime itself does store that, since the runtime handles cell evaluation and the computation tree. If you had direct access to the “module” of a notebook, you could do something like:

const main = runtime.module();

main.variable().define("a", 1);
main.variable().define("b", 2);
main.variable().define("c", ["a", "b"], (a, b) => a + b);

console.log(Array.from(main._scope.keys())) // ["a", "b", "c"]

Here’s this in action.

But, you don’t have access to a notebook’s runtime from within the notebook itself. The autocomplete works in observablehq.com because the notebook editor has access to the runtime (I assume), but your code that you write does not. If you embed the notebook elsewhere, then you have more control, but not in the typical notebook at observablehq.com.

Overall I recommend How Observable Runs to learn more about how the runtime/notebook cells work!

Thanks a lot!
I have spent quite some time trying to get access to the ‘list’ :joy: seems that I have to change my approach

There is no officially-supported way, but you can get access to the notebook module from within the notebook with a kind of hack; see @mootari’s notebook for a demo:

1 Like

Thanks a lot! I am going to check it out now :slight_smile:

Does anyone know the thinking behind not providing module/runtime access from within a notebook (security issues perhaps) ? Seeing as access is already available, via hacks like @mootari 's, it would be really nice if there was a clean way to do this…

@dhowe That would require providing official support, and would hinder changes to the internals. Same reason there’s no official API.

Seeing as access is already available

Take that statement with truckloads of salt. “Available” is definitely not the right choice of words here, given the nature of the hack, and its various gotchas.

1 Like