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