🏠 back to Observable

Initialize cell values when embedding a notebook

When embedding a notebook as explained in https://observablehq.com/@observablehq/downloading-and-embedding-notebooks, is there a way to provide an initial value to some cells, before any evaluation?

I understand that it’s possible to do:

const main = new Runtime().module(notebook, name => {
  if (name === "chart") {
    return new Inspector(container);
  }
});
main.redefine("data", newData);

but I wonder if it is the correct way to do.

At the point of redefining the data cell, maybe (?) the previous value of data would already have been used to evaluate other cells. And maybe (?) it would be better to have defined that value before launching any cell evaluation.

The snippet you posted will actually redefine the "data" cell before the original cell is evaluated and only newData will run. This is because the cell evaluation occurs in a promise which resolves after the main thread unblocks (see variable_define, which calls variable_defineImpl, which calls runtime_compute, which calls runtime_computeSoon).

Here’s a test using a variant of your code: https://bl.ocks.org/bryangingechen/03cdddd2b669b90dfdf09268338444bb

(Original notebook: https://observablehq.com/d/90c7d6a9d26692a1)

1 Like

Excellent! Thanks for the demonstration, it’s now clear to me that the cells redefined just after the module creation will be run as expected.

1 Like