Skip to content

Commit 9e45531

Browse files
committed
allow example to live closer to content
1 parent 6335666 commit 9e45531

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+103
-308
lines changed

docs/examples.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ def load_one_example(file_or_name: Path | str) -> Callable[[], ComponentType]:
3535
)
3636

3737

38+
def get_normalized_example_name(
39+
name: str, relative_to: str | Path | None = SOURCE_DIR
40+
) -> str:
41+
return "/".join(
42+
_get_root_example_path_by_name(name, relative_to).relative_to(SOURCE_DIR).parts
43+
)
44+
45+
3846
def get_main_example_file_by_name(
39-
name: str, relative_to: str | Path = SOURCE_DIR
47+
name: str, relative_to: str | Path | None = SOURCE_DIR
4048
) -> Path:
4149
path = _get_root_example_path_by_name(name, relative_to)
4250
if path.is_dir():
@@ -46,7 +54,7 @@ def get_main_example_file_by_name(
4654

4755

4856
def get_example_files_by_name(
49-
name: str, relative_to: str | Path = SOURCE_DIR
57+
name: str, relative_to: str | Path | None = SOURCE_DIR
5058
) -> list[Path]:
5159
path = _get_root_example_path_by_name(name, relative_to)
5260
if path.is_dir():
@@ -58,9 +66,10 @@ def get_example_files_by_name(
5866

5967
def _iter_example_files() -> Iterator[Path]:
6068
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:
69+
if path.is_dir():
70+
if not path.name.startswith("_") or path.name == "_examples":
71+
yield from path.rglob("*.py")
72+
elif path != CONF_FILE and path.suffix == ".py":
6473
yield path
6574

6675

@@ -118,8 +127,8 @@ def PrintView():
118127
return Wrapper()
119128

120129

121-
def _get_root_example_path_by_name(name: str, relative_to: str | Path) -> Path:
122-
if not name.startswith("/"):
130+
def _get_root_example_path_by_name(name: str, relative_to: str | Path | None) -> Path:
131+
if not name.startswith("/") and relative_to is not None:
123132
rel_path = Path(relative_to)
124133
rel_path = rel_path.parent if rel_path.is_file() else rel_path
125134
else:

docs/source/_examples/show_click_event.py

-15
This file was deleted.

docs/source/_examples/simple_and_gate.py

-24
This file was deleted.

docs/source/_examples/snake_game_started.py

-180
This file was deleted.

docs/source/_exts/interactive_widget.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import os
22

33
from docutils.nodes import raw
4-
from docutils.parsers.rst import Directive, directives
4+
from docutils.parsers.rst import directives
55
from sphinx.application import Sphinx
6+
from sphinx.util.docutils import SphinxDirective
7+
8+
from docs.examples import get_normalized_example_name
69

710

811
_IDOM_EXAMPLE_HOST = os.environ.get("IDOM_DOC_EXAMPLE_SERVER_HOST", "")
912
_IDOM_STATIC_HOST = os.environ.get("IDOM_DOC_STATIC_SERVER_HOST", "/docs").rstrip("/")
1013

1114

12-
class IteractiveWidget(Directive):
15+
class IteractiveWidget(SphinxDirective):
1316

1417
has_content = False
1518
required_arguments = 1
@@ -23,7 +26,11 @@ class IteractiveWidget(Directive):
2326
def run(self):
2427
IteractiveWidget._next_id += 1
2528
container_id = f"idom-widget-{IteractiveWidget._next_id}"
26-
view_id = self.arguments[0]
29+
view_id = get_normalized_example_name(
30+
self.arguments[0],
31+
# only used if example name starts with "/"
32+
self.get_source_info()[0],
33+
)
2734
return [
2835
raw(
2936
"",

docs/source/_exts/widget_example.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
from sphinx.util.docutils import SphinxDirective
1111
from sphinx_design.tabs import TabSetDirective
1212

13-
from docs.examples import SOURCE_DIR, get_example_files_by_name
13+
from docs.examples import (
14+
SOURCE_DIR,
15+
get_example_files_by_name,
16+
get_normalized_example_name,
17+
)
1418

1519

1620
class WidgetExample(SphinxDirective):
@@ -25,16 +29,17 @@ class WidgetExample(SphinxDirective):
2529
}
2630

2731
def run(self):
28-
example_name = self.arguments[0]
32+
example_name = get_normalized_example_name(
33+
self.arguments[0],
34+
# only used if example name starts with "/"
35+
self.get_source_info()[0],
36+
)
37+
2938
show_linenos = "linenos" in self.options
3039
live_example_is_default_tab = "result-is-default-tab" in self.options
3140
activate_result = "activate-result" in self.options
3241

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-
)
42+
ex_files = get_example_files_by_name(example_name)
3843
if not ex_files:
3944
src_file, line_num = self.get_source_info()
4045
raise ValueError(
@@ -162,7 +167,7 @@ def _interactive_widget(name, with_activate_button):
162167

163168

164169
_literal_include_template = """
165-
.. literalinclude:: /_examples/{name}
170+
.. literalinclude:: /{name}
166171
:language: {language}
167172
{options}
168173
"""

0 commit comments

Comments
 (0)