1
1
from pathlib import Path
2
+ from typing import Any
2
3
3
4
from docutils .parsers .rst import directives
4
5
from docutils .statemachine import StringList
5
6
from sphinx .application import Sphinx
6
7
from sphinx .util .docutils import SphinxDirective
7
8
from sphinx_design .tabs import TabSetDirective
8
9
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
14
11
15
12
16
13
class WidgetExample (SphinxDirective ):
@@ -31,54 +28,54 @@ def run(self):
31
28
live_example_is_default_tab = "result-is-default-tab" in self .options
32
29
activate_result = "activate-result" in self .options
33
30
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 :
36
33
src_file , line_num = self .get_source_info ()
37
34
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 } "
39
37
)
40
38
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 (
47
65
name = example_name ,
48
66
with_activate_button = not activate_result ,
49
67
),
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" ]
69
68
)
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 )
70
73
71
74
return TabSetDirective (
72
75
"WidgetExample" ,
73
76
[],
74
77
{},
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 ),
82
79
self .lineno - 1 ,
83
80
self .content_offset ,
84
81
"" ,
@@ -97,16 +94,18 @@ def _make_tab_items(labeled_content_tuples):
97
94
return _string_to_nested_lines (tab_items )
98
95
99
96
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} " )
107
106
108
107
return _literal_include_template .format (
109
- name = name ,
108
+ name = str ( path . relative_to ( EXAMPLES_DIR )) ,
110
109
language = language ,
111
110
linenos = ":linenos:" if linenos else "" ,
112
111
)
0 commit comments