Public
Edited
Sep 1, 2024
Importers
1 star
Insert cell
Insert cell
copyCellButton(["x = Math.sin(now / 1000)", "y = Math.round(x * 10)"])
Insert cell
copyCellButton([{type: "md", value: "# Hello"}, "now"])
Insert cell
Insert cell
Insert cell
cellsToClipboard = (cells) => {
function listener(e) {
e.clipboardData.setData("text/plain", cells.join("\n\n"));
e.clipboardData.setData(
"application/vnd.observablehq+json",
JSON.stringify(cells.map(makeCell))
);
e.preventDefault();
}
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
}
Insert cell
copyCellButton = (cells) =>
Inputs.button("Copy cells", { reduce: () => cellsToClipboard(cells) })
Insert cell
makeCell = (value) => {
const id = mutable id++;
if (typeof value === "string") {
return { ...defaultJS, id, value };
} else if (typeof value === "object") {
if (value.type === "js") return { ...defaultJS, id, ...value };
return { ...defaultOther, id, ...value };
} else {
throw new Error("Value must be string or object");
}
}
Insert cell
defaultJS = ({
id: 0,
value: "",
pinned: true,
mode: "js",
data: null,
name: null
});
Insert cell
defaultOther = ({
id: 0,
value: "",
pinned: false,
mode: "md",
data: null,
name: ""
})
Insert cell
// I don't think pasting cells even looks at their IDs but this lets them autoincrement
mutable id = 0
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