1010
1111
1212HERE = Path (__file__ )
13- EXAMPLES_DIR = HERE .parent / "source" / "_examples"
13+ SOURCE_DIR = HERE .parent / "source"
14+ CONF_FILE = SOURCE_DIR / "conf.py"
1415RUN_IDOM = idom .run
1516
1617
@@ -21,26 +22,48 @@ def load_examples() -> Iterator[tuple[str, Callable[[], ComponentType]]]:
2122
2223def all_example_names () -> set [str ]:
2324 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 ))
3428 return names
3529
3630
3731def load_one_example (file_or_name : Path | str ) -> Callable [[], ComponentType ]:
3832 return lambda : (
39- # we do this to ensure each instance is fresh
33+ # we use a lambda to ensure each instance is fresh
4034 _load_one_example (file_or_name )
4135 )
4236
4337
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+
4467def _load_one_example (file_or_name : Path | str ) -> ComponentType :
4568 if isinstance (file_or_name , str ):
4669 file = get_main_example_file_by_name (file_or_name )
@@ -95,25 +118,13 @@ def PrintView():
95118 return Wrapper ()
96119
97120
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
110125 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 ("/" ))
117128
118129
119130class _PrintBuffer :
0 commit comments