35
35
html_theme = "pytorch_sphinx_theme2"
36
36
html_theme_path = [pytorch_sphinx_theme2 .get_html_theme_path ()]
37
37
38
- import torch
38
+ import distutils . file_util
39
39
import glob
40
40
import random
41
+ import re
41
42
import shutil
43
+ from pathlib import Path
44
+
42
45
import numpy
43
46
import pandocfilters
44
47
import plotly .io as pio
45
48
import pypandoc
46
49
import torch
47
- import distutils .file_util
48
- import re
49
50
from get_sphinx_filenames import SPHINX_SHOULD_RUN
50
- import pandocfilters
51
- import pypandoc
52
- import plotly .io as pio
53
- from pathlib import Path
54
51
55
52
pio .renderers .default = "sphinx_gallery"
56
53
57
- import sphinx_gallery .gen_rst
58
54
import multiprocessing
59
55
56
+ import sphinx_gallery .gen_rst
57
+
58
+
60
59
# Monkey patch sphinx gallery to run each example in an isolated process so that
61
60
# we don't need to worry about examples changing global state.
62
61
#
@@ -76,12 +75,12 @@ def call_fn(func, args, kwargs, result_queue):
76
75
except Exception as e :
77
76
result_queue .put ((False , str (e )))
78
77
78
+
79
79
def call_in_subprocess (func ):
80
80
def wrapper (* args , ** kwargs ):
81
81
result_queue = multiprocessing .Queue ()
82
82
p = multiprocessing .Process (
83
- target = call_fn ,
84
- args = (func , args , kwargs , result_queue )
83
+ target = call_fn , args = (func , args , kwargs , result_queue )
85
84
)
86
85
p .start ()
87
86
p .join ()
@@ -90,9 +89,13 @@ def wrapper(*args, **kwargs):
90
89
return result
91
90
else :
92
91
raise RuntimeError (f"Error in subprocess: { result } " )
92
+
93
93
return wrapper
94
94
95
- sphinx_gallery .gen_rst .generate_file_rst = call_in_subprocess (sphinx_gallery .gen_rst .generate_file_rst )
95
+
96
+ sphinx_gallery .gen_rst .generate_file_rst = call_in_subprocess (
97
+ sphinx_gallery .gen_rst .generate_file_rst
98
+ )
96
99
97
100
try :
98
101
import torchvision
@@ -152,18 +155,26 @@ def wrapper(*args, **kwargs):
152
155
153
156
154
157
sphinx_gallery_conf = {
155
- 'examples_dirs' : ['beginner_source' , 'intermediate_source' ,
156
- 'advanced_source' , 'recipes_source' , 'prototype_source' ],
157
- 'gallery_dirs' : ['beginner' , 'intermediate' , 'advanced' , 'recipes' , 'prototype' ],
158
- 'filename_pattern' : re .compile (SPHINX_SHOULD_RUN ),
159
- 'promote_jupyter_magic' : True ,
160
- 'backreferences_dir' : None ,
161
- 'first_notebook_cell' : ("# For tips on running notebooks in Google Colab, see\n "
162
- "# https://pytorch.org/tutorials/beginner/colab\n "
163
- "%matplotlib inline" ),
164
- 'ignore_pattern' : r'_torch_export_nightly_tutorial.py' ,
165
- 'pypandoc' : {'extra_args' : ['--mathjax' , '--toc' ],
166
- 'filters' : ['.jenkins/custom_pandoc_filter.py' ],
158
+ "examples_dirs" : [
159
+ "beginner_source" ,
160
+ "intermediate_source" ,
161
+ "advanced_source" ,
162
+ "recipes_source" ,
163
+ "prototype_source" ,
164
+ ],
165
+ "gallery_dirs" : ["beginner" , "intermediate" , "advanced" , "recipes" , "prototype" ],
166
+ "filename_pattern" : re .compile (SPHINX_SHOULD_RUN ),
167
+ "promote_jupyter_magic" : True ,
168
+ "backreferences_dir" : None ,
169
+ "first_notebook_cell" : (
170
+ "# For tips on running notebooks in Google Colab, see\n "
171
+ "# https://pytorch.org/tutorials/beginner/colab\n "
172
+ "%matplotlib inline"
173
+ ),
174
+ "ignore_pattern" : r"_torch_export_nightly_tutorial.py" ,
175
+ "pypandoc" : {
176
+ "extra_args" : ["--mathjax" , "--toc" ],
177
+ "filters" : [".jenkins/custom_pandoc_filter.py" ],
167
178
},
168
179
}
169
180
@@ -205,9 +216,9 @@ def wrapper(*args, **kwargs):
205
216
},
206
217
],
207
218
"use_edit_page_button" : True ,
208
- #"logo": {
219
+ # "logo": {
209
220
# "text": "Home",
210
- #},
221
+ # },
211
222
"header_links_before_dropdown" : 9 ,
212
223
"navbar_start" : ["pytorch_version" ],
213
224
"navbar_center" : "navbar-nav" ,
@@ -262,6 +273,41 @@ def wrapper(*args, **kwargs):
262
273
os .path .join (os .path .dirname (pytorch_sphinx_theme2 .__file__ ), "templates" ),
263
274
]
264
275
276
+
277
+ def fix_gallery_edit_links (app , pagename , templatename , context , doctree ):
278
+ if pagename .startswith (
279
+ ("beginner/" , "intermediate/" , "advanced/" , "recipes/" , "prototype/" )
280
+ ):
281
+ parts = pagename .split ("/" )
282
+ gallery_dir = parts [0 ]
283
+ example_name = parts [- 1 ]
284
+
285
+ source_dirs = {
286
+ "beginner" : "beginner_source" ,
287
+ "intermediate" : "intermediate_source" ,
288
+ "advanced" : "advanced_source" ,
289
+ "recipes" : "recipes_source" ,
290
+ "prototype" : "prototype_source" ,
291
+ }
292
+
293
+ if gallery_dir in source_dirs :
294
+ source_dir = source_dirs [gallery_dir ]
295
+ # Check if .py file exists
296
+ py_path = f"{ source_dir } /{ example_name } .py"
297
+ rst_path = f"{ source_dir } /{ example_name } .rst"
298
+
299
+ # Default to .py file, fallback to .rst if needed
300
+ file_path = py_path
301
+ if not os .path .exists (
302
+ os .path .join (os .path .dirname (__file__ ), py_path )
303
+ ) and os .path .exists (os .path .join (os .path .dirname (__file__ ), rst_path )):
304
+ file_path = rst_path
305
+
306
+ context ["edit_url" ] = (
307
+ f"{ html_context ['github_url' ]} /{ html_context ['github_user' ]} /{ html_context ['github_repo' ]} /edit/{ html_context ['github_version' ]} /{ file_path } "
308
+ )
309
+
310
+
265
311
# The suffix(es) of source filenames.
266
312
# You can specify multiple suffix as a list of string:
267
313
#
@@ -342,7 +388,7 @@ def handle_jinja_templates(app, docname, source):
342
388
# # relative to this directory. They are copied after the builtin static files,
343
389
# # so a file named "default.css" will overwrite the builtin "default.css".
344
390
html_static_path = ["_static" ]
345
- html_js_files = [' searchindex.js' ]
391
+ html_js_files = [" searchindex.js" ]
346
392
# # Custom sidebar templates, maps document names to template names.
347
393
# html_sidebars = {
348
394
# 'index': ['sidebarlogo.html', 'globaltoc.html', 'searchbox.html', 'sourcelink.html'],
@@ -413,3 +459,4 @@ def handle_jinja_templates(app, docname, source):
413
459
414
460
def setup (app ):
415
461
app .connect ("source-read" , handle_jinja_templates )
462
+ app .connect ("html-page-context" , fix_gallery_edit_links )
0 commit comments