Skip to content

Commit

Permalink
DOCS: Clarify registerJsModule and submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Dec 26, 2023
1 parent 06bbb38 commit e389f14
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/js/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,28 @@ export class PyodideAPI {
* module from :py:data:`sys.modules`. This calls
* :func:`~pyodide.ffi.register_js_module`.
*
* Any attributes of the JavaScript objects which are themselves objects will
* be treated as submodules:
* ```pyodide
* pyodide.registerJsModule("mymodule", { submodule: { value: 7 } });
* pyodide.runPython(`
* from mymodule.submodule import value
* assert value == 7
* `);
* ```
* If you wish to prevent this, try the following instead:
* ```pyodide
* const sys = pyodide.pyimport("sys");
* sys.modules.set("mymodule", { obj: { value: 7 } });
* pyodide.runPython(`
* from mymodule import obj
* assert obj.value == 7
* # attempting to treat obj as a submodule raises ModuleNotFoundError:
* # "No module named 'mymodule.obj'; 'mymodule' is not a package"
* from mymodule.obj import value
* `);
* ```
*
* @param name Name of the JavaScript module to add
* @param module JavaScript object backing the module
*/
Expand Down

0 comments on commit e389f14

Please sign in to comment.