Public
Edited
Feb 21, 2023
Insert cell
Insert cell
math = (await import('https://cdn.skypack.dev/mathjs'))
Insert cell
function branchConditionalHandler(node, options) {
if (node.type !== 'ConditionalNode') {
return node.toTex(options)
}
const conditions = [node]
let curr = node
while (curr.falseExpr.type === 'ConditionalNode') {
conditions.push(curr.falseExpr)
curr = curr.falseExpr
}
const deparen = node => node.type === 'ParenthesisNode' ? node.content : node
const deparenCondition = c => ({...c, condition: deparen(c.condition), trueExpr: deparen(c.trueExpr), falseExpr: deparen(c.falseExpr)})
conditions.push(conditions[conditions.length - 1]) // duplicate the final condition
return conditions.map(deparenCondition).map((c, i) =>
i === 0 ? `\\mathbf{if} \\> ${c.condition.toTex()}: \\\\ \\quad ${c.trueExpr.toTex()}`
: i !== conditions.length - 1 ? `\\mathbf{elif} \\> ${c.condition.toTex()}: \\\\ \\quad ${c.trueExpr.toTex()}`
: `\\mathbf{else :} \\\\ \\quad ${c.falseExpr.toTex()}`).join('\\\\')
}
Insert cell
htl.html`<button onclick=${() => navigator.clipboard.writeText('hey')}>click me</button>`
Insert cell
tex`${math.parse('x > 1 ? 1 : 0').toTex({handler: (node, options) => node.type === 'ConditionalNode' ? `\\mathbf{if} \\> ${node.condition.toTex()}: \\\\ \\quad ${node.trueExpr.toTex()} \\\\ \\mathbf{else}: \\\\ \\quad ${node.falseExpr.toTex()}` : node.toTex(options)})}`
Insert cell
tex`${math.parse('(x > 1) ? (0) : x > 2 ? 1 : 3').toTex({handler: branchConditionalHandler})}`
Insert cell
math.parse('(x > 1)')
Insert cell
math.parse("(x)")
Insert cell
async function waitUntil(testFn, interval) {
if (testFn()) {return;}
let resolve = undefined
const p = new Promise(r => resolve = r)
const id = setInterval(() => { if (testFn()) { clearInterval(id); resolve(); }}, interval )
await p
}
Insert cell
a = ({b: 40})
Insert cell
{
const t0 = performance.now()
await waitUntil(() => a.b == 42, 1000)
return performance.now() - t0
}
Insert cell
a.b = 42
Insert cell
viewof table = Inputs.table(cars)
Insert cell
inputs = require('@observablehq/inputs')
Insert cell
inputs.table(cars)
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