-
-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathone_example.py
More file actions
103 lines (78 loc) · 2.51 KB
/
Copy pathone_example.py
File metadata and controls
103 lines (78 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import sys
import time
from os.path import getmtime
from threading import Event, Thread
import idom
from docs.examples import all_example_names, get_example_files_by_name, load_one_example
from idom.widgets import hotswap
EXAMPLE_NAME_SET = all_example_names()
EXAMPLE_NAME_LIST = tuple(sorted(EXAMPLE_NAME_SET))
def on_file_change(path, callback):
did_call_back_once = Event()
def watch_for_change():
last_modified = 0
while True:
modified_at = getmtime(path)
if modified_at != last_modified:
callback()
did_call_back_once.set()
last_modified = modified_at
time.sleep(1)
Thread(target=watch_for_change, daemon=True).start()
did_call_back_once.wait()
def main():
ex_name = _example_name_input()
mount, component = hotswap(update_on_change=True)
def update_component():
print(f"Loading example: {ex_name!r}")
mount(load_one_example(ex_name))
for file in get_example_files_by_name(ex_name):
on_file_change(file, update_component)
idom.run(component)
def _example_name_input() -> str:
if len(sys.argv) == 1:
_print_error(
"No example argument given. Provide an example's number from above."
)
sys.exit(1)
ex_num = sys.argv[1]
try:
ex_num = int(ex_num)
except ValueError:
_print_error(
f"No example {ex_num!r} exists. Provide an example's number as an integer."
)
sys.exit(1)
ex_index = ex_num - 1
try:
return EXAMPLE_NAME_LIST[ex_index]
except IndexError:
_print_error(f"No example #{ex_num} exists. Choose from an option above.")
sys.exit(1)
def _print_error(*args) -> None:
_print_available_options()
print(*args)
def _print_available_options():
examples_by_path = {}
for i, name in enumerate(EXAMPLE_NAME_LIST):
if "/" not in name:
path = ""
else:
path, name = name.rsplit("/", 1)
examples_by_path.setdefault(path, []).append(name)
number = 1
print()
for path, names in examples_by_path.items():
title = " > ".join(
section.replace("-", " ").replace("_", " ").title()
for section in path.split("/")
if not section.startswith("_")
)
print(title)
print("-" * len(title))
for name in names:
print(f"{number}. ", name)
number += 1
print()
if __name__ == "__main__":
main()