Unlisted
Edited
Aug 22, 2022
Paused
Importers
23 stars
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
script = `Promise.all([
(${fetchDefine})("${notebook.module}"),
(${fetchDependencies})(${encode({minified: min})})
]).then(([define, {${Object.keys(deps)}}])=>{

${inspectImport}
${highlight}

(${main})(define, ${encode(data)}, ${encode({pinAll})})

})`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
[...comments]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
async function fetchDependencies({minified = true} = {}) {
const [
{Inspector, Runtime, Library},
{parseCell},
{default: hljs},
] = await Promise.all([
import(minified ? 'https://esm.sh/@observablehq/runtime@5' : 'https://cdn.skypack.dev/@observablehq/runtime@5'),
import(minified ? 'https://esm.sh/@observablehq/parser@6.1' : 'https://unpkg.com/@observablehq/parser?module@6.1'),
import('https://cdn.skypack.dev/highlight.js@11.5.1'),
]);
hljs.registerLanguage('javascript', hljs => {
const def = hljs.getLanguage('javascript').rawDefinition();
def.keywords.keyword.push('viewof', 'mutable');
return def;
});
const {html} = await new Library().htl();
return {Inspector, Library, Runtime, html, hljs, parseCell};
}
Insert cell
function main(define, notebook, options) {
const {
pinAll = false,
container = document.body,
filter = (node, i) => true,
runtime = new Runtime()
} = options;
const {nodes} = notebook;

const noop = () => {};
const languages = { md: 'markdown', js: 'javascript', html: 'html', sql: 'sql' };


const observers = [];
const cells = nodes.map((node, i) => {
const {value, mode, pinned, name} = node;
const isImport = mode === 'js' && value.startsWith('import ');

if(!filter(node, i, nodes)) {
if(!isImport) observers.push(() => null);
return null;
}
const input = highlight(value, {language: languages[mode] ?? 'text'});
input.style.display = pinned || pinAll ? null : 'none';
const output = html`<div class="observablehq">`;
const element = html`<div>${output}${input}`;
if(isImport) output.appendChild(inspectImport(parseCell(value)));
else observers.push(() => new Inspector(output));
return element;
});

container.append(...cells.filter(d => d));

let i = 0, skip = false;
return runtime.module(define, name => {
if(!skip) {
// For mutable, we inspect the next variable instead.
if(name?.startsWith('mutable ')) return;
// For viewof, we skip the next variable.
if(name?.startsWith('viewof ')) skip = true;
return observers[i++]();
}
skip = false;
});
}
Insert cell
Insert cell
Insert cell
Insert cell
renderComments(data.comments)
Insert cell
data.comments
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
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more