Public
Edited
Nov 7, 2024
Insert cell
Insert cell
Insert cell
mutable state_data = ({})
Insert cell
Insert cell
Insert cell
Insert cell
viewof name = Inputs.text({value : state_data.name})
Insert cell
viewof description = Inputs.text({value : state_data.description})
Insert cell
viewof status = Inputs.text({value : state_data.status})
Insert cell
viewof load_template_data = Inputs.button("Load Template", {reduce: () => swap_data(template_data)});
Insert cell
viewof load_file_data = Inputs.button("Load File Data", {reduce: () => swap_data(file_data)});
Insert cell
function swap_data(data) {
mutable state_data = data;
return state_data
}
Insert cell
// If I don't have anything in local storage, I'll work from this template
template_data = ({
name: "Sample Project",
description: "Initial project data",
status: "active"
})
Insert cell
// Let's assume these are the data values after a change.
file_data = ({
name: "File Project",
description: "Changed project data",
status: "pending"
})
Insert cell
ephemeral_state = (function() {
const ephemeral_state = localStorage.getItem('state_data');
return ephemeral_state !== null && ephemeral_state !== 'null'
? mutable state_data = JSON.parse(ephemeral_state)
: mutable state_data = template_data;
})();
Insert cell
current_state = ({
name: name,
description: description,
status: status
})
Insert cell
viewof persist_current_state = Inputs.button("Persist Changed Data", {
reduce: () => {
localStorage.setItem('state_data', JSON.stringify(current_state)) }
})
Insert cell
download_name = "state_data"
Insert cell
DOM.download(serialize(current_state), download_name, "Download Current State")
Insert cell
function serialize (data) {
let s = JSON.stringify(data);
return new Blob([s], {type: "application/json"})
}
Insert cell
Insert cell
Insert cell
/// not updating mutable until intermediate action taken
viewof local_data = Inputs.file({
label : "Load Project",
multiple : false,
//reduce: (f) => swap_data(f.json()),
})
Insert cell
load_data = ({
name: "Load Project",
description: "Loaded project data",
status: "loaded"
})
Insert cell
// swap_data(local_data)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
(async function(d) {
return d === null ? "pending file selection" : mutable state_data = await d.json();
})(local_data);
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