Skip to content

Commit 6335666

Browse files
committed
try and make examples load relative to current file
1 parent 1c1e573 commit 6335666

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

docs/examples.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111

1212
HERE = Path(__file__)
13-
EXAMPLES_DIR = HERE.parent / "source" / "_examples"
13+
SOURCE_DIR = HERE.parent / "source"
14+
CONF_FILE = SOURCE_DIR / "conf.py"
1415
RUN_IDOM = idom.run
1516

1617

@@ -21,26 +22,48 @@ def load_examples() -> Iterator[tuple[str, Callable[[], ComponentType]]]:
2122

2223
def 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

3731
def 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+
4467
def _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

119130
class _PrintBuffer:

docs/source/_exts/widget_example.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sphinx.util.docutils import SphinxDirective
1111
from sphinx_design.tabs import TabSetDirective
1212

13-
from docs.examples import EXAMPLES_DIR, get_example_files_by_name
13+
from docs.examples import SOURCE_DIR, get_example_files_by_name
1414

1515

1616
class WidgetExample(SphinxDirective):
@@ -25,13 +25,16 @@ class WidgetExample(SphinxDirective):
2525
}
2626

2727
def run(self):
28-
print(self.get_source_info())
2928
example_name = self.arguments[0]
3029
show_linenos = "linenos" in self.options
3130
live_example_is_default_tab = "result-is-default-tab" in self.options
3231
activate_result = "activate-result" in self.options
3332

34-
ex_files = get_example_files_by_name(example_name)
33+
ex_files = get_example_files_by_name(
34+
example_name,
35+
# only used if example name starts with "/"
36+
relative_to=self.get_source_info()[0],
37+
)
3538
if not ex_files:
3639
src_file, line_num = self.get_source_info()
3740
raise ValueError(
@@ -108,7 +111,7 @@ def _literal_include(path: Path, linenos: bool):
108111
raise ValueError(f"Unknown extension type {path.suffix!r}")
109112

110113
return _literal_include_template.format(
111-
name=str(path.relative_to(EXAMPLES_DIR)),
114+
name=str(path.relative_to(SOURCE_DIR)),
112115
language=language,
113116
options=_join_options(_get_file_options(path)),
114117
)
@@ -170,4 +173,4 @@ def _string_to_nested_lines(content):
170173

171174

172175
def setup(app: Sphinx) -> None:
173-
app.add_directive("example", WidgetExample)
176+
app.add_directive("idom", WidgetExample)

0 commit comments

Comments
 (0)