Skip to content

Commit c3db40a

Browse files
committed
Fix Edit on Github
1 parent 4b29ae9 commit c3db40a

File tree

1 file changed

+75
-26
lines changed

1 file changed

+75
-26
lines changed

conf.py

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,23 @@
3737
import torch
3838
import glob
3939
import random
40-
import re
4140
import shutil
4241
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
4546
import pandocfilters
46-
import plotly.io as pio
4747
import pypandoc
48-
49-
from get_sphinx_filenames import SPHINX_SHOULD_RUN
50-
48+
import plotly.io as pio
49+
from pathlib import Path
5150
pio.renderers.default = "sphinx_gallery"
5251

53-
import sphinx_gallery.gen_rst
5452
import multiprocessing
5553

54+
import sphinx_gallery.gen_rst
55+
56+
5657
# Monkey patch sphinx gallery to run each example in an isolated process so that
5758
# we don't need to worry about examples changing global state.
5859
#
@@ -72,12 +73,12 @@ def call_fn(func, args, kwargs, result_queue):
7273
except Exception as e:
7374
result_queue.put((False, str(e)))
7475

76+
7577
def call_in_subprocess(func):
7678
def wrapper(*args, **kwargs):
7779
result_queue = multiprocessing.Queue()
7880
p = multiprocessing.Process(
79-
target=call_fn,
80-
args=(func, args, kwargs, result_queue)
81+
target=call_fn, args=(func, args, kwargs, result_queue)
8182
)
8283
p.start()
8384
p.join()
@@ -86,9 +87,13 @@ def wrapper(*args, **kwargs):
8687
return result
8788
else:
8889
raise RuntimeError(f"Error in subprocess: {result}")
90+
8991
return wrapper
9092

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

9398
try:
9499
import torchvision
@@ -147,18 +152,26 @@ def wrapper(*args, **kwargs):
147152
# -- Sphinx-gallery configuration --------------------------------------------
148153

149154
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"],
162175
},
163176
}
164177

@@ -200,9 +213,9 @@ def wrapper(*args, **kwargs):
200213
},
201214
],
202215
"use_edit_page_button": True,
203-
#"logo": {
216+
# "logo": {
204217
# "text": "Home",
205-
#},
218+
# },
206219
"header_links_before_dropdown": 9,
207220
"navbar_start": ["pytorch_version"],
208221
"navbar_center": "navbar-nav",
@@ -257,6 +270,41 @@ def wrapper(*args, **kwargs):
257270
os.path.join(os.path.dirname(pytorch_sphinx_theme2.__file__), "templates"),
258271
]
259272

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+
260308
# The suffix(es) of source filenames.
261309
# You can specify multiple suffix as a list of string:
262310
#
@@ -337,7 +385,7 @@ def handle_jinja_templates(app, docname, source):
337385
# # relative to this directory. They are copied after the builtin static files,
338386
# # so a file named "default.css" will overwrite the builtin "default.css".
339387
html_static_path = ["_static"]
340-
html_js_files = ['searchindex.js']
388+
html_js_files = ["searchindex.js"]
341389
# # Custom sidebar templates, maps document names to template names.
342390
# html_sidebars = {
343391
# 'index': ['sidebarlogo.html', 'globaltoc.html', 'searchbox.html', 'sourcelink.html'],
@@ -408,3 +456,4 @@ def handle_jinja_templates(app, docname, source):
408456

409457
def setup(app):
410458
app.connect("source-read", handle_jinja_templates)
459+
app.connect("html-page-context", fix_gallery_edit_links)

0 commit comments

Comments
 (0)