Skip to content

Commit 6c09a86

Browse files
committed
change shared to update_on_change
also adds reload capability for one_example script
1 parent cfdd67a commit 6c09a86

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

docs/source/examples/slideshow.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ def next_image(event):
1010

1111
return idom.html.img(
1212
{
13-
"src": f"https://picsum.photos/800/300?image={index}",
13+
"src": f"https://picsum.photos/400/200?image={index}",
1414
"style": {"cursor": "pointer"},
15+
"height": "300px",
1516
"onClick": next_image,
1617
}
1718
)

scripts/one_example.py

+33-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import sys
2+
import time
3+
from os.path import getmtime
24
from pathlib import Path
5+
from threading import Thread
36

47
import idom
8+
from idom.widgets.utils import hotswap
59

610

711
here = Path(__file__).parent
@@ -13,6 +17,19 @@
1317
continue
1418

1519

20+
def on_file_change(path, callback):
21+
def watch_for_change():
22+
last_modified = 0
23+
while True:
24+
modified_at = getmtime(path)
25+
if modified_at != last_modified:
26+
callback()
27+
last_modified = modified_at
28+
time.sleep(1)
29+
30+
Thread(target=watch_for_change, daemon=True).start()
31+
32+
1633
def main():
1734
try:
1835
ex_name = sys.argv[1]
@@ -29,16 +46,22 @@ def main():
2946
return
3047

3148
idom_run = idom.run
32-
idom.run = lambda component: idom_run(component, port=8000)
33-
34-
with example_file.open() as f:
35-
exec(
36-
f.read(),
37-
{
38-
"__file__": str(file),
39-
"__name__": f"__main__.examples.{file.stem}",
40-
},
41-
)
49+
idom.run, component = hotswap(update_on_change=True)
50+
51+
def update_component():
52+
print(f"Reloading {ex_name}")
53+
with example_file.open() as f:
54+
exec(
55+
f.read(),
56+
{
57+
"__file__": str(file),
58+
"__name__": f"__main__.examples.{file.stem}",
59+
},
60+
)
61+
62+
on_file_change(example_file, update_component)
63+
64+
idom_run(component, port=8000)
4265

4366

4467
def _print_available_options():

src/idom/server/prefab.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def hotswap_server(
142142
The server instance and a function for swapping views.
143143
See :func:`idom.widgets.common.hotswap` for details.
144144
"""
145-
mount, component = hotswap(shared=sync_views)
145+
mount, component = hotswap(update_on_change=sync_views)
146146

147147
server = run(
148148
component,

src/idom/widgets/utils.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
MountFunc = Callable[[ComponentConstructor], None]
1515

1616

17-
def hotswap(shared: bool = False) -> Tuple[MountFunc, ComponentConstructor]:
17+
def hotswap(update_on_change: bool = False) -> Tuple[MountFunc, ComponentConstructor]:
1818
"""Swap out components from a layout on the fly.
1919
2020
Since you can't change the component functions used to create a layout
2121
in an imperative manner, you can use ``hotswap`` to do this so
2222
long as you set things up ahead of time.
2323
2424
Parameters:
25-
shared: Whether or not all views of the layout should be udpated on a swap.
25+
update_on_change: Whether or not all views of the layout should be udpated on a swap.
2626
2727
Example:
2828
.. code-block:: python
@@ -50,7 +50,7 @@ def DivTwo(self):
5050
"""
5151
constructor_ref: Ref[Callable[[], Any]] = Ref(lambda: {"tagName": "div"})
5252

53-
if shared:
53+
if update_on_change:
5454
set_constructor_callbacks: Set[Callable[[Callable[[], Any]], None]] = set()
5555

5656
@component

tests/test_widgets/test_utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_multiview_repr():
1010
assert str(idom.widgets.utils.MultiViewMount({})) == "MultiViewMount({})"
1111

1212

13-
def test_shared_hostwap(driver, display):
13+
def test_hostwap_update_on_change(driver, display):
1414
"""Ensure shared hotswapping works
1515
1616
This basically means that previously rendered views of a hotswap component get updated
@@ -39,7 +39,7 @@ async def on_click(event):
3939

4040
incr = idom.html.button({"onClick": on_click, "id": "incr-button"}, "incr")
4141

42-
mount, make_hostswap = idom.widgets.hotswap(shared=True)
42+
mount, make_hostswap = idom.widgets.hotswap(update_on_change=True)
4343
mount(make_next_count_constructor(count))
4444
hotswap_view = make_hostswap()
4545

0 commit comments

Comments
 (0)