Access current cell metadata

There was a previous request along these lines here but in the end something much simpler (a trick to get the name of a function argument by wrapping it in an object) sufficed.

Here are some thoughts on how you might approach getting that data now. I see ways that you can get something like the Notebook global (with data about all the cells in the notebook), but getting a currentCell object working seems harder:

As hinted at in the above-linked thread, you can get a lot of the information you want about a notebook by fetching the notebook JSON object from the Observable API (specifically, from https://api.observablehq.com/document/username/notebook-name); however, this can’t be done directly from a notebook because the origin of the request needs to be observablehq.com and not .observableusercontent.com. I think there used to be some notebooks like this one which got around this using a CORS proxy, but the proxies don’t seem to be working at the moment.

An imperfect way to get something like this notebook JSON data is to use the fromV1 function I’ve written here, which lets you pass a URL to the notebook’s runtime V1 module JS file (which the API lets you access from a notebook). This function can reconstruct a notebook JSON object from the JS file (n.b. this is stuck at the most-recently shared / published version of the notebook), but there are limitations, particularly for notebooks with imports.

Note that the notebook JSON object only contains the id of each cell (currently, a number) and the cell “body” (i.e. the source string you submit for the cell). To get the name and viewof data you want from that, you can pass the cell bodies to the observable parser library; (see e.g. this notebook of mine).

Finally, let’s assume you have accomplished enough of the above so that one of your live cells cell1 has access to a parsed notebook array nbdat whose elements are objects with metadata on each of the cells in that notebook. Then you still have the problem of figuring out from within cell1 which entry of nbdat it corresponds to. This seems tricky to do in general.

OK, I ended up making a rough little demo notebook. The punchline is that cells which output HTML elements can sort of get their own metadata, but you’ll need a “real” notebook JSON object – the one created by fromV1 won’t be good enough:

4 Likes