Skip to content

Commit

Permalink
feat: auto-installs also have toasts now
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Jul 2, 2024
1 parent 5e4f0c6 commit 3dd1672
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/python/patches/imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from functools import cache
from sys import modules

from micropip._compat import REPODATA_PACKAGES
from pyodide.code import find_imports


@cache
def build_reversed_index() -> dict[str, str]:
return {import_name: package_name for package_name, info in REPODATA_PACKAGES.items() for import_name in info["imports"]}


def import_name_to_package_name(import_name: str):
if import_name not in modules:
return build_reversed_index().get(import_name)


def find_packages_to_install(source: str):
return list(filter(None, map(import_name_to_package_name, find_imports(source))))
15 changes: 13 additions & 2 deletions src/python/patches/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ def patch_linecache():

@cache
def patch_console():
from pyodide.console import PyodideConsole
from pyodide.console import Console, PyodideConsole

PyodideConsole.runcode = with_lock(PyodideConsole.runcode)
@with_lock
@wraps(PyodideConsole.runcode)
async def runcode(self: PyodideConsole, source: str, code):
from .imports import find_packages_to_install

packages = find_packages_to_install(source)
if packages:
await micropip.install(packages)

return await Console.runcode(self, source, code)

PyodideConsole.runcode = runcode


@cache
Expand Down
1 change: 1 addition & 0 deletions src/python/patches/pyodide_js.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
async def loadPackagesFromImports(source: str): ... # noqa: N802

0 comments on commit 3dd1672

Please sign in to comment.