Published
Edited
Sep 5, 2022
Importers
7 stars
Insert cell
Insert cell
Insert cell
function peek({
notebook = "@tomlarkworthy/metaprogramming", // Target notebook
cell = undefined, // Name of target cell, or undefined for anonymous
filter = value => true // Filter values (useful for extracting anonymous values
} = {}) {
notebook = notebook.replace('https://observablehq.com/', '');
const safeFilter = value => {
try {
return filter(value);
} catch (err) {
console.error(err);
return false;
}
};
return Generators.observe(next => {
// See Mike Bostock's https://talk.observablehq.com/t/importcell-conflicts-with-static-import/4548/14
// Instantiate a new runtime, but reuse the existing runtime’s require.
// (This is ensures that any requires in imports still work.)
const library = Object.assign(new Library(), { require: () => require });
const runtime = new Runtime(library);

import(`https://api.observablehq.com/${notebook}.js?v=3`)
.then(({ default: define } = {}) => {
const imported = runtime.module(define, name => {
if (cell && name !== cell) return null;
console.log("name", name);
return {
fulfilled(value) {
if (safeFilter(value)) next(value);
},
rejected(err) {
if (safeFilter(err)) next(Promise.reject(err));
}
};
});
})
.catch(err => next(Promise.reject(err)));

return () => runtime.dispose();
});
}
Insert cell
Insert cell
Insert cell
Insert cell
function peekMany({
notebook = "@tomlarkworthy/metaprogramming", // Target notebook
cells = [] // Name of target cell, or undefined for anonymous
} = {}) {
notebook = notebook.replace('https://observablehq.com/', '');

const generators = [];
const nexts = cells.reduce((acc, cell) => {
generators.push(Generators.observe(next => (acc[cell] = next)));
return acc;
}, {});

// See Mike Bostock's https://talk.observablehq.com/t/importcell-conflicts-with-static-import/4548/14
// Instantiate a new runtime, but reuse the existing runtime’s require.
// (This is ensures that any requires in imports still work.)
const library = Object.assign(new Library(), { require: () => require });
const runtime = new Runtime(library);

import(`https://api.observablehq.com/${notebook}.js?v=3`).then(
({ default: define } = {}) => {
const imported = runtime.module(define, name => {
if (!nexts[name]) return null;
return {
fulfilled(value) {
nexts[name](value);
},
rejected(err) {
nexts[name](Promise.reject(err));
}
};
});
}
);
return generators;
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
md`## Examples`
Insert cell
peek_value = peek({
notebook: "@tomlarkworthy/metaprogramming",
cell: 'constant_string'
});
Insert cell
/*
peek_injected_value = peek({
notebook: "@tomlarkworthy/metaprogramming",
cell: 'constant_string',
injections: { constant_string: "rewrite" }
})*/
Insert cell
peek_promise = peek({
notebook: "@tomlarkworthy/metaprogramming",
cell: 'promise'
});
Insert cell
peek_promise_err = peek({
notebook: "@tomlarkworthy/metaprogramming",
cell: 'promise_err'
});
Insert cell
peek_generator = peek({
notebook: "@tomlarkworthy/metaprogramming",
cell: 'generator'
});
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
/*
peekTests.test("peek_injected_value", async () => {
expect(peek_injected_value).toMatch("rewrite");
})*/
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
observablehq = require("@observablehq/runtime@4")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more