Unlisted
Edited
Apr 4
3 stars
Insert cell
Insert cell
Insert cell
import { localStorageView } from "@tomlarkworthy/local-storage-view"
Insert cell
viewof editor = Inputs.bind(Inputs.text(), localStorageView("code"))
Insert cell
Inputs.button("set code", {
reduce: () => {
viewof editor.value = "example";
viewof editor.dispatchEvent(new Event("input"));
}
})
Insert cell
Insert cell
import { CodeMirror } from "@tomlarkworthy/codemirror-6"
Insert cell
Insert cell
Insert cell
elements = [{ bar: true }]
Insert cell
expr = elements[0].
Insert cell
Insert cell
viewof reactiveMultiControlUI = {
const answer = Inputs.radio(["a", "b"]);
const result = Inputs.text({ label: "result", disabled: true });

const checker = () => {
if (answer.value == "b") result.value = "correct!";
if (answer.value == "a") result.value = "wrong!";
};
answer.addEventListener("input", checker);
invalidation.then(() => answer.removeEventListener("input", checker)); // For every addXXX their is a removeXXX

return Inputs.form([answer, result]);
}
Insert cell
viewof submitted = Inputs.button("submit", {
required: true,
reduce: () => viewof reactiveMultiControlUI.value
})
Insert cell
submitted
Insert cell
Insert cell
viewof ui2 = {
return view`<div>
${["response", Inputs.radio(["a", "b"])]}
${["submit", Inputs.button("submit")]}
${["result", Inputs.text({ label: "result", disabled: true })]}
</div>`;
}
Insert cell
Insert cell
viewof ui2
Insert cell
checker = {
// Note you could put all this logic inside the viewof ui2 code block too
const checker = () => {
if (viewof ui2.value.response === "a") {
viewof ui2.value.result = "wrong";
} else if (viewof ui2.value.response === "b") {
viewof ui2.value.result = "correct";
} else {
viewof ui2.value.result = "you didn't answer!!!";
}
};

viewof ui2.submit.addEventListener("input", checker);
invalidation.then(() =>
viewof ui2.submit.removeEventListener("input", checker)
);
}
Insert cell
viewof ui2
Insert cell
## Fetchp
Insert cell
image1 = FileAttachment("image@1.png").image()
Insert cell
import { fetchp } from "@tomlarkworthy/fetchp"
Insert cell
Insert cell
Insert cell
Insert cell
dynamicRadio = juice(Inputs.radio, {
options: "[0]",
result: "[1].value"
})
Insert cell
viewof control = dynamicRadio(["a", "b", "c"])
Insert cell
Insert cell
control
Insert cell
Insert cell
Insert cell
logic = {
if (filter === "all") {
viewof control.options.value = ["a", "b", "c"];
} else if (filter === "b,c") {
if (viewof control.result.value == "a") viewof control.result.value = null;
viewof control.options.value = ["b", "c"];
} else if (filter === "a,c") {
if (viewof control.result.value == "b") viewof control.result.value = null;
viewof control.options.value = ["a", "c"];
}
viewof control.dispatchEvent(new Event("input", { bubbles: true }));
}
Insert cell
Insert cell
Insert cell
Insert cell
myRequire = d3Require.requireFrom(async (name) => {
return `${registry}/${name}`;
})
Insert cell
Insert cell
(runImport, myRequire("@securityscorecard/design-system@1.0.0-alpha.148"))
Insert cell
Insert cell
viewof selection = Inputs.select(["red", "green", "blue"])
Insert cell
Insert cell
viewof submit1 = Inputs.textarea({ value: selection, submit: true })
Insert cell
submit1
Insert cell
Insert cell
viewof submit2 = Inputs.textarea({ submit: true, minlength: 1 })
Insert cell
submit2
Insert cell
Inputs.bind(viewof selection, viewof submit2)
Insert cell
Insert cell
viewof submit3 = Inputs.button("submit", {
required: true,
reduce: () => selection
})
Insert cell
submit3
Insert cell
Insert cell
Insert cell
import { tweet } from "@mbostock/tweet"
Insert cell
tweet("1442010098482626563")
Insert cell
*I am a mardown cell but I escape to JS and have some smart casting so that if my JS returns a DOM node I can render that too ${html`<input type="range">`}*

Look ${function foo() {
const el = document.createElement("h1")
el.innerHTML = "Programmatic Title in embedded markdown";
return el
}()}
Insert cell
Insert cell
viewof foo = {
// Create a view 'foo' with value 'bar'
return Object.defineProperty(md`foo`, "value", {
value: "bar",
writable: true,
enumerable: true
});
}
Insert cell
viewof foo.value
Insert cell
foo
Insert cell
set_value_to_promise = Inputs.button("set new promise", {
reduce: () => {
viewof foo.value = invalidation;
viewof foo.dispatchEvent(new Event('input', { bubbles: true }));
}
})
Insert cell
set_value_to_undefined = Inputs.button("set undefined", {
reduce: () => {
viewof foo.value = undefined;
viewof foo.dispatchEvent(new Event('input', { bubbles: true }));
}
})
Insert cell
md`If you refresh the 'viewof foo.value' cell it will not resolve`
Insert cell
Insert cell
viewof x = Object.assign(Inputs.range([0, 10], { label: "yo", step: 2 }), {
oninput: event => event.isTrusted && event.stopImmediatePropagation(),
onchange: event => event.currentTarget.dispatchEvent(new Event("input"))
})
Insert cell
x
Insert cell
Promises.delay(Number.NaN)
Insert cell
Insert cell
viewof textAreaWithSubmit = Inputs.textarea({
submit: true
})
Insert cell
textAreaWithSubmit
Insert cell
viewof textWithSubmit = Inputs.text({
submit: true
})
Insert cell
textWithSubmit
Insert cell
Insert cell
viewof start = Inputs.text()
Insert cell
viewof middle = {
const middle = Inputs.text();
Inputs.bind(viewof start, middle);
return middle;
}
Insert cell
viewof end = {
const end = Inputs.text();
Inputs.bind(viewof middle, end);
return end;
}
Insert cell
end
Insert cell
start
Insert cell
md`### Repo multi update`
Insert cell
import { exportString } from "@tomlarkworthy/repo1"
Insert cell
exportString
Insert cell
ganja = md`### Ganja.js`
Insert cell
Algebra = require("ganja.js")
Insert cell
Algebra(2, 0, 1, () => {
// 1e1 is the dimension (Y)
// Change to 1e2 to make this a horizontal line
return this.graph([1e1]);
})
Insert cell
Insert cell
viewof cell_a = Inputs.select(["one", "two"])
Insert cell
cell_a
Insert cell
viewof cell_b = Inputs.text()
Insert cell
Insert cell
updater_with_event = {
// You could declare this updating in cell_b too, but I think its more illastrative taking it into its own cell
viewof cell_a.addEventListener("input", (evt) => {
viewof cell_b.value = viewof cell_a.value; // Updates visual appearance but does not trigger dataflow from cell_b
viewof cell_b.dispatchEvent(new Event("input", { bubbles: true }));
});
}
Insert cell
Insert cell
import { view, variable } from "@tomlarkworthy/view"
Insert cell
import { viewroutine, ask } from "@tomlarkworthy/viewroutine"
Insert cell
Insert cell
viewof cell_c = mutableSelection(["a", "b"])
Insert cell
cell_c
Insert cell
cell_c.selection
Insert cell
updater_first_option_using_cell_b = {
viewof cell_b.addEventListener("input", (evt) => {
viewof cell_c.options.value = [viewof cell_b.value, "other"]; // Assign any array of options you like (does nto support in-place mutation like .push)
viewof cell_c.options.dispatchEvent(new Event("input", { bubbles: true }));
});
}
Insert cell
import ("")
Insert cell
Insert cell
cells = [
{
labelX: "row1",
labelY: "col1",
group: "A",
value: 42
},
{
labelX: "row2",
labelY: "col1",
group: "A",
value: 42
},
{
labelX: "row3",
labelY: "col1",
group: "A",
value: 42
},
{
labelX: "row1",
labelY: "col2",
group: "A",
value: 42
},
{
labelX: "row2",
labelY: "col2",
group: "A",
value: 42
},
{
labelX: "row3",
labelY: "col2",
group: "A",
value: 42
},
{
labelX: "row1",
labelY: "col3",
group: "A",
value: 42
},
{
labelX: "row2",
labelY: "col3",
group: "A",
value: 42
},
{
labelX: "row3",
labelY: "col3",
group: "A",
value: 42
}
]
Insert cell
Plot.plot({
color: { legend: true },
marks: [
Plot.cell(cells, {
x: "labelX",
y: "labelY",
fill: "value"
}),
Plot.tickY(["col2"], { dy: 11 })
]
})
Insert cell
Insert cell
plot_grid = Plot.plot({
color: { legend: true },
marks: [
Plot.cell(cells, {
x: "labelX",
y: "labelY",
fill: "value"
}),
Plot.tickY(["col2"])
]
})
Insert cell
x_scale = plot_grid.scale("x") // d3.scale
Insert cell
x_scale.apply("row2")
Insert cell
Insert cell
viewof response = Inputs.button("connect", {
reduce: async () =>
(
await fetch(
"https://sso.dsmz.de/auth/realms/dsmz/protocol/openid-connect/token",
{
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
},
body: "username=myUsername&password=myPassword&client_id=api.bacdive.public&grant_type=password&code=&redirect_uri=&scope=openid"
}
)
).text()
})
Insert cell
response
Insert cell
Insert cell
viewof pos = Plot.plot({
marks: [
Plot.dot(
penguins,
Plot.pointer({
x: "culmen_length_mm",
y: "culmen_depth_mm",
fill: "red",
r: 8
})
),
Plot.dot(penguins, { x: "culmen_length_mm", y: "culmen_depth_mm" })
]
})
Insert cell
pos
Insert cell
d3.csv(
"https://docs.google.com/spreadsheets/d/e/2PACX-1vStElZLXAh6_Zy1z6HqCc6xf6_xOVBJMOzVvNQGLOf6KmsVY07etajt3_gFplnqoCCOi17bBkib_Xv0/pub?gid=2033815436&single=true&output=csv"
)
Insert cell
(
await fetch(
"https://docs.google.com/spreadsheets/d/e/2PACX-1vStElZLXAh6_Zy1z6HqCc6xf6_xOVBJMOzVvNQGLOf6KmsVY07etajt3_gFplnqoCCOi17bBkib_Xv0/pub?gid=2033815436&single=true&output=csv",
{
method: "HEAD"
}
)
).headers.get("content-length")
Insert cell
faster = {
const sheetId = "1Z7Dja43FepxVOJc5_pMdP0etERM6h0BPAWT74zjdbno";
const sheetName = "Sheet1"; // Change if your sheet name is different
const url = `https://docs.google.com/spreadsheets/d/2PACX-1vStElZLXAh6_Zy1z6HqCc6xf6_xOVBJMOzVvNQGLOf6KmsVY07etajt3_gFplnqoCCOi17bBkib_Xv0/gviz/tq?tqx=out:csv&sheet=${sheetName}`;

return d3.csv(url);
}
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