11from pathlib import Path
2+ from typing import Any
23
34from docutils .parsers .rst import directives
45from docutils .statemachine import StringList
56from sphinx .application import Sphinx
67from sphinx .util .docutils import SphinxDirective
78from sphinx_design .tabs import TabSetDirective
89
9- from docs .examples import get_js_example_file_by_name , get_py_example_file_by_name
10-
11-
12- HERE = Path (__file__ )
13- EXAMPLES_DIR = HERE .parent .parent / "_examples"
10+ from docs .examples import EXAMPLES_DIR , get_example_files_by_name
1411
1512
1613class WidgetExample (SphinxDirective ):
@@ -31,54 +28,54 @@ def run(self):
3128 live_example_is_default_tab = "result-is-default-tab" in self .options
3229 activate_result = "activate-result" in self .options
3330
34- py_ex_path = get_py_example_file_by_name (example_name )
35- if not py_ex_path . exists () :
31+ ex_files = get_example_files_by_name (example_name )
32+ if not ex_files :
3633 src_file , line_num = self .get_source_info ()
3734 raise ValueError (
38- f"Missing example file named { py_ex_path } referenced by document { src_file } :{ line_num } "
35+ f"Missing example named { example_name !r} "
36+ f"referenced by document { src_file } :{ line_num } "
3937 )
4038
41- labeled_tab_items = {
42- "python" : _literal_include (
43- name = str (py_ex_path .relative_to (EXAMPLES_DIR )),
44- linenos = show_linenos ,
45- ),
46- "result" : _interactive_widget (
39+ labeled_tab_items : list [tuple [str , Any ]] = []
40+ if len (ex_files ) == 1 :
41+ labeled_tab_items .append (
42+ (
43+ "app.py" ,
44+ _literal_include (
45+ path = ex_files [0 ],
46+ linenos = show_linenos ,
47+ ),
48+ )
49+ )
50+ else :
51+ for path in sorted (ex_files , key = lambda p : p .name ):
52+ labeled_tab_items .append (
53+ (
54+ path .name ,
55+ _literal_include (
56+ path = path ,
57+ linenos = show_linenos ,
58+ ),
59+ )
60+ )
61+
62+ result_tab_item = (
63+ "▶️ result" ,
64+ _interactive_widget (
4765 name = example_name ,
4866 with_activate_button = not activate_result ,
4967 ),
50- }
51-
52- labeled_tab_titles = {
53- "python" : "Python" ,
54- "javascript" : "Javascript" ,
55- "result" : "▶️ Result" ,
56- }
57-
58- js_ex_path = get_js_example_file_by_name (example_name )
59- if js_ex_path .exists ():
60- labeled_tab_items ["javascript" ] = _literal_include (
61- name = str (js_ex_path .relative_to (EXAMPLES_DIR )),
62- linenos = show_linenos ,
63- )
64-
65- tab_label_order = (
66- ["result" , "python" , "javascript" ]
67- if live_example_is_default_tab
68- else ["python" , "javascript" , "result" ]
6968 )
69+ if live_example_is_default_tab :
70+ labeled_tab_items .insert (0 , result_tab_item )
71+ else :
72+ labeled_tab_items .append (result_tab_item )
7073
7174 return TabSetDirective (
7275 "WidgetExample" ,
7376 [],
7477 {},
75- _make_tab_items (
76- [
77- (labeled_tab_titles [label ], labeled_tab_items [label ])
78- for label in tab_label_order
79- if label in labeled_tab_items
80- ]
81- ),
78+ _make_tab_items (labeled_tab_items ),
8279 self .lineno - 1 ,
8380 self .content_offset ,
8481 "" ,
@@ -97,16 +94,18 @@ def _make_tab_items(labeled_content_tuples):
9794 return _string_to_nested_lines (tab_items )
9895
9996
100- def _literal_include (name , linenos ):
101- if name .endswith (".py" ):
102- language = "python"
103- elif name .endswith (".js" ):
104- language = "javascript"
105- else :
106- raise ValueError ("Unknown extension type" )
97+ def _literal_include (path : Path , linenos : bool ):
98+ try :
99+ language = {
100+ ".py" : "python" ,
101+ ".js" : "javascript" ,
102+ ".json" : "json" ,
103+ }[path .suffix ]
104+ except KeyError :
105+ raise ValueError (f"Unknown extension type { path .suffix !r} " )
107106
108107 return _literal_include_template .format (
109- name = name ,
108+ name = str ( path . relative_to ( EXAMPLES_DIR )) ,
110109 language = language ,
111110 linenos = ":linenos:" if linenos else "" ,
112111 )
0 commit comments