Skip to content

Commit

Permalink
Fix #1899 - Expose pyscript.js_modules as module (#1902)
Browse files Browse the repository at this point in the history
* Fix #1899 - Expose pyscript.js_modules as module

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix #1899 - Make import as smooth as in polyscript

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
WebReflection and pre-commit-ci[bot] committed Jan 3, 2024
1 parent 355866a commit 0f788fa
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 75 deletions.
154 changes: 84 additions & 70 deletions pyscript.core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions pyscript.core/package.json
@@ -1,6 +1,6 @@
{
"name": "@pyscript/core",
"version": "0.3.10",
"version": "0.3.14",
"type": "module",
"description": "PyScript",
"module": "./index.js",
Expand Down Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"@ungap/with-resolvers": "^0.1.0",
"basic-devtools": "^0.1.6",
"polyscript": "^0.6.2",
"polyscript": "^0.6.8",
"sticky-module": "^0.1.1",
"to-json-callback": "^0.1.1",
"type-checked-collections": "^0.1.7"
Expand All @@ -52,7 +52,7 @@
"@codemirror/lang-python": "^6.1.3",
"@codemirror/language": "^6.9.3",
"@codemirror/state": "^6.3.3",
"@codemirror/view": "^6.22.1",
"@codemirror/view": "^6.22.3",
"@playwright/test": "^1.40.1",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
Expand All @@ -61,8 +61,8 @@
"@xterm/addon-fit": "^0.9.0-beta.1",
"chokidar": "^3.5.3",
"codemirror": "^6.0.1",
"eslint": "^8.55.0",
"rollup": "^4.6.1",
"eslint": "^8.56.0",
"rollup": "^4.9.1",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-string": "^3.0.0",
"static-handler": "^0.4.3",
Expand Down
19 changes: 19 additions & 0 deletions pyscript.core/src/stdlib/pyscript/magic_js.py
@@ -1,9 +1,28 @@
import sys

import js as globalThis
from polyscript import js_modules
from pyscript.util import NotSupported

RUNNING_IN_WORKER = not hasattr(globalThis, "document")


# allow `from pyscript.js_modules.xxx import yyy`
class JSModule(object):
def __init__(self, name):
self.name = name

def __getattr__(self, field):
# avoid pyodide looking for non existent fields
if not field.startswith("_"):
return getattr(getattr(js_modules, self.name), field)


# generate N modules in the system that will proxy the real value
for name in globalThis.Reflect.ownKeys(js_modules):
sys.modules[f"pyscript.js_modules.{name}"] = JSModule(name)
sys.modules["pyscript.js_modules"] = js_modules

if RUNNING_IN_WORKER:
import js
import polyscript
Expand Down

0 comments on commit 0f788fa

Please sign in to comment.