Refactor the pyscript python package. (#1713)

This commit is contained in:
Antonio Cuni
2023-09-15 11:57:06 +00:00
committed by GitHub
parent 840bc803b7
commit 5191c45113
10 changed files with 54 additions and 73 deletions

View File

@@ -82,7 +82,7 @@ const registerModule = ({ XWorker: $XWorker, interpreter, io }) => {
}
// enrich the Python env with some JS utility for main
interpreter.registerJsModule("_pyscript_js", {
interpreter.registerJsModule("_pyscript", {
PyWorker,
get target() {
return isScript(currentElement)

View File

@@ -1,12 +0,0 @@
import js as window
IS_WORKER = not hasattr(window, "document")
if IS_WORKER:
from polyscript import xworker as _xworker
window = _xworker.window
document = window.document
sync = _xworker.sync
else:
document = window.document

File diff suppressed because one or more lines are too long

View File

@@ -1,34 +0,0 @@
# export only what we want to expose as `pyscript` module
# but not what is WORKER/MAIN dependent
from _pyscript import window, document, IS_WORKER
from _pyscript.display import HTML, display as _display
from _pyscript.event_handling import when
# this part is needed to disambiguate between MAIN and WORKER
if IS_WORKER:
# in workers the display does not have a default ID
# but there is a sync utility from xworker
import polyscript as _polyscript
from _pyscript import sync
def current_target():
return _polyscript.target
else:
# in MAIN both PyWorker and current element target exist
# so these are both exposed and the display will use,
# if not specified otherwise, such current element target
import _pyscript_js
PyWorker = _pyscript_js.PyWorker
def current_target():
return _pyscript_js.target
# the display provides a handy default target either in MAIN or WORKER
def display(*values, target=None, append=True):
if target is None:
target = current_target()
return _display(*values, target=target, append=append)

View File

@@ -3,7 +3,7 @@ import html
import io
import re
from . import document, window
from pyscript.magic_js import document, window, current_target
_MIME_METHODS = {
"__repr__": "text/plain",
@@ -146,6 +146,9 @@ def _write(element, value, append=False):
def display(*values, target=None, append=True):
if target is None:
target = current_target()
element = document.getElementById(target)
for v in values:
_write(element, v, append=append)

View File

@@ -1,7 +1,7 @@
import inspect
from pyodide.ffi.wrappers import add_event_listener
from pyscript import document
from pyscript.magic_js import document
def when(event_type=None, selector=None):

View File

@@ -0,0 +1,33 @@
import js as globalThis
RUNNING_IN_WORKER = not hasattr(globalThis, "document")
if RUNNING_IN_WORKER:
import polyscript
# XXX we should use a "smarter" object which emits a clearer error message
# if you try to access it
PyWorker = None
window = polyscript.xworker.window
document = window.document
sync = polyscript.xworker.sync
# in workers the display does not have a default ID
# but there is a sync utility from xworker
def current_target():
return polyscript.target
else:
import _pyscript
from _pyscript import PyWorker
window = globalThis
document = globalThis.document
# XXX we should use a "smarter" object which emits a clearer error message
# if you try to access it
sync = None
# in MAIN the current element target exist, just use it
def current_target():
return _pyscript.target