1
1
import sys
2
+ import time
3
+ from os .path import getmtime
2
4
from pathlib import Path
5
+ from threading import Thread
3
6
4
7
import idom
8
+ from idom .widgets .utils import hotswap
5
9
6
10
7
11
here = Path (__file__ ).parent
13
17
continue
14
18
15
19
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
+
16
33
def main ():
17
34
try :
18
35
ex_name = sys .argv [1 ]
@@ -29,16 +46,22 @@ def main():
29
46
return
30
47
31
48
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 )
42
65
43
66
44
67
def _print_available_options ():
0 commit comments