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

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