@cfasim-ui/pyodide 0.3.14 → 0.3.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/pyodide.worker.ts +38 -8
- package/src/vitePlugin.js +15 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfasim-ui/pyodide",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.16",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Pyodide (Python-in-browser) integration for cfasim-ui",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"pyodide": "^0.29.3",
|
|
24
|
-
"@cfasim-ui/shared": "0.3.
|
|
24
|
+
"@cfasim-ui/shared": "0.3.16"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"vue": "^3.5.0"
|
package/src/pyodide.worker.ts
CHANGED
|
@@ -17,18 +17,48 @@ let wheelMap: Record<string, string> = {};
|
|
|
17
17
|
|
|
18
18
|
const baseUrl = import.meta.env.BASE_URL ?? "/";
|
|
19
19
|
|
|
20
|
+
const DEFAULT_PYODIDE_PACKAGES = ["micropip", "numpy"];
|
|
21
|
+
|
|
20
22
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
23
|
let micropip: any;
|
|
22
24
|
|
|
25
|
+
async function fetchPyodidePackages(): Promise<string[]> {
|
|
26
|
+
const url = `${self.location.origin}${baseUrl}pyodide-packages.json`;
|
|
27
|
+
try {
|
|
28
|
+
const res = await fetch(url);
|
|
29
|
+
if (!res.ok) {
|
|
30
|
+
console.warn(
|
|
31
|
+
`[pyodide-worker] ${url} returned ${res.status}; falling back to default packages`,
|
|
32
|
+
);
|
|
33
|
+
return DEFAULT_PYODIDE_PACKAGES;
|
|
34
|
+
}
|
|
35
|
+
const list = await res.json();
|
|
36
|
+
if (!Array.isArray(list)) {
|
|
37
|
+
console.warn(
|
|
38
|
+
`[pyodide-worker] ${url} did not contain an array; falling back to default packages`,
|
|
39
|
+
);
|
|
40
|
+
return DEFAULT_PYODIDE_PACKAGES;
|
|
41
|
+
}
|
|
42
|
+
return list as string[];
|
|
43
|
+
} catch (err) {
|
|
44
|
+
console.warn(
|
|
45
|
+
`[pyodide-worker] failed to load ${url}; falling back to default packages`,
|
|
46
|
+
err,
|
|
47
|
+
);
|
|
48
|
+
return DEFAULT_PYODIDE_PACKAGES;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
23
52
|
const pyodideReadyPromise = (async () => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
53
|
+
const [pyodideModule, packages] = await Promise.all([
|
|
54
|
+
// @ts-expect-error - Pyodide types from CDN
|
|
55
|
+
import(
|
|
56
|
+
/* @vite-ignore */ "https://cdn.jsdelivr.net/pyodide/v0.29.3/full/pyodide.mjs"
|
|
57
|
+
),
|
|
58
|
+
fetchPyodidePackages(),
|
|
59
|
+
]);
|
|
60
|
+
const { loadPyodide } = pyodideModule;
|
|
61
|
+
const pyodide = await loadPyodide({ packages });
|
|
32
62
|
|
|
33
63
|
micropip = pyodide.pyimport("micropip");
|
|
34
64
|
|
package/src/vitePlugin.js
CHANGED
|
@@ -6,17 +6,23 @@ import { resolve } from "node:path";
|
|
|
6
6
|
* @typedef {object} CfasimPyodideOptions
|
|
7
7
|
* @property {string} [model] Path to the Python model directory (default: "model")
|
|
8
8
|
* @property {string[]} [pypiDeps] PyPI packages to download at build time and serve locally
|
|
9
|
+
* @property {string[]} [pyodidePackages] Pyodide built-in packages to preload in the worker (e.g. "scipy", "pandas"). See https://pyodide.org/en/stable/usage/packages-in-pyodide.html. `micropip` and `numpy` are always included.
|
|
9
10
|
* @property {string} [pipCommand] Command used to invoke pip for downloading `pypiDeps` (default: "uvx pip"). Override with e.g. "pip" or "uv run pip".
|
|
10
11
|
* @property {string} [pythonVersion] Python version passed to pip's --python-version flag when downloading `pypiDeps` (default: "3.12"). Should match the Python shipped by your Pyodide runtime.
|
|
11
12
|
*/
|
|
12
13
|
|
|
14
|
+
const DEFAULT_PYODIDE_PACKAGES = ["micropip", "numpy"];
|
|
15
|
+
|
|
13
16
|
/**
|
|
14
17
|
* Vite plugin that builds a Python wheel from a local model directory
|
|
15
|
-
* and generates wheels.json in the public directory.
|
|
18
|
+
* and generates wheels.json + pyodide-packages.json in the public directory.
|
|
16
19
|
*
|
|
17
20
|
* Use `pypiDeps` to prebuild PyPI packages (like cfasim-model) as local
|
|
18
21
|
* wheels for faster browser runtime — avoids PyPI round-trips on page load.
|
|
19
22
|
*
|
|
23
|
+
* Use `pyodidePackages` to preload Pyodide built-in packages (scipy, pandas,
|
|
24
|
+
* etc.) into the Pyodide worker on startup.
|
|
25
|
+
*
|
|
20
26
|
* @param {CfasimPyodideOptions} [options]
|
|
21
27
|
* @returns {import("vite").Plugin}
|
|
22
28
|
*/
|
|
@@ -24,6 +30,10 @@ export function cfasimPyodide(options) {
|
|
|
24
30
|
const modelDir = options?.model ?? "model";
|
|
25
31
|
const pipCommand = options?.pipCommand ?? "uvx pip";
|
|
26
32
|
const pythonVersion = options?.pythonVersion ?? "3.12";
|
|
33
|
+
const extraPackages = options?.pyodidePackages ?? [];
|
|
34
|
+
const pyodidePackages = [
|
|
35
|
+
...new Set([...DEFAULT_PYODIDE_PACKAGES, ...extraPackages]),
|
|
36
|
+
];
|
|
27
37
|
|
|
28
38
|
function build(root) {
|
|
29
39
|
const publicDir = resolve(root, "public");
|
|
@@ -40,6 +50,10 @@ export function cfasimPyodide(options) {
|
|
|
40
50
|
});
|
|
41
51
|
const wheels = readdirSync(publicDir).filter((f) => f.endsWith(".whl"));
|
|
42
52
|
writeFileSync(resolve(publicDir, "wheels.json"), JSON.stringify(wheels));
|
|
53
|
+
writeFileSync(
|
|
54
|
+
resolve(publicDir, "pyodide-packages.json"),
|
|
55
|
+
JSON.stringify(pyodidePackages),
|
|
56
|
+
);
|
|
43
57
|
}
|
|
44
58
|
|
|
45
59
|
return {
|