Skip to content

Commit 0f788fa

Browse files
Fix pyscript#1899 - Expose pyscript.js_modules as module (pyscript#1902)
* Fix pyscript#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 pyscript#1899 - Make import as smooth as in polyscript --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 355866a commit 0f788fa

File tree

6 files changed

+164
-75
lines changed

6 files changed

+164
-75
lines changed

pyscript.core/package-lock.json

Lines changed: 84 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyscript.core/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pyscript/core",
3-
"version": "0.3.10",
3+
"version": "0.3.14",
44
"type": "module",
55
"description": "PyScript",
66
"module": "./index.js",
@@ -42,7 +42,7 @@
4242
"dependencies": {
4343
"@ungap/with-resolvers": "^0.1.0",
4444
"basic-devtools": "^0.1.6",
45-
"polyscript": "^0.6.2",
45+
"polyscript": "^0.6.8",
4646
"sticky-module": "^0.1.1",
4747
"to-json-callback": "^0.1.1",
4848
"type-checked-collections": "^0.1.7"
@@ -52,7 +52,7 @@
5252
"@codemirror/lang-python": "^6.1.3",
5353
"@codemirror/language": "^6.9.3",
5454
"@codemirror/state": "^6.3.3",
55-
"@codemirror/view": "^6.22.1",
55+
"@codemirror/view": "^6.22.3",
5656
"@playwright/test": "^1.40.1",
5757
"@rollup/plugin-commonjs": "^25.0.7",
5858
"@rollup/plugin-node-resolve": "^15.2.3",
@@ -61,8 +61,8 @@
6161
"@xterm/addon-fit": "^0.9.0-beta.1",
6262
"chokidar": "^3.5.3",
6363
"codemirror": "^6.0.1",
64-
"eslint": "^8.55.0",
65-
"rollup": "^4.6.1",
64+
"eslint": "^8.56.0",
65+
"rollup": "^4.9.1",
6666
"rollup-plugin-postcss": "^4.0.2",
6767
"rollup-plugin-string": "^3.0.0",
6868
"static-handler": "^0.4.3",

pyscript.core/src/stdlib/pyscript/magic_js.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
import sys
2+
13
import js as globalThis
24
from polyscript import js_modules
35
from pyscript.util import NotSupported
46

57
RUNNING_IN_WORKER = not hasattr(globalThis, "document")
68

9+
10+
# allow `from pyscript.js_modules.xxx import yyy`
11+
class JSModule(object):
12+
def __init__(self, name):
13+
self.name = name
14+
15+
def __getattr__(self, field):
16+
# avoid pyodide looking for non existent fields
17+
if not field.startswith("_"):
18+
return getattr(getattr(js_modules, self.name), field)
19+
20+
21+
# generate N modules in the system that will proxy the real value
22+
for name in globalThis.Reflect.ownKeys(js_modules):
23+
sys.modules[f"pyscript.js_modules.{name}"] = JSModule(name)
24+
sys.modules["pyscript.js_modules"] = js_modules
25+
726
if RUNNING_IN_WORKER:
827
import js
928
import polyscript

0 commit comments

Comments
 (0)