11import sys
2+ import time
3+ from os .path import getmtime
24from pathlib import Path
5+ from threading import Thread
36
47import idom
8+ from idom .widgets .utils import hotswap
59
610
711here = Path (__file__ ).parent
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+
1633def 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
4467def _print_available_options ():
0 commit comments