Skip to content

Commit bd327c3

Browse files
committed
misc fixes
1 parent 8d5b995 commit bd327c3

File tree

6 files changed

+45
-16
lines changed

6 files changed

+45
-16
lines changed

docs/source/escape-hatches/_examples/super_simple_chart/app.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55

66
file = Path(__file__).parent / "super-simple-chart.js"
7-
ssc = web.module_from_file("super-simple-chart", file, fallback="⌛")
7+
ssc = web.module_from_file(
8+
"super-simple-chart",
9+
file,
10+
fallback="⌛",
11+
# normally this option is not required
12+
replace_existing=True,
13+
)
814
SuperSimpleChart = web.export(ssc, "SuperSimpleChart")
915

1016

docs/source/reference-material/_examples/matplotlib_plot.py

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def poly_coef_input(index, callback):
7171
"onChange": callback,
7272
},
7373
),
74+
key=index,
7475
)
7576

7677

docs/source/reference-material/_examples/snake_game.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ def GameView():
1818
game_state, set_game_state = idom.hooks.use_state(GameState.init)
1919

2020
if game_state == GameState.play:
21-
return GameLoop(grid_size=6, block_scale=50, set_game_state=set_game_state)
21+
return GameLoop(
22+
grid_size=6,
23+
block_scale=50,
24+
set_game_state=set_game_state,
25+
key="game loop",
26+
)
2227

2328
start_button = idom.html.button(
2429
{"onClick": lambda event: set_game_state(GameState.play)},
@@ -40,7 +45,12 @@ def GameView():
4045
"""
4146
)
4247

43-
return idom.html.div({"className": "snake-game-menu"}, menu_style, menu)
48+
return idom.html.div(
49+
{"className": "snake-game-menu"},
50+
menu_style,
51+
menu,
52+
key="menu",
53+
)
4454

4555

4656
class Direction(enum.Enum):
@@ -154,14 +164,18 @@ def create_grid(grid_size, block_scale):
154164
[
155165
idom.html.div(
156166
{"style": {"height": f"{block_scale}px"}},
157-
[create_grid_block("black", block_scale) for i in range(grid_size)],
167+
[
168+
create_grid_block("black", block_scale, key=i)
169+
for i in range(grid_size)
170+
],
171+
key=i,
158172
)
159173
for i in range(grid_size)
160174
],
161175
)
162176

163177

164-
def create_grid_block(color, block_scale):
178+
def create_grid_block(color, block_scale, key):
165179
return idom.html.div(
166180
{
167181
"style": {
@@ -170,7 +184,8 @@ def create_grid_block(color, block_scale):
170184
"backgroundColor": color,
171185
"outline": "1px solid grey",
172186
}
173-
}
187+
},
188+
key=key,
174189
)
175190

176191

src/idom/core/proto.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
"""Simple function returning a new component"""
2323

2424

25+
Key = Union[str, int]
26+
27+
2528
@runtime_checkable
2629
class ComponentType(Protocol):
2730
"""The expected interface for all component-like objects"""
2831

29-
key: Optional[Any]
32+
key: Key | None
3033
"""An identifier which is unique amongst a component's immediate siblings"""
3134

3235
def render(self) -> VdomDict:
@@ -74,7 +77,7 @@ def __exit__(
7477

7578

7679
class _VdomDictOptional(TypedDict, total=False):
77-
key: str
80+
key: Key | None
7881
children: Sequence[
7982
# recursive types are not allowed yet:
8083
# https://github.com/python/mypy/issues/731
@@ -101,7 +104,7 @@ class ImportSourceDict(TypedDict):
101104

102105

103106
class _OptionalVdomJson(TypedDict, total=False):
104-
key: str
107+
key: Key
105108
error: str
106109
children: List[Any]
107110
attributes: Dict[str, Any]

src/idom/core/vdom.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def is_vdom(value: Any) -> bool:
129129
def vdom(
130130
tag: str,
131131
*attributes_and_children: VdomAttributesAndChildren,
132-
key: str = "",
132+
key: str | int | None = None,
133133
event_handlers: Optional[EventHandlerMapping] = None,
134134
import_source: Optional[ImportSourceDict] = None,
135135
) -> VdomDict:
@@ -169,7 +169,7 @@ def vdom(
169169
if event_handlers:
170170
model["eventHandlers"] = event_handlers
171171

172-
if key != "":
172+
if key is not None:
173173
model["key"] = key
174174

175175
if import_source is not None:
@@ -182,7 +182,7 @@ class _VdomDictConstructor(Protocol):
182182
def __call__(
183183
self,
184184
*attributes_and_children: VdomAttributesAndChildren,
185-
key: str = ...,
185+
key: str | int | None = ...,
186186
event_handlers: Optional[EventHandlerMapping] = ...,
187187
import_source: Optional[ImportSourceDict] = ...,
188188
) -> VdomDict:
@@ -200,7 +200,7 @@ def make_vdom_constructor(
200200

201201
def constructor(
202202
*attributes_and_children: VdomAttributesAndChildren,
203-
key: str = "",
203+
key: str | int | None = None,
204204
event_handlers: Optional[EventHandlerMapping] = None,
205205
import_source: Optional[ImportSourceDict] = None,
206206
) -> VdomDict:
@@ -333,7 +333,11 @@ def _is_single_child(value: Any) -> bool:
333333
logger.error(f"Key not specified for child in list {child}")
334334
elif isinstance(child, Mapping) and "key" not in child:
335335
# remove 'children' to reduce log spam
336-
child_copy = {**child, "children": ...}
336+
child_copy = {**child, "children": _EllipsisRepr()}
337337
logger.error(f"Key not specified for child in list {child_copy}")
338338

339339
return False
340+
341+
class _EllipsisRepr:
342+
def __repr__(self) -> str:
343+
return "..."

src/idom/web/module.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ def module_from_file(
195195
Using this option has negative performance consequences since all DOM
196196
elements must be changed on each render. See :issue:`461` for more info.
197197
replace_existing:
198-
Whether to replace the source for a module with the same name if
199-
if already exists. Otherwise raise an error.
198+
Whether to replace the source for a module with the same name if it already
199+
exists. Otherwise raise an error.
200200
"""
201201
source_file = Path(file)
202202
target_file = _web_module_path(name)

0 commit comments

Comments
 (0)