-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-install the imported packages (#902)
* Add autoInstall option to the kernel * Run pyodide.loadPackagesFromImports() at init and file writing * Send the auto-installed packages to the editor and update the requirements tab content * Update the auto install event emits a promise to resolve the installed package list * Update to call loadPackage() only when needed * Improve the toast notification * Fix * Fix * Update the editor content * Fix * Implement module auto-load at each run and split the autoInstall option into moduleAutoLoadOnRun and moduleAutoLoadOnSave * Revert "Implement module auto-load at each run and split the autoInstall option into moduleAutoLoadOnRun and moduleAutoLoadOnSave" This reverts commit 61cfd1c. * Rename auto-install to module-auto-load * Update findImports impl * Fix to await the auto-load promise in the script runner * Fix * Rename messages * Fix * Apply formatter * Add comment * Refactoring * Refactoring * Fix inter-window messaging * Fix * Fix * Fix <Editor /> to handle addRequirements() in an imperative way
- Loading branch information
Showing
14 changed files
with
607 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { PackageData, PyodideInterface } from "pyodide"; | ||
import type { ModuleAutoLoadMessage } from "./types"; | ||
import type { PostMessageFn } from "./worker-runtime"; | ||
|
||
function findImports(pyodide: PyodideInterface, source: string): string[] { | ||
return pyodide.pyodide_py.ffi._pyodide._base | ||
.find_imports(source) | ||
.toJs() as string[]; | ||
} | ||
|
||
export async function tryModuleAutoLoad( | ||
pyodide: PyodideInterface, | ||
postMessage: PostMessageFn, | ||
sources: string[] | ||
): Promise<void> { | ||
// Ref: `pyodide.loadPackagesFromImports` (https://github.com/pyodide/pyodide/blob/0.26.0/src/js/api.ts#L191) | ||
|
||
const importsArr = sources.map((source) => findImports(pyodide, source)); | ||
const imports = Array.from(new Set(importsArr.flat())); | ||
|
||
const notFoundImports = imports.filter( | ||
(name) => | ||
!pyodide.runPython(`__import__('importlib').util.find_spec('${name}')`) | ||
); | ||
|
||
const packagesToLoad = notFoundImports | ||
.map((name) => | ||
( | ||
pyodide as unknown as { | ||
_api: { _import_name_to_package_name: Map<string, string> }; | ||
} | ||
)._api._import_name_to_package_name.get(name) | ||
) | ||
.filter((name) => name) as string[]; | ||
|
||
if (packagesToLoad.length === 0) { | ||
return; | ||
} | ||
|
||
const channel = new MessageChannel(); | ||
|
||
postMessage( | ||
{ | ||
type: "event:moduleAutoLoad", | ||
data: { | ||
packagesToLoad, | ||
}, | ||
}, | ||
channel.port2 | ||
); | ||
|
||
try { | ||
const loadedPackages = await pyodide.loadPackage(packagesToLoad); | ||
|
||
channel.port1.postMessage({ | ||
type: "moduleAutoLoad:success", | ||
data: { | ||
loadedPackages, | ||
}, | ||
} as ModuleAutoLoadMessage); | ||
channel.port1.close(); | ||
return; | ||
} catch (error) { | ||
channel.port1.postMessage({ | ||
type: "moduleAutoLoad:error", | ||
error: error as Error, | ||
} as ModuleAutoLoadMessage); | ||
channel.port1.close(); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.