Skip to content

Commit 4fb09a7

Browse files
committed
Fix Edit on Github
1 parent 2737ca9 commit 4fb09a7

File tree

1 file changed

+73
-26
lines changed

1 file changed

+73
-26
lines changed

conf.py

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,27 @@
3535
html_theme = "pytorch_sphinx_theme2"
3636
html_theme_path = [pytorch_sphinx_theme2.get_html_theme_path()]
3737

38-
import torch
38+
import distutils.file_util
3939
import glob
4040
import random
41+
import re
4142
import shutil
43+
from pathlib import Path
44+
4245
import numpy
4346
import pandocfilters
4447
import plotly.io as pio
4548
import pypandoc
4649
import torch
47-
import distutils.file_util
48-
import re
4950
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
5451

5552
pio.renderers.default = "sphinx_gallery"
5653

57-
import sphinx_gallery.gen_rst
5854
import multiprocessing
5955

56+
import sphinx_gallery.gen_rst
57+
58+
6059
# Monkey patch sphinx gallery to run each example in an isolated process so that
6160
# we don't need to worry about examples changing global state.
6261
#
@@ -76,12 +75,12 @@ def call_fn(func, args, kwargs, result_queue):
7675
except Exception as e:
7776
result_queue.put((False, str(e)))
7877

78+
7979
def call_in_subprocess(func):
8080
def wrapper(*args, **kwargs):
8181
result_queue = multiprocessing.Queue()
8282
p = multiprocessing.Process(
83-
target=call_fn,
84-
args=(func, args, kwargs, result_queue)
83+
target=call_fn, args=(func, args, kwargs, result_queue)
8584
)
8685
p.start()
8786
p.join()
@@ -90,9 +89,13 @@ def wrapper(*args, **kwargs):
9089
return result
9190
else:
9291
raise RuntimeError(f"Error in subprocess: {result}")
92+
9393
return wrapper
9494

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+
)
9699

97100
try:
98101
import torchvision
@@ -152,18 +155,26 @@ def wrapper(*args, **kwargs):
152155

153156

154157
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"],
167178
},
168179
}
169180

@@ -205,9 +216,9 @@ def wrapper(*args, **kwargs):
205216
},
206217
],
207218
"use_edit_page_button": True,
208-
#"logo": {
219+
# "logo": {
209220
# "text": "Home",
210-
#},
221+
# },
211222
"header_links_before_dropdown": 9,
212223
"navbar_start": ["pytorch_version"],
213224
"navbar_center": "navbar-nav",
@@ -262,6 +273,41 @@ def wrapper(*args, **kwargs):
262273
os.path.join(os.path.dirname(pytorch_sphinx_theme2.__file__), "templates"),
263274
]
264275

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+
265311
# The suffix(es) of source filenames.
266312
# You can specify multiple suffix as a list of string:
267313
#
@@ -342,7 +388,7 @@ def handle_jinja_templates(app, docname, source):
342388
# # relative to this directory. They are copied after the builtin static files,
343389
# # so a file named "default.css" will overwrite the builtin "default.css".
344390
html_static_path = ["_static"]
345-
html_js_files = ['searchindex.js']
391+
html_js_files = ["searchindex.js"]
346392
# # Custom sidebar templates, maps document names to template names.
347393
# html_sidebars = {
348394
# 'index': ['sidebarlogo.html', 'globaltoc.html', 'searchbox.html', 'sourcelink.html'],
@@ -413,3 +459,4 @@ def handle_jinja_templates(app, docname, source):
413459

414460
def setup(app):
415461
app.connect("source-read", handle_jinja_templates)
462+
app.connect("html-page-context", fix_gallery_edit_links)

0 commit comments

Comments
 (0)