Published
Edited
Nov 2, 2019
1 fork
17 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
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
nbDefine = (await import(apiUrl)).default
Insert cell
Insert cell
m1 = {
const runtime = new oRuntime();
const main = nbDefine(runtime, name => true);
await runtime._compute();
runtime.dispose();
return main;
}
Insert cell
Insert cell
scope1 = [...m1._runtime._modules.values()].map(({ _scope }) => _scope)
Insert cell
Insert cell
varset1 = m1._runtime._variables
Insert cell
Insert cell
example_a = m1._resolve('a')
Insert cell
importOfDuplicate = example_a._inputs[0]
Insert cell
Insert cell
varset1.has(importOfDuplicate)
Insert cell
Insert cell
allVars1 = {
const allVars1 = new Set(varset1);
for (const v of allVars1) {
for (const w of v._inputs) {
allVars1.add(w);
}
}
return allVars1;
}
Insert cell
Insert cell
Array.from(varset1, getAllPropertyNamesAsObj)
Insert cell
v1 = Array.from(varset1)
// .filter(({ _reachable }) => _reachable)
.map(({ _name }) => _name)
Insert cell
Insert cell
v1.filter(s => s === 'DOM')
Insert cell
Insert cell
modules = new Set(Array.from(varset1, ({ _module }) => _module))
Insert cell
Array.from(modules, getAllPropertyNamesAsObj)
Insert cell
Insert cell
varsetReachable = Array.from(varset1).filter(({ _reachable }) => _reachable)
Insert cell
modulesReachable = new Set(
Array.from(varsetReachable, ({ _module }) => _module)
)
Insert cell
Insert cell
testImportMap = await importMap(apiUrl)
Insert cell
Insert cell
m2 = {
const runtime = new Runtime(testImportMap);
const main = nbDefine(runtime, name => true);
main._slug = [...testImportMap.values()][0];
await runtime._compute();
runtime.dispose();
return main;
}
Insert cell
r2 = m2._runtime
Insert cell
allModules2Set = {
const all = new Set([...r2._variables].map(({ _module }) => _module));
Array.from(all, (m, i) => {
m._id = i;
});
return all;
}
Insert cell
allModules2 = Array.from(allModules2Set)
Insert cell
Insert cell
rVariables2 = [...r2._variables]
Insert cell
Insert cell
moreVariables2Set = {
const allVars = new Set(rVariables2);
for (const v of allVars) {
for (const w of v._inputs) {
allVars.add(w);
}
}
return allVars;
}
Insert cell
Insert cell
allVariables2 = r2._allVariables.map((v, i) => {
v.def = v._definition.toString();
v._id = i;
return v;
})
Insert cell
Insert cell
reachableVars2 = [...r2._allVariables].filter(({ _reachable }) => _reachable)
Insert cell
reachableModules2Set = new Set(reachableVars2.map(({ _module }) => _module))
Insert cell
reachableModules2 = Array.from(reachableModules2Set)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// titles
[...r2._modules.keys()].map(d => d.toString().match(/# ([^\n]+)/g)[0])
Insert cell
// imports
[...r2._variables].filter(({ _definition }) => _definition === identity)
Insert cell
Insert cell
Insert cell
version = '4.6.3'
Insert cell
observable = require(`@observablehq/runtime@${version}`)
Insert cell
oRuntime = observable.Runtime
Insert cell
// used below
Library = observable.Library
Insert cell
// https://github.com/observablehq/stdlib/blob/81a7995764923b73decfe3e50844dae2c15d2a9a/src/fileAttachment.js#L50
function FileAttachments(resolve) {
return name => {
const url = resolve((name += "")); // Returns a Promise, string, or null.
if (url == null) throw new Error(`File not found: ${name}`);
return new FileAttachment(url, name);
};
}
Insert cell
Insert cell
// assumes first line of JS module has a comment with the notebook URL
async function fetchDefine(apiUrl) {
const [{ default: define }, src] = await Promise.all([
import(apiUrl),
fetch(apiUrl).then(res => res.text())
]);
return { define, src, nbUrl: src.split('\n')[0].slice(3) };
}
Insert cell
// assumes lines after the first until the first double line break contain either:
// - imports of other notebook JS modules
// - no imports (returns [])
function parseImports(src) {
const matches = src
.split('\n\n')[0]
.split('\n')
.slice(1)
.map(s => s.match(/^import (\w+) from "([^"]+)"/));
return matches[0]
? matches.map(([, name, relativeUrl]) => ({
name,
slug: relativeUrl.slice(0, -7),
apiUrl: "https://api.observablehq.com" + relativeUrl
}))
: [];
}
Insert cell
async function importMap(apiUrl) {
const map = new Map();
const seen = new Set([apiUrl]);

const { src, define } = await fetchDefine(apiUrl);
map.set(define.toString(), apiUrl.match(/\.com\/([^\.]+)/)[1]);
const queue = parseImports(src);

for (const { name, apiUrl } of queue) {
if (!seen.has(apiUrl)) {
seen.add(apiUrl);
const { src, define } = await fetchDefine(apiUrl);
map.set(define.toString(), apiUrl.match(/\.com\/([^\.]+)/)[1]);
queue.push(...parseImports(src));
}
}
return map;
}
Insert cell
Insert cell
Insert cell
Insert cell
RuntimeError = Error
Insert cell
variable_invalidation = ({})
Insert cell
variable_visibility = ({})
Insert cell
frame = requestAnimationFrame
Insert cell
function generatorish(value) {
return (
value &&
typeof value.next === "function" &&
typeof value.return === "function"
);
}
Insert cell
function window_global(name) {
return window[name];
}
Insert cell
Insert cell
TYPE_NORMAL = 1
Insert cell
TYPE_IMPLICIT = 2
Insert cell
TYPE_DUPLICATE = 3
Insert cell
function rethrow(e) {
return function() {
throw e;
};
}
Insert cell
Insert cell
no_observer = ({})
Insert cell
function identity(x) {
return x;
}
Insert cell
noop = () => {}
Insert cell
function constant(x) {
return function() {
return x;
};
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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