Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Import Pyodide
import pyodideModule from "npm:pyodide/pyodide.js";
// Mocking necessary global objects to mimic browser environment in Deno
(globalThis as any).window = {};
(globalThis as any).document = { location: { href: "" }, currentScript: { src: "" } };
(globalThis as any).navigator = {};
// Function to load Pyodide and Python packages
export async function loadPython(params?: { packages: string[] }) {
const pyodide = await pyodideModule.loadPyodide();
for (const pkg of params?.packages || []) {
await pyodide.loadPackage(pkg);
}
// Returns a function that can run Python code asynchronously
return (strings: TemplateStringsArray) => pyodide.runPythonAsync(strings[0]);
}
// Function to run Python code
async function runHelloWorld() {
const runPython = await loadPython();
// Run a simple Python print statement
await runPython`print("Hello, World!")`;
}
// Execute the function
runHelloWorld().catch(console.error);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 29, 2024