Published
Edited
Oct 22, 2021
7 stars
Insert cell
Insert cell
Insert cell
Insert cell
a = `I show up first, depend on the value of "b" defined later: ${b}`
Insert cell
b = '"Cell b" here! Wow, observable knew to run me first. 🧠!'
Insert cell
Insert cell
"Implicitely, this naked string gets 'returned' from this cell and Observable displays it"
Insert cell
42
Insert cell
Insert cell
immuteyTheObservableVariable = "Haha, I'm still in JS land"
Insert cell
immuteyTheObservableVariable + ' 😊'
Insert cell
new Promise(resolve =>
setTimeout(
() =>
resolve(
`${new Date()} -- Async code is native to Observable: it just waits patiently for it to resolve. Re-run this cell...`
),
1000
)
)
Insert cell
Insert cell
const whoops = "I forgot that left of = is Observable Land, no JS-style variable declarations allowed 😛"
Insert cell
Insert cell
outputReturned = {
const x = 40;
const y = 2;
return x + y;
}
Insert cell
Insert cell
outputYielded = {
const x = 40;
const y = 2;
yield x + y;
}
Insert cell
Insert cell
{
yield 1;
yield 2;
yield 3;
yield "Yowza, 1,2, and 3 were yielded so fast I didn't even see the numbers!";

const p = new Promise(resolve =>
setTimeout(
() =>
resolve(
'If you yield a promise, Observable waits for it to fetch, then displays it 🐶 '
),
2000
)
);
yield p;
}
Insert cell
Insert cell
opacity = {
let opacity = 0;
while (true) {
if (opacity > 1) {
opacity = 0;
}
opacity += 0.01;
yield opacity;
}
}
Insert cell
html`<h1 style="opacity: ${opacity}">Weeeee, I love generatorsssss</h1>`
Insert cell
md`Which brings us to our last point: when inside JS Land, Observable leaves you a few functions floating in the global namespace, most prominently: \`md()\` and \`html()\``
Insert cell
html
Insert cell
md
Insert cell
html(["<h2>You can use regular () style function calls</h2>"])
Insert cell
html`<cool-kids style="color: BlueViolet">... but the cool kids use template literals</cool-kids>`
Insert cell
Insert cell
_ = require("lodash")
Insert cell
_.camelCase('I love lodash')
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
{
const color = d3
.scaleLinear()
.domain([0, 1])
.range(['red', 'blue'])(opacity);

yield html`<div style="color: ${color}">D3 scales numbers into colors, wowow</div>`;
}
Insert cell
Insert cell
import { slider } from '@jashkenas/inputs'
Insert cell
html`<h1 style="opacity: ${sliderOutput}">Weeeee ... can't let generators have all the fun. Slider time!</h1>`
Insert cell
viewof sliderOutput = slider()
Insert cell
Insert cell
import { chart } from '@observablehq/notebook-visualizer'
Insert cell
chart
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