MCPcopy
hub / github.com/mayneyao/eidos / loadPyodideAndPackages

Function loadPyodideAndPackages

packages/worker/web-worker/pyodide/pyodide.ts:43–90  ·  view source on GitHub ↗
(
  config?: PyodideConfig
)

Source from the content-addressed store, hash-verified

41
42let pyodide: PyodideInterface
43async function loadPyodideAndPackages(
44 config?: PyodideConfig
45): Promise<PyodideInterface> {
46 pyodide = await loadPyodide({
47 indexURL: "https://testingcf.jsdelivr.net/pyodide/v0.27.0/full/",
48 fullStdLib: false,
49 stdout: (text: string) => {
50 self.postMessage({
51 type: "PythonStdout",
52 data: text,
53 })
54 },
55 stderr: (text: string) => {
56 self.postMessage({
57 type: "PythonStderr",
58 data: text,
59 })
60 },
61 })
62 const dirHandle = await navigator.storage.getDirectory()
63 const permissionStatus = await dirHandle.requestPermission({
64 mode: "readwrite",
65 })
66
67 if (permissionStatus !== "granted") {
68 throw new Error("readwrite access to directory not granted")
69 }
70 const nativefs = await pyodide.mountNativeFS("/mount_dir", dirHandle)
71
72 await pyodide.loadPackage("micropip")
73
74 if (config?.preloadPackages?.length) {
75 const micropip = pyodide.pyimport("micropip")
76 await Promise.all(
77 config.preloadPackages.map((pkg) => micropip.install(pkg))
78 )
79
80 for (const pkg of config.preloadPackages) {
81 try {
82 pyodide.pyimport(pkg)
83 } catch (error) {
84 console.warn(`Failed to import preloaded package ${pkg}:`, error)
85 }
86 }
87 }
88
89 return pyodide
90}
91
92const getPyodide = async (
93 config?: PyodideConfig

Callers 1

getPyodideFunction · 0.85

Calls 5

getDirectoryMethod · 0.80
allMethod · 0.80
requestPermissionMethod · 0.65
warnMethod · 0.65
installMethod · 0.45

Tested by

no test coverage detected