Skip to content

Commit 897b84d

Browse files
committed
reorganize + misc simplifications + test coverage
1 parent 74494ff commit 897b84d

File tree

19 files changed

+428
-246
lines changed

19 files changed

+428
-246
lines changed

examples/chart.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ export default {
1313
domainPadding=${20}
1414
>
1515
<${VictoryBar}
16-
data=${[
17-
{ x: 1, y: 2 },
18-
{ x: 2, y: 4 },
19-
{ x: 3, y: 7 },
20-
{ x: 4, y: 3 },
21-
{ x: 5, y: 5 },
22-
]}
16+
data=${props.data}
2317
dataComponent=${html`
2418
<${Bar}
2519
events=${{

examples/introduction.ipynb

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"\n",
8181
"def display(element, *args, **kwargs):\n",
8282
" view_id = mount(element, *args, **kwargs)\n",
83+
" print(f\"View ID: {view_id}\")\n",
8384
" return idom.display(\"jupyter\", server_url, {\"view_id\": view_id})"
8485
]
8586
},
@@ -296,6 +297,15 @@
296297
"for the `mu` and `sigma` inputs to be the same."
297298
]
298299
},
300+
{
301+
"cell_type": "code",
302+
"execution_count": null,
303+
"metadata": {},
304+
"outputs": [],
305+
"source": [
306+
"import inspect"
307+
]
308+
},
299309
{
300310
"cell_type": "code",
301311
"execution_count": null,
@@ -351,11 +361,11 @@
351361
" var = idom.Var(value)\n",
352362
"\n",
353363
" inputs = []\n",
354-
" for t in types:\n",
355-
" inp = idom.Input(t, value, **attributes)\n",
364+
" for tp in types:\n",
365+
" inp = idom.Input(tp, value, attributes, cast=float)\n",
356366
"\n",
357367
" @inp.events.on(\"change\")\n",
358-
" async def on_change(inp, event):\n",
368+
" async def on_change(event, inp=inp):\n",
359369
" for i in inputs:\n",
360370
" i.update(inp.value)\n",
361371
" var.set(inp.value)\n",
@@ -673,9 +683,10 @@
673683
"source": [
674684
"print(\"It may take a moment to download and install Victory... 🕒\")\n",
675685
"\n",
676-
"victory = idom.Import(\"victory\", fallback=\"loading...\", install=True)\n",
686+
"victory = idom.Module(\"victory\", install=True)\n",
687+
"VictoryBar = victory.Import(\"VictoryBar\")\n",
677688
"\n",
678-
"display(victory.VictoryBar, {\"style\": {\"parent\": {\"width\": \"500px\"}}})"
689+
"display(VictoryBar, {\"style\": {\"parent\": {\"width\": \"500px\"}}})"
679690
]
680691
},
681692
{
@@ -697,14 +708,23 @@
697708
"source": [
698709
"print(\"Click the bars to see event data printed in Python 👇\")\n",
699710
"\n",
700-
"chart = idom.widgets.define_module(\"chart\", \"chart.js\")\n",
711+
"with open(\"chart.js\") as f:\n",
712+
" ClickableChart = idom.Module(\"chart\", source=f).Import(\"ClickableChart\")\n",
701713
"\n",
702714
"async def handle_event(event):\n",
703715
" print(event)\n",
704716
"\n",
717+
"data = [\n",
718+
" {\"x\": 1, \"y\": 2},\n",
719+
" {\"x\": 2, \"y\": 4},\n",
720+
" {\"x\": 3, \"y\": 7},\n",
721+
" {\"x\": 4, \"y\": 3},\n",
722+
" {\"x\": 5, \"y\": 5},\n",
723+
"]\n",
724+
"\n",
705725
"display(\n",
706-
" chart.ClickableChart,\n",
707-
" {\"onClick\": handle_event, \"style\": {\"parent\": {\"width\": \"500px\"}}}\n",
726+
" ClickableChart,\n",
727+
" {\"data\": data, \"onClick\": handle_event, \"style\": {\"parent\": {\"width\": \"500px\"}}}\n",
708728
")"
709729
]
710730
},

idom/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
from .core.layout import Layout
88
from .core.vdom import vdom
99

10+
from .widgets.input import Input
1011
from .widgets.html import html
11-
from .widgets.common import hotswap, Import
12+
from .widgets.utils import Module, Import, hotswap, multiview
1213
from .widgets.display import display
13-
from .widgets.inputs import Input
14-
from .widgets.images import Image
14+
from .widgets.image import Image
1515

1616
from .tools import Var, html_to_vdom
1717

@@ -42,8 +42,10 @@
4242
"Var",
4343
"vdom",
4444
"Image",
45+
"Module",
4546
"Import",
4647
"hotswap",
48+
"multiview",
4749
"display",
4850
"Input",
4951
"html_to_vdom",

idom/client/__init__.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,42 @@
1414
WEB_MODULES = CLIENT_DIR / "web_modules"
1515

1616

17-
def import_path(name: str) -> str:
17+
def core_module(name: str) -> str:
18+
path = f"../{CORE_MODULES.name}/{name}.js"
19+
if not core_module_exists(name):
20+
raise ValueError(f"Module '{path}' does not exist.")
21+
return path
22+
23+
24+
def core_module_exists(name: str) -> bool:
25+
return _find_module_os_path(CORE_MODULES, name) is not None
26+
27+
28+
def web_module(name: str) -> str:
1829
path = f"../{WEB_MODULES.name}/{name}.js"
19-
if not module_exists(name):
30+
if not web_module_exists(name):
2031
raise ValueError(f"Module '{path}' does not exist.")
2132
return path
2233

2334

24-
def define_module(name: str, source: str) -> str:
35+
def web_module_exists(name: str) -> bool:
36+
return _find_module_os_path(WEB_MODULES, name) is not None
37+
38+
39+
def define_web_module(name: str, source: str) -> str:
2540
path = _create_web_module_os_path(name)
2641
with path.open("w+") as f:
2742
f.write(source)
28-
return import_path(name)
43+
return web_module(name)
2944

3045

31-
def delete_module(name: str) -> None:
32-
path = _find_web_module_os_path(name)
46+
def delete_web_module(name: str) -> None:
47+
path = _find_module_os_path(WEB_MODULES, name)
3348
if path is None:
3449
raise ValueError(f"Module '{name}' does not exist.")
3550
_delete_os_paths(path)
3651

3752

38-
def module_exists(name: str) -> bool:
39-
return _find_web_module_os_path(name) is not None
40-
41-
4253
def install(dependencies: Dict[str, str]) -> None:
4354
pkg = _package_json()
4455

@@ -98,8 +109,7 @@ def _run_subprocess(args: List[str], cwd: Union[str, Path]):
98109
raise
99110

100111

101-
def _find_web_module_os_path(name: str) -> Optional[Path]:
102-
path = WEB_MODULES
112+
def _find_module_os_path(path: Path, name: str) -> Optional[Path]:
103113
for name_part in name.split("/"):
104114
if not path.is_dir():
105115
return None

idom/core/element.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
overload,
1515
Awaitable,
1616
Union,
17+
overload,
1718
)
1819

1920

@@ -115,7 +116,11 @@ async def unmount(self) -> None:
115116
"""
116117
self._layout = None
117118

118-
def _update_layout(self) -> None:
119+
# FIXME: https://github.com/python/mypy/issues/5876
120+
update: Callable[..., Any]
121+
122+
def update(self) -> None: # type: ignore
123+
"""Schedule this element to be rendered again soon."""
119124
if self._layout is not None:
120125
self._layout.update(self)
121126

@@ -184,7 +189,7 @@ def update(self, *args: Any, **kwargs: Any) -> None:
184189
self._state = {}
185190
self._state_updated = True
186191
self._cancel_animation()
187-
self._update_layout()
192+
super().update()
188193
bound = self._function_signature.bind_partial(None, *args, **kwargs)
189194
self._state.update(list(bound.arguments.items())[1:])
190195

idom/core/events.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from weakref import proxy, CallableProxyType
2-
from functools import partial
31
from typing import (
42
Mapping,
53
Dict,
@@ -11,7 +9,6 @@
119
Awaitable,
1210
Union,
1311
)
14-
from .element import AbstractElement
1512

1613

1714
EventsMapping = Union[
@@ -66,13 +63,10 @@ class Events(Mapping[str, "EventHandler"]):
6663
Assign this object to the ``"eventHandlers"`` field of an element model.
6764
"""
6865

69-
__slots__ = ("_handlers", "_bound")
66+
__slots__ = "_handlers"
7067

71-
def __init__(self, bound: Optional[AbstractElement] = None) -> None:
68+
def __init__(self) -> None:
7269
self._handlers: Dict[str, EventHandler] = {}
73-
self._bound: Optional[CallableProxyType] = None
74-
if bound is not None:
75-
self._bound = proxy(bound)
7670

7771
def on(
7872
self, event: str, stop_propagation: bool = False, prevent_default: bool = False
@@ -118,8 +112,6 @@ def handler(event):
118112
handler = self._handlers[event_name]
119113

120114
def setup(function: EventHandlerFunction) -> EventHandlerFunction:
121-
if self._bound is not None:
122-
function = partial(function, self._bound)
123115
handler.add(function)
124116
return function
125117

0 commit comments

Comments
 (0)