Public
Edited
Aug 9, 2023
Importers
Insert cell
Insert cell
Insert cell
py = async (strings, ...expressions) => {
let globals = {};
const code = strings.reduce((result, string, index) => {
if (expressions[index]) {
const name = `x${index}`;
globals[name] = expressions[index];
return result + string + name;
}
return result + string;
}, "");
await pyodide.loadPackagesFromImports(code);
const result = await pyodide.pyodide_py.code.eval_code_async(
code,
pyodide.toPy(globals)
);
if (result?.toJs) return result.toJs();
return result;
}
Insert cell
pyodide = {
const pyodide_pkg =
await require("https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.js");
return await pyodide_pkg.loadPyodide({
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.23.4/full/"
});
}
Insert cell
/* todo: use correct mime type */
downloadPyoFile = async (path) => {
// read with javascript, using e.g.
// pyodide.FS.root.contents["tmp.ttl"].contents
// but it doesn't allow using a path
const readArrayBuffer = py`
read = ""
with open(${path}, "rb") as f:
read = f.read()
read
`;
const blob = new Blob([await readArrayBuffer], {
type: "text/plain"
});
return DOM.download(blob, path, "Download the " + path);
}
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