Skip to content

Commit f8e0cf8

Browse files
committed
various minor fixes + client python refactor
1 parent 472dbca commit f8e0cf8

File tree

10 files changed

+103
-91
lines changed

10 files changed

+103
-91
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
.jupyter
1212

1313
# --- Python ---
14+
.venv
15+
venv
1416
MANIFEST
1517
build
1618
dist

examples/chart.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

requirements/dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
jupyter
1+
jupyterlab
22
pre-commit
33
notebook
44
twine

src/idom/client/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
web_modules
3+
etc_modules

src/idom/client/__init__.py

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,33 @@
99

1010
CLIENT_DIR = Path(__file__).parent
1111

12+
CORE_MODULES = CLIENT_DIR / "core_modules"
13+
NODE_MODULES = CLIENT_DIR / "node_modules"
14+
WEB_MODULES = CLIENT_DIR / "web_modules"
15+
ETC_MODULES = CLIENT_DIR / "etc_modules"
1216

13-
def import_path(name: str) -> Optional[str]:
14-
path = CLIENT_DIR / "web_modules"
15-
for name_part in name.split("/"):
16-
if not path.is_dir():
17-
return None
18-
path /= name_part
19-
full_path = path.with_suffix(".js")
20-
if not full_path.is_file():
21-
return None
22-
return _web_module(name)
17+
18+
def import_path(prefix: str, name: str) -> Optional[str]:
19+
if not module_exists(prefix, name):
20+
raise ValueError(f"Module '{_module_js_path(prefix, name)}' does not exist.")
21+
return _module_js_path(prefix, name)
2322

2423

2524
def define_module(name: str, source: str) -> str:
26-
path = CLIENT_DIR
27-
for n in ["etc_modules"] + name.split("/"):
28-
if not path.exists():
29-
path.mkdir()
30-
path /= n
31-
module = path.with_suffix(".js")
32-
with module.open("w+") as f:
25+
path = _create_module_os_path(ETC_MODULES, name)
26+
with path.open("w+") as f:
3327
f.write(source)
34-
return _etc_module(name)
28+
return _module_js_path("etc_modules", name)
29+
30+
31+
def delete_module(prefix: str, name: str) -> None:
32+
if not module_exists(prefix, name):
33+
raise ValueError(f"Module '{_module_js_path(prefix, name)}' does not exist.")
34+
return None
35+
36+
37+
def module_exists(prefix: Union[str, Path], name: str) -> bool:
38+
return _find_module_os_path(prefix, name) is not None
3539

3640

3741
def install(*dependencies: str) -> None:
@@ -59,12 +63,7 @@ def install(*dependencies: str) -> None:
5963

6064

6165
def restore() -> None:
62-
for path in ["web_modules", "node_modules"]:
63-
full_path = CLIENT_DIR.joinpath(*path.split("/"))
64-
if full_path.is_file():
65-
full_path.unlink()
66-
elif full_path.is_dir():
67-
shutil.rmtree(full_path)
66+
_delete_os_paths(WEB_MODULES, NODE_MODULES, ETC_MODULES)
6867
install()
6968

7069

@@ -78,8 +77,8 @@ def _package_json():
7877
"devDependencies": {"snowpack": "^1.6.0"},
7978
"snowpack": {
8079
"installOptions": {
81-
"dest": str(CLIENT_DIR / "web_modules"),
82-
"include": str(CLIENT_DIR / "modules" / "**" / "*.js"),
80+
"dest": str(WEB_MODULES),
81+
"include": str(CORE_MODULES / "**" / "*.js"),
8382
},
8483
"webDependencies": [],
8584
},
@@ -96,9 +95,40 @@ def _run_subprocess(args: List[str], cwd: Union[str, Path]):
9695
raise
9796

9897

99-
def _web_module(name: str) -> str:
100-
return f"../web_modules/{name}.js"
98+
def _module_js_path(prefix: str, name: str) -> str:
99+
return f"../{prefix}/{name}.js"
100+
101+
102+
def _find_module_os_path(prefix: Union[str, Path], name: str) -> Optional[Path]:
103+
if isinstance(prefix, str):
104+
path = CLIENT_DIR / prefix
105+
else:
106+
path = prefix
107+
for name_part in name.split("/"):
108+
if not path.is_dir():
109+
return None
110+
path /= name_part
111+
full_path = path.with_suffix(".js")
112+
if not full_path.is_file():
113+
return None
114+
return full_path
115+
116+
117+
def _create_module_os_path(prefix: Union[str, Path], name: str) -> Path:
118+
if isinstance(prefix, str):
119+
path = CLIENT_DIR / prefix
120+
else:
121+
path = prefix
122+
for n in name.split("/"):
123+
if not path.exists():
124+
path.mkdir()
125+
path /= n
126+
return path.with_suffix(".js")
101127

102128

103-
def _etc_module(name: str) -> str:
104-
return f"../etc_modules/{name}.js"
129+
def _delete_os_paths(*paths: Path) -> None:
130+
for p in paths:
131+
if p.is_file():
132+
p.unlink()
133+
elif p.is_dir():
134+
shutil.rmtree(p)

src/idom/client/core_modules/lazy-component.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ function lazyComponent(model) {
1212
return { default: cmpt };
1313
},
1414
(error) => {
15-
function Catch() {
16-
return html`
17-
<pre>
18-
<code>${error.stack}</code>
19-
</pre
20-
>
21-
`;
15+
if (!error.stack) {
16+
throw error;
17+
} else {
18+
return {
19+
default: function Catch() {
20+
return html`
21+
<pre>
22+
<code>${error.stack}</code>
23+
</pre
24+
>
25+
`;
26+
}
27+
};
2228
}
23-
return { default: Catch };
2429
}
2530
);
2631
});

src/idom/client/etc_modules/chart.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import htm from "../web_modules/htm.js";
55
const html = htm.bind(React.createElement);
66

77
const data = [
8-
{quarter: 1, earnings: 13000},
9-
{quarter: 2, earnings: 16500},
10-
{quarter: 3, earnings: 14250},
11-
{quarter: 4, earnings: 19000}
8+
{ quarter: 1, earnings: 13000 },
9+
{ quarter: 2, earnings: 16500 },
10+
{ quarter: 3, earnings: 14250 },
11+
{ quarter: 4, earnings: 19000 }
1212
];
1313

14-
class Chart extends React.Component {
15-
render() {
16-
return html`
14+
function Chart() {
15+
return html`
1716
<${VictoryChart} domainPadding=20>
1817
<${VictoryAxis}
1918
tickValues=${[1, 2, 3, 4]}
@@ -30,7 +29,6 @@ class Chart extends React.Component {
3029
/>
3130
</${VictoryChart}>
3231
`
33-
}
3432
}
3533

36-
export default {Chart: Chart}
34+
export default { Chart: Chart }

src/idom/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"snowpack": {
2424
"installOptions": {
25-
"include": "modules/**/*.js",
25+
"include": "core_modules/**/*.js",
2626
"clean": true
2727
}
2828
}

src/idom/widgets/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
from .common import Import, hotswap
1+
from .common import Import, import_module, hotswap
22
from .display import display
33
from .inputs import Input
44
from .images import Image
55
from .html import html
66

7-
__all__ = ["display", "node", "Image", "hotswap", "html", "Input", "Import"]
7+
__all__ = [
8+
"display",
9+
"node",
10+
"Image",
11+
"import_module",
12+
"hotswap",
13+
"html",
14+
"Input",
15+
"Import",
16+
]

src/idom/widgets/common.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import inspect
12
from typing import Any, Callable, Tuple, Optional, Union
23

34
from idom import client
@@ -6,10 +7,12 @@
67
from idom.tools import Var
78

89

9-
def module(name: str, source: Any, raw: bool = False) -> "Import":
10+
def import_module(name: str, source: Any, raw: bool = False) -> "Import":
1011
if not raw:
1112
with open(str(source), "r") as f:
1213
source = f.read()
14+
else:
15+
source = inspect.cleandoc(source)
1316
return Import(client.define_module(name, source))
1417

1518

@@ -43,13 +46,13 @@ def __init__(
4346
fallback: Optional[str] = None,
4447
install: Union[str, bool] = False,
4548
) -> None:
46-
if install is not False:
47-
if not client.import_path(package):
49+
if install:
50+
if not client.module_exists("web_modules", package):
4851
if isinstance(install, str):
4952
client.install(f"{install} {package}")
5053
else:
5154
client.install(f"{package} {package}")
52-
new_import_path = client.import_path(package)
55+
new_import_path = client.import_path("web_modules", package)
5356
if new_import_path is None:
5457
raise ValueError(f"Unexpectedly failed to find install of {package}")
5558
package = new_import_path

0 commit comments

Comments
 (0)