Skip to content

Added lazy_py_modules #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,25 @@ The module is registered within the interpreter as *JS* module and it offers var

### Main exports

| name | example | description |
| :------------ | :------------------------------------- | :---------- |
| XWorker | `from polyscript import XWorker` | described in the [XWorker](#xworker) part. |
| currentScript | `from polyscript import currentScript` | it's an explicit, always correct, reference to the current node running the generic script code. |
| js_modules | `from polyscript import js_modules` | described in the [Extra config Features](#extra-config-features) part. |
| name | example | description |
| :-------------- | :--------------------------------------- | :---------- |
| XWorker | `from polyscript import XWorker` | described in the [XWorker](#xworker) part. |
| currentScript | `from polyscript import currentScript` | it's an explicit, always correct, reference to the current node running the generic script code. |
| js_modules | `from polyscript import js_modules` | described in the [Extra config Features](#extra-config-features) part. |
| lazy_py_modules | `from polyscript import lazy_py_modules` | allows, only in *Python* related interpreters, and without needing static config entries, to import lazily any available module.


#### lazy_py_modules

```html
<script type="pyodide" async>
from polyscript import lazy_py_modules

matplotlib, regex, = await lazy_py_modules("matplotlib", "regex")

print(matplotlib, regex)
</script>
```


### Worker exports
Expand Down
4 changes: 2 additions & 2 deletions docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions esm/interpreter/_python.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { dedent } from '../utils.js';
import { io } from './_io.js';

export const loader = new WeakMap();

// REQUIRES INTEGRATION TEST
/* c8 ignore start */
export const registerJSModule = (interpreter, name, value) => {
if (name === 'polyscript') {
value.lazy_py_modules = async (...packages) => {
await loader.get(interpreter)(packages);
return packages.map(name => interpreter.pyimport(name));
};
}
interpreter.registerJsModule(name, value);
};

Expand Down
16 changes: 10 additions & 6 deletions esm/interpreter/micropython.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import fetch from '@webreflection/fetch';
import { fetchFiles, fetchJSModules, fetchPaths, writeFile } from './_utils.js';
import { getFormat, registerJSModule, run, runAsync, runEvent } from './_python.js';
import { getFormat, loader, registerJSModule, run, runAsync, runEvent } from './_python.js';
import { stdio, buffered } from './_io.js';
import mip from '../python/mip.js';
import zip from '../zip.js';
Expand All @@ -20,17 +20,15 @@ export default {
});
url = url.replace(/\.m?js$/, '.wasm');
const interpreter = await get(loadMicroPython({ linebuffer: false, stderr, stdout, url }));
const py_imports = importPackages.bind(interpreter);
loader.set(interpreter, py_imports);
if (config.files) await fetchFiles(this, interpreter, config.files);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
if (config.js_modules) await fetchJSModules(config.js_modules);

// Install Micropython Package
this.writeFile(interpreter, './mip.py', mip);
if (config.packages){
const mpyPackageManager = interpreter.pyimport('mip');
for (const mpyPackage of config.packages)
mpyPackageManager.install(mpyPackage);
}
if (config.packages) await py_imports(config.packages);
return interpreter;
},
registerJSModule,
Expand Down Expand Up @@ -92,4 +90,10 @@ export default {
return writeFile(fs, path, buffer);
},
};

async function importPackages(packages) {
const mpyPackageManager = this.pyimport('mip');
for (const mpyPackage of packages)
mpyPackageManager.install(mpyPackage);
}
/* c8 ignore stop */
19 changes: 12 additions & 7 deletions esm/interpreter/pyodide.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { create } from 'gc-hook';

import { RUNNING_IN_WORKER, fetchFiles, fetchJSModules, fetchPaths, writeFile } from './_utils.js';
import { getFormat, registerJSModule, run, runAsync, runEvent } from './_python.js';
import { getFormat, loader, registerJSModule, run, runAsync, runEvent } from './_python.js';
import { stdio } from './_io.js';

const type = 'pyodide';
Expand Down Expand Up @@ -89,15 +89,12 @@ export default {
const interpreter = await get(
loadPyodide({ stderr, stdout, indexURL }),
);
const py_imports = importPackages.bind(interpreter);
loader.set(interpreter, py_imports);
if (config.files) await fetchFiles(this, interpreter, config.files);
if (config.fetch) await fetchPaths(this, interpreter, config.fetch);
if (config.js_modules) await fetchJSModules(config.js_modules);
if (config.packages) {
await interpreter.loadPackage('micropip');
const micropip = await interpreter.pyimport('micropip');
await micropip.install(config.packages, { keep_going: true });
micropip.destroy();
}
if (config.packages) await py_imports(config.packages);
return interpreter;
},
registerJSModule,
Expand All @@ -120,4 +117,12 @@ export default {
return writeFile({ FS, PATH, PATH_FS }, path, buffer);
},
};

// exposed utility to import packages via polyscript.lazy_py_modules
async function importPackages(packages) {
await this.loadPackage('micropip');
const micropip = this.pyimport('micropip');
await micropip.install(packages, { keep_going: true });
micropip.destroy();
}
/* c8 ignore stop */
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@
"to-json-callback": "^0.1.1"
},
"worker": {
"blob": "sha256-NuQY85dugu2Ks8ubOmDiR+aw7eOW0CQvTfRQIknWkPk="
"blob": "sha256-8x1Yej68/ZpJysyfYgjrdqQ+hQmhGh7Pol8qLUUi/g4="
}
}
19 changes: 19 additions & 0 deletions test/py_modules.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>python modules</title>
<link rel="stylesheet" href="style.css">
<script type="module" src="../dist/index.js"></script>
</head>
<body>
<script type="pyodide" async>
from polyscript import lazy_py_modules

matplotlib, regex, = await lazy_py_modules("matplotlib", "regex")

print(matplotlib, regex)
</script>
</body>
</html>