Skip to content

Commit b406c86

Browse files
committed
optional playwright dependency (for conda)
1 parent 3444352 commit b406c86

File tree

7 files changed

+70
-20
lines changed

7 files changed

+70
-20
lines changed

pyjs_code_runner/backend/backend.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from enum import Enum
2-
3-
from .node.node import NodeBackend
4-
from .browser_main.browser_main import BrowserMainBackend
5-
from .browser_worker.browser_worker import BrowserWorkerBackend
2+
import textwrap
63

74

85
class BackendFamilyType(str, Enum):
@@ -23,10 +20,53 @@ def get_backend_type_family(backend):
2320
return BackendFamilyType.browser
2421

2522

23+
def ensure_playwright_imports():
24+
try:
25+
import playwright
26+
except ModuleNotFoundError as e:
27+
msg = """\n
28+
pyks-code-runner error:
29+
30+
* Cannot import playwright!
31+
32+
to use the browser-{worker/main} backends
33+
playwright needs to be installed.
34+
35+
Install playwight with:
36+
37+
* conda:
38+
39+
conda install -c microsoft playwright
40+
41+
* mamba:
42+
43+
mamba install -c microsoft playwright
44+
45+
* micromamba:
46+
47+
micromamb install -c microsoft playwright
48+
49+
* pip:
50+
51+
python -m pip install playwright
52+
53+
"""
54+
raise ModuleNotFoundError(textwrap.dedent(msg))
55+
56+
2657
def get_backend_cls(backend_type):
2758
if backend_type == BackendType.node:
59+
from .node.node import NodeBackend
60+
2861
return NodeBackend
62+
2963
elif backend_type == BackendType.browser_main:
64+
ensure_playwright_imports()
65+
from .browser_main.browser_main import BrowserMainBackend
66+
3067
return BrowserMainBackend
3168
elif backend_type == BackendType.browser_worker:
69+
ensure_playwright_imports()
70+
from .browser_worker.browser_worker import BrowserWorkerBackend
71+
3272
return BrowserWorkerBackend

pyjs_code_runner/backend/browser_main/browser_main.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ def __init__(
2828
self.slow_mo = slow_mo
2929

3030
def run(self):
31-
print(
32-
"run in browser-main",
33-
self.script,
34-
"port",
35-
self.port,
36-
"headless",
37-
self.headless,
38-
)
3931

4032
browser_main_html = "browser_main.html"
4133
main = HTML_DIR / browser_main_html

pyjs_code_runner/backend/node/node.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66

77
from ..backend_base import BackendBase
8+
from subprocess import Popen, PIPE, STDOUT
89

910
THIS_DIR = os.path.dirname(os.path.realpath(__file__))
1011

@@ -21,8 +22,6 @@ def __init__(self, host_work_dir, work_dir, script, async_main, node_exe):
2122
self.node_exe = node_exe
2223

2324
def run(self):
24-
print("run in node", self.script, self.node_exe, "CWD", os.getcwd())
25-
2625
main_name = "node_main.js"
2726
main = Path(THIS_DIR) / main_name
2827
shutil.copyfile(main, self.host_work_dir / main_name)
@@ -36,6 +35,26 @@ def run(self):
3635
str(int(self.async_main)),
3736
]
3837

39-
returncode = subprocess.run(cmd, cwd=os.getcwd()).returncode
40-
if returncode != 0:
41-
sys.exit(returncode)
38+
if False:
39+
ret = subprocess.run(cmd, cwd=os.getcwd(), stdout=PIPE)
40+
returncode = ret.returncode
41+
output = ret.stdout.decode()
42+
print(output)
43+
if returncode != 0:
44+
sys.exit(returncode)
45+
46+
else:
47+
print("START")
48+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
49+
50+
# Poll process.stdout to show stdout live
51+
while True:
52+
output = process.stdout.readline()
53+
if process.poll() is not None:
54+
break
55+
if output:
56+
print(output.decode().strip())
57+
rc = process.poll()
58+
# print("RC", rc)
59+
if process.returncode != 0:
60+
sys.exit(process.returncode)

pyjs_code_runner/backend/node/node_main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async_main = parseInt(process.argv[4]);
2727
}
2828
catch(e){
2929
console.error("error while evaluating main file:",e)
30-
return 1;
30+
process.exit(1);
3131
}
3232
if(async_main)
3333
{

pyjs_code_runner/js/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
console.log("THE UTILS")
21
async function make_pyjs(print, error) {
32
var pyjs = await createModule({print:print,error:print})
43
var EmscriptenForgeModule = pyjs

pyjs_code_runner/js/worker.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
console.log("in woker.js");
21
importScripts('./pyjs_runtime_browser.js')

pyjs_code_runner/run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def pack_env(conda_env, backend_type, pkg_file_filter, cache_dir, outdir):
108108
outname=outname,
109109
export_name=export_name,
110110
pkg_file_filter=pkg_file_filter,
111+
silent=True,
111112
)
112113

113114
# copy from cache to work dir

0 commit comments

Comments
 (0)