10
10
11
11
12
12
HERE = Path (__file__ )
13
- EXAMPLES_DIR = HERE .parent / "source" / "_examples"
13
+ SOURCE_DIR = HERE .parent / "source"
14
+ CONF_FILE = SOURCE_DIR / "conf.py"
14
15
RUN_IDOM = idom .run
15
16
16
17
@@ -21,26 +22,48 @@ def load_examples() -> Iterator[tuple[str, Callable[[], ComponentType]]]:
21
22
22
23
def all_example_names () -> set [str ]:
23
24
names = set ()
24
- for file in EXAMPLES_DIR .rglob ("*.py" ):
25
- if file .name != "app.py" and (file .parent / "app.py" ).exists ():
26
- # this isn't the main example file
27
- continue
28
- path = file .relative_to (EXAMPLES_DIR )
29
- if path .name == "app.py" :
30
- path = path .parent
31
- else :
32
- path = path .with_suffix ("" )
33
- names .add ("/" .join (path .parts ))
25
+ for file in _iter_example_files ():
26
+ path = file .parent if file .name == "app.py" else file
27
+ names .add ("/" .join (path .relative_to (SOURCE_DIR ).with_suffix ("" ).parts ))
34
28
return names
35
29
36
30
37
31
def load_one_example (file_or_name : Path | str ) -> Callable [[], ComponentType ]:
38
32
return lambda : (
39
- # we do this to ensure each instance is fresh
33
+ # we use a lambda to ensure each instance is fresh
40
34
_load_one_example (file_or_name )
41
35
)
42
36
43
37
38
+ def get_main_example_file_by_name (
39
+ name : str , relative_to : str | Path = SOURCE_DIR
40
+ ) -> Path :
41
+ path = _get_root_example_path_by_name (name , relative_to )
42
+ if path .is_dir ():
43
+ return path / "app.py"
44
+ else :
45
+ return path .with_suffix (".py" )
46
+
47
+
48
+ def get_example_files_by_name (
49
+ name : str , relative_to : str | Path = SOURCE_DIR
50
+ ) -> list [Path ]:
51
+ path = _get_root_example_path_by_name (name , relative_to )
52
+ if path .is_dir ():
53
+ return list (path .glob ("*" ))
54
+ else :
55
+ path = path .with_suffix (".py" )
56
+ return [path ] if path .exists () else []
57
+
58
+
59
+ def _iter_example_files () -> Iterator [Path ]:
60
+ for path in SOURCE_DIR .iterdir ():
61
+ if path .is_dir () and not path .name .startswith ("_" ):
62
+ yield from path .rglob ("*.py" )
63
+ elif path != CONF_FILE :
64
+ yield path
65
+
66
+
44
67
def _load_one_example (file_or_name : Path | str ) -> ComponentType :
45
68
if isinstance (file_or_name , str ):
46
69
file = get_main_example_file_by_name (file_or_name )
@@ -95,25 +118,13 @@ def PrintView():
95
118
return Wrapper ()
96
119
97
120
98
- def get_main_example_file_by_name (name : str ) -> Path :
99
- path = _get_root_example_path_by_name (name )
100
- if path .is_dir ():
101
- return path / "app.py"
102
- else :
103
- return path .with_suffix (".py" )
104
-
105
-
106
- def get_example_files_by_name (name : str ) -> list [Path ]:
107
- path = _get_root_example_path_by_name (name )
108
- if path .is_dir ():
109
- return list (path .glob ("*" ))
121
+ def _get_root_example_path_by_name (name : str , relative_to : str | Path ) -> Path :
122
+ if not name .startswith ("/" ):
123
+ rel_path = Path (relative_to )
124
+ rel_path = rel_path .parent if rel_path .is_file () else rel_path
110
125
else :
111
- path = path .with_suffix (".py" )
112
- return [path ] if path .exists () else []
113
-
114
-
115
- def _get_root_example_path_by_name (name : str ) -> Path :
116
- return EXAMPLES_DIR .joinpath (* name .split ("/" ))
126
+ rel_path = SOURCE_DIR
127
+ return rel_path .joinpath (* name .split ("/" ))
117
128
118
129
119
130
class _PrintBuffer :
0 commit comments