Skip to content

Commit 6777d2c

Browse files
authored
Fixes (#3)
* fixes
1 parent ec3d30d commit 6777d2c

File tree

7 files changed

+49
-16
lines changed

7 files changed

+49
-16
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
A driver to run python code in a wasm environment, almost like running vanilla python code.
55

6+
## Motivation
7+
8+
Debugging, experimenting and testing python code from a dedicated conda environment
9+
in browser environment is a complex process with a lot of (complicated) steps.
10+
11+
* create the environment for emscripten
12+
* pack the environemtn
13+
14+
615

716
## Examples
817

pyjs_code_runner/backend/browser_main/browser_main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def run(self):
3737
server,
3838
url,
3939
):
40-
4140
page_url = f"{url}/{browser_main_html}"
4241
ret = asyncio.run(self.playwright_run_in_main_thread(page_url=page_url))
4342
if ret != 0:

pyjs_code_runner/cli/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import typer
22

3-
app = typer.Typer()
3+
app = typer.Typer(pretty_exceptions_show_locals=False)

pyjs_code_runner/cli/run.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def parse_mounts(mounts):
9494
help="""cache-directory, if env-cache-dir is not specified, the an `appdir` is used""",
9595
)
9696

97+
cache_option = typer.Option(
98+
True,
99+
help="""use cache""",
100+
)
97101

98102
port_option = typer.Option(
99103
None,
@@ -124,6 +128,7 @@ def browser_main(
124128
pyjs_dir: Optional[Path] = pyjs_dir_option,
125129
host_work_dir: Optional[Path] = host_work_dir_option,
126130
cache_dir: Optional[Path] = cache_dir_option,
131+
cache: bool = cache_option,
127132
port: Optional[int] = port_option,
128133
headless: bool = headless_option,
129134
slow_mo: Optional[int] = slowmo_option,
@@ -138,6 +143,7 @@ def browser_main(
138143
pkg_file_filter=pkg_file_filter,
139144
pyjs_dir=pyjs_dir,
140145
cache_dir=cache_dir,
146+
use_cache=cache,
141147
host_work_dir=host_work_dir,
142148
backend_kwargs=dict(port=port, headless=headless, slow_mo=slow_mo),
143149
)
@@ -154,6 +160,7 @@ def browser_worker(
154160
pyjs_dir: Optional[Path] = pyjs_dir_option,
155161
host_work_dir: Optional[Path] = host_work_dir_option,
156162
cache_dir: Optional[Path] = cache_dir_option,
163+
cache: bool = cache_option,
157164
port: Optional[int] = port_option,
158165
headless: bool = headless_option,
159166
slow_mo: Optional[int] = slowmo_option,
@@ -168,6 +175,7 @@ def browser_worker(
168175
pkg_file_filter=pkg_file_filter,
169176
pyjs_dir=pyjs_dir,
170177
cache_dir=cache_dir,
178+
use_cache=cache,
171179
host_work_dir=host_work_dir,
172180
backend_kwargs=dict(port=port, headless=headless, slow_mo=slow_mo),
173181
)
@@ -191,6 +199,7 @@ def node(
191199
pyjs_dir: Optional[Path] = pyjs_dir_option,
192200
host_work_dir: Optional[Path] = host_work_dir_option,
193201
cache_dir: Optional[Path] = cache_dir_option,
202+
cache: bool = cache_option,
194203
node_binary: Optional[Path] = node_binary_option,
195204
):
196205
run_script(
@@ -203,6 +212,7 @@ def node(
203212
pkg_file_filter=pkg_file_filter,
204213
pyjs_dir=pyjs_dir,
205214
cache_dir=cache_dir,
215+
use_cache=cache,
206216
host_work_dir=host_work_dir,
207217
backend_kwargs=dict(node_binary=node_binary),
208218
)
@@ -219,13 +229,17 @@ def run_script(
219229
pyjs_dir,
220230
host_work_dir,
221231
cache_dir,
232+
use_cache,
222233
backend_kwargs=None,
223234
):
224235

225236
if backend_kwargs is None:
226237
backend_kwargs = dict()
227238
mounts = parse_mounts(mounts)
228-
cache_dir = get_cache_dir(cache_dir=cache_dir)
239+
if use_cache:
240+
cache_dir = get_cache_dir(cache_dir=cache_dir)
241+
else:
242+
cache_dir = None
229243
pkg_file_filter = get_file_filter(pkg_file_filter, cache_dir=cache_dir)
230244

231245
run(
@@ -238,6 +252,7 @@ def run_script(
238252
pkg_file_filter=pkg_file_filter,
239253
pyjs_dir=pyjs_dir,
240254
cache_dir=cache_dir,
255+
use_cache=use_cache,
241256
host_work_dir=host_work_dir,
242257
backend_kwargs=backend_kwargs,
243258
)

pyjs_code_runner/get_file_filter.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
def get_file_filter(pkg_file_filter, cache_dir):
99
if pkg_file_filter is None or len(pkg_file_filter) == 0:
1010

11-
path_in_cache = Path(cache_dir) / "empack_config.yaml"
12-
if not path_in_cache.exists():
11+
# empack now supports passing None
12+
return None
13+
# path_in_cache = Path(cache_dir) / "empack_config.yaml"
14+
# if not path_in_cache.exists():
1315

14-
urllib.request.urlretrieve(EMPACK_FILE_FILTER_URL, path_in_cache)
15-
return pkg_file_filter_from_yaml(path_in_cache)
16+
# urllib.request.urlretrieve(EMPACK_FILE_FILTER_URL, path_in_cache)
17+
# return pkg_file_filter_from_yaml(path_in_cache)
1618

1719
else:
1820
print(pkg_file_filter)

pyjs_code_runner/js/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ globalThis.make_pyjs = make_pyjs
2121

2222
function eval_main_script(pyjs, workdir, filename) {
2323
try{
24-
pyjs.exec("import os")
24+
pyjs.exec("import os;from os.path import exists")
2525
pyjs.exec(`os.chdir("${workdir}")`)
2626
pyjs.eval_file(filename);
2727
return 0;

pyjs_code_runner/run.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from .constants import EXPORT_NAME_SUFFIX
33
from .js_global_object import js_global_object
44
from .work_dir_context import work_dir_context
5+
from .get_cache_dir import get_cache_dir
6+
57
import tempfile
68
from pathlib import Path
79
from empack.file_packager import pack_directory, pack_file, split_pack_environment
@@ -88,19 +90,18 @@ def copy_pyjs(conda_env, backend_type, pyjs_dir, outdir):
8890
shutil.copyfile(source_dir / file, outdir / file)
8991

9092

91-
def pack_env(conda_env, backend_type, pkg_file_filter, cache_dir, outdir):
93+
def pack_env(conda_env, backend_type, pkg_file_filter, cache_dir, use_cache, outdir):
9294

9395
outname = "packed_env"
9496
cache_folder_name = Path(
9597
conda_env_to_cache_name(conda_env, backend_type=backend_type)
9698
)
9799
folder_for_env = cache_dir / cache_folder_name
98100
file_to_probe = folder_for_env / f"{outname}.js"
99-
if not file_to_probe.exists():
101+
if (not file_to_probe.exists()) or (not use_cache):
100102
folder_for_env.mkdir(parents=True, exist_ok=True)
101103

102104
export_name = f"{js_global_object(backend_type)}.{EXPORT_NAME_SUFFIX}"
103-
104105
with work_dir_context(folder_for_env):
105106
split_pack_environment(
106107
env_prefix=conda_env,
@@ -132,12 +133,18 @@ def run(
132133
async_main,
133134
mounts,
134135
work_dir,
135-
pkg_file_filter,
136-
pyjs_dir,
137-
cache_dir,
138-
host_work_dir,
139-
backend_kwargs,
136+
pkg_file_filter=None,
137+
pyjs_dir=None,
138+
cache_dir=None,
139+
use_cache=False,
140+
host_work_dir=None,
141+
backend_kwargs=None,
140142
):
143+
if backend_kwargs is None:
144+
backend_kwargs = dict()
145+
146+
if cache_dir is None:
147+
cache_dir = get_cache_dir(cache_dir=None)
141148

142149
# create a temporary host work directory
143150
with host_work_dir_context(host_work_dir) as host_work_dir:
@@ -156,6 +163,7 @@ def run(
156163
outdir=host_work_dir,
157164
pkg_file_filter=pkg_file_filter,
158165
cache_dir=cache_dir,
166+
use_cache=use_cache,
159167
)
160168

161169
# pack all the mounts

0 commit comments

Comments
 (0)