Skip to content

Commit 2e40fd5

Browse files
committed
Update
1 parent 1687503 commit 2e40fd5

File tree

1 file changed

+131
-68
lines changed

1 file changed

+131
-68
lines changed

conf.py

Lines changed: 131 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,33 @@
2929
#
3030
import os
3131
import sys
32+
3233
sys.path.insert(0, os.path.abspath("."))
3334
sys.path.insert(0, os.path.abspath("./.jenkins"))
3435
import pytorch_sphinx_theme2
36+
3537
html_theme = "pytorch_sphinx_theme2"
3638
html_theme_path = [pytorch_sphinx_theme2.get_html_theme_path()]
37-
import torch
39+
import distutils.file_util
3840
import glob
3941
import random
40-
import shutil
41-
import distutils.file_util
4242
import re
43-
from get_sphinx_filenames import SPHINX_SHOULD_RUN
43+
import shutil
44+
from pathlib import Path
45+
4446
import pandocfilters
45-
import pypandoc
4647
import plotly.io as pio
47-
from pathlib import Path
48-
pio.renderers.default = 'sphinx_gallery'
49-
from redirects import redirects
48+
import pypandoc
49+
import torch
50+
from get_sphinx_filenames import SPHINX_SHOULD_RUN
5051

51-
import sphinx_gallery.gen_rst
52+
pio.renderers.default = "sphinx_gallery"
5253
import multiprocessing
5354

55+
import sphinx_gallery.gen_rst
56+
from redirects import redirects
57+
58+
5459
# Monkey patch sphinx gallery to run each example in an isolated process so that
5560
# we don't need to worry about examples changing global state.
5661
#
@@ -70,12 +75,12 @@ def call_fn(func, args, kwargs, result_queue):
7075
except Exception as e:
7176
result_queue.put((False, str(e)))
7277

78+
7379
def call_in_subprocess(func):
7480
def wrapper(*args, **kwargs):
7581
result_queue = multiprocessing.Queue()
7682
p = multiprocessing.Process(
77-
target=call_fn,
78-
args=(func, args, kwargs, result_queue)
83+
target=call_fn, args=(func, args, kwargs, result_queue)
7984
)
8085
p.start()
8186
p.join()
@@ -84,20 +89,29 @@ def wrapper(*args, **kwargs):
8489
return result
8590
else:
8691
raise RuntimeError(f"Error in subprocess: {result}")
92+
8793
return wrapper
8894

95+
8996
# Windows does not support multiprocessing with fork and mac has issues with
9097
# fork so we do not monkey patch sphinx gallery to run in subprocesses.
91-
if os.getenv("TUTORIALS_ISOLATE_BUILD", "1") == "1" and not sys.platform.startswith("win") and not sys.platform == "darwin":
92-
sphinx_gallery.gen_rst.generate_file_rst = call_in_subprocess(sphinx_gallery.gen_rst.generate_file_rst)
98+
if (
99+
os.getenv("TUTORIALS_ISOLATE_BUILD", "1") == "1"
100+
and not sys.platform.startswith("win")
101+
and not sys.platform == "darwin"
102+
):
103+
sphinx_gallery.gen_rst.generate_file_rst = call_in_subprocess(
104+
sphinx_gallery.gen_rst.generate_file_rst
105+
)
93106

94107
try:
95108
import torchvision
96109
except ImportError:
97110
import warnings
111+
98112
warnings.warn('unable to load "torchvision" package')
99113

100-
rst_epilog ="""
114+
rst_epilog = """
101115
.. |edit| image:: /_static/pencil-16.png
102116
:width: 16px
103117
:height: 16px
@@ -110,23 +124,23 @@ def wrapper(*args, **kwargs):
110124
# needs_sphinx = '1.0'
111125

112126
html_meta = {
113-
'description': 'Master PyTorch with our step-by-step tutorials for all skill levels. Start your journey to becoming a PyTorch expert today!',
114-
'keywords': 'PyTorch, tutorials, Getting Started, deep learning, AI',
115-
'author': 'PyTorch Contributors'
127+
"description": "Master PyTorch with our step-by-step tutorials for all skill levels. Start your journey to becoming a PyTorch expert today!",
128+
"keywords": "PyTorch, tutorials, Getting Started, deep learning, AI",
129+
"author": "PyTorch Contributors",
116130
}
117131

118132
# Add any Sphinx extension module names here, as strings. They can be
119133
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
120134
# ones.
121135
extensions = [
122-
'sphinxcontrib.katex',
123-
'sphinx.ext.intersphinx',
124-
'sphinx_copybutton',
125-
'sphinx_gallery.gen_gallery',
126-
'sphinx_design',
127-
'sphinx_sitemap',
128-
'sphinx_reredirects',
129-
'sphinxcontrib.mermaid'
136+
"sphinxcontrib.katex",
137+
"sphinx.ext.intersphinx",
138+
"sphinx_copybutton",
139+
"sphinx_gallery.gen_gallery",
140+
"sphinx_design",
141+
"sphinx_sitemap",
142+
"sphinx_reredirects",
143+
"sphinxcontrib.mermaid",
130144
]
131145

132146
intersphinx_mapping = {
@@ -151,22 +165,30 @@ def wrapper(*args, **kwargs):
151165
# -- Sphinx-gallery configuration --------------------------------------------
152166

153167
sphinx_gallery_conf = {
154-
'examples_dirs': ['beginner_source', 'intermediate_source',
155-
'advanced_source', 'recipes_source', 'prototype_source'],
156-
'gallery_dirs': ['beginner', 'intermediate', 'advanced', 'recipes', 'prototype'],
157-
'filename_pattern': re.compile(SPHINX_SHOULD_RUN),
158-
'promote_jupyter_magic': True,
159-
'backreferences_dir': None,
160-
'first_notebook_cell': ("# For tips on running notebooks in Google Colab, see\n"
161-
"# https://pytorch.org/tutorials/beginner/colab\n"
162-
"%matplotlib inline"),
163-
'ignore_pattern': r'_torch_export_nightly_tutorial.py',
164-
'pypandoc': {'extra_args': ['--mathjax', '--toc'],
165-
'filters': ['.jenkins/custom_pandoc_filter.py'],
168+
"examples_dirs": [
169+
"beginner_source",
170+
"intermediate_source",
171+
"advanced_source",
172+
"recipes_source",
173+
"prototype_source",
174+
],
175+
"gallery_dirs": ["beginner", "intermediate", "advanced", "recipes", "prototype"],
176+
"filename_pattern": re.compile(SPHINX_SHOULD_RUN),
177+
"promote_jupyter_magic": True,
178+
"backreferences_dir": None,
179+
"first_notebook_cell": (
180+
"# For tips on running notebooks in Google Colab, see\n"
181+
"# https://pytorch.org/tutorials/beginner/colab\n"
182+
"%matplotlib inline"
183+
),
184+
"ignore_pattern": r"_torch_export_nightly_tutorial.py",
185+
"pypandoc": {
186+
"extra_args": ["--mathjax", "--toc"],
187+
"filters": [".jenkins/custom_pandoc_filter.py"],
166188
},
167189
}
168190

169-
html_baseurl = 'https://pytorch.org/tutorials/' # needed for sphinx-sitemap
191+
html_baseurl = "https://pytorch.org/tutorials/" # needed for sphinx-sitemap
170192
sitemap_locales = [None]
171193
sitemap_excludes = [
172194
"search.html",
@@ -226,17 +248,19 @@ def wrapper(*args, **kwargs):
226248
}
227249

228250

229-
if os.getenv('GALLERY_PATTERN'):
251+
if os.getenv("GALLERY_PATTERN"):
230252
# GALLERY_PATTERN is to be used when you want to work on a single
231253
# tutorial. Previously this was fed into filename_pattern, but
232254
# if you do that, you still end up parsing all of the other Python
233255
# files which takes a few seconds. This strategy is better, as
234256
# ignore_pattern also skips parsing.
235257
# See https://github.com/sphinx-gallery/sphinx-gallery/issues/721
236258
# for a more detailed description of the issue.
237-
sphinx_gallery_conf['ignore_pattern'] = r'/(?!' + re.escape(os.getenv('GALLERY_PATTERN')) + r')[^/]+$'
259+
sphinx_gallery_conf["ignore_pattern"] = (
260+
r"/(?!" + re.escape(os.getenv("GALLERY_PATTERN")) + r")[^/]+$"
261+
)
238262

239-
for i in range(len(sphinx_gallery_conf['examples_dirs'])):
263+
for i in range(len(sphinx_gallery_conf["examples_dirs"])):
240264
gallery_dir = Path(sphinx_gallery_conf["gallery_dirs"][i])
241265
source_dir = Path(sphinx_gallery_conf["examples_dirs"][i])
242266

@@ -257,15 +281,15 @@ def wrapper(*args, **kwargs):
257281
# You can specify multiple suffix as a list of string:
258282
#
259283
# source_suffix = ['.rst', '.md']
260-
source_suffix = '.rst'
284+
source_suffix = ".rst"
261285

262286
# The master toctree document.
263-
master_doc = 'index'
287+
master_doc = "index"
264288

265289
# General information about the project.
266-
project = 'PyTorch Tutorials'
267-
copyright = '2024, PyTorch'
268-
author = 'PyTorch contributors'
290+
project = "PyTorch Tutorials"
291+
copyright = "2024, PyTorch"
292+
author = "PyTorch contributors"
269293

270294
# The version info for the project you're documenting, acts as replacement for
271295
# |version| and |release|, also used in various other places throughout the
@@ -281,23 +305,30 @@ def wrapper(*args, **kwargs):
281305
#
282306
# This is also used if you do content translation via gettext catalogs.
283307
# Usually you set "language" from the command line for these cases.
284-
language = 'en'
308+
language = "en"
285309

286310
# List of patterns, relative to source directory, that match files and
287311
# directories to ignore when looking for source files.
288312
# This patterns also effect to html_static_path and html_extra_path
289-
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', 'src/pytorch-sphinx-theme/docs*']
290-
exclude_patterns += sphinx_gallery_conf['examples_dirs']
291-
exclude_patterns += ['*/index.rst']
313+
exclude_patterns = [
314+
"_build",
315+
"Thumbs.db",
316+
".DS_Store",
317+
"src/pytorch-sphinx-theme/docs*",
318+
]
319+
exclude_patterns += sphinx_gallery_conf["examples_dirs"]
320+
exclude_patterns += ["*/index.rst"]
321+
292322

293323
# Handling for HuggingFace Hub jinja templates
294324
def handle_jinja_templates(app, docname, source):
295325
if "huggingface_hub/templates" in docname:
296326
# Replace Jinja templates with quoted strings
297327
source[0] = re.sub(r"(\{\{.*?\}\})", r'"\1"', source[0])
298328

329+
299330
# The name of the Pygments (syntax highlighting) style to use.
300-
pygments_style = 'sphinx'
331+
pygments_style = "sphinx"
301332

302333
# If true, `todo` and `todoList` produce output, else they produce nothing.
303334
todo_include_todos = False
@@ -325,7 +356,7 @@ def handle_jinja_templates(app, docname, source):
325356
# # Add any paths that contain custom static files (such as style sheets) here,
326357
# # relative to this directory. They are copied after the builtin static files,
327358
# # so a file named "default.css" will overwrite the builtin "default.css".
328-
html_static_path = ['_static']
359+
html_static_path = ["_static"]
329360

330361
# # Custom sidebar templates, maps document names to template names.
331362
# html_sidebars = {
@@ -334,11 +365,10 @@ def handle_jinja_templates(app, docname, source):
334365
# }
335366

336367

337-
338368
# -- Options for HTMLHelp output ------------------------------------------
339369

340370
# Output file base name for HTML help builder.
341-
htmlhelp_basename = 'PyTorchTutorialsdoc'
371+
htmlhelp_basename = "PyTorchTutorialsdoc"
342372

343373

344374
# -- Options for LaTeX output ---------------------------------------------
@@ -347,15 +377,12 @@ def handle_jinja_templates(app, docname, source):
347377
# The paper size ('letterpaper' or 'a4paper').
348378
#
349379
# 'papersize': 'letterpaper',
350-
351380
# The font size ('10pt', '11pt' or '12pt').
352381
#
353382
# 'pointsize': '10pt',
354-
355383
# Additional stuff for the LaTeX preamble.
356384
#
357385
# 'preamble': '',
358-
359386
# Latex figure (float) alignment
360387
#
361388
# 'figure_align': 'htbp',
@@ -365,19 +392,21 @@ def handle_jinja_templates(app, docname, source):
365392
# (source start file, target name, title,
366393
# author, documentclass [howto, manual, or own class]).
367394
latex_documents = [
368-
(master_doc, 'PyTorchTutorials.tex', 'PyTorch Tutorials',
369-
'Sasank, PyTorch contributors', 'manual'),
395+
(
396+
master_doc,
397+
"PyTorchTutorials.tex",
398+
"PyTorch Tutorials",
399+
"Sasank, PyTorch contributors",
400+
"manual",
401+
),
370402
]
371403

372404

373405
# -- Options for manual page output ---------------------------------------
374406

375407
# One entry per manual page. List of tuples
376408
# (source start file, name, description, authors, manual section).
377-
man_pages = [
378-
(master_doc, 'pytorchtutorials', 'PyTorch Tutorials',
379-
[author], 1)
380-
]
409+
man_pages = [(master_doc, "pytorchtutorials", "PyTorch Tutorials", [author], 1)]
381410

382411

383412
# -- Options for Texinfo output -------------------------------------------
@@ -386,14 +415,48 @@ def handle_jinja_templates(app, docname, source):
386415
# (source start file, target name, title, author,
387416
# dir menu entry, description, category)
388417
texinfo_documents = [
389-
(master_doc, 'PyTorchTutorials', 'PyTorch Tutorials',
390-
author, 'PyTorchTutorials', 'One line description of project.',
391-
'Miscellaneous'),
418+
(
419+
master_doc,
420+
"PyTorchTutorials",
421+
"PyTorch Tutorials",
422+
author,
423+
"PyTorchTutorials",
424+
"One line description of project.",
425+
"Miscellaneous",
426+
),
392427
]
393428

394429
html_css_files = [
395-
'https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css',
396-
]
430+
"https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css",
431+
]
432+
433+
434+
def html_page_context(app, pagename, templatename, context, doctree):
435+
# Check if the page is in gallery directories
436+
for gallery_dir in sphinx_gallery_conf["gallery_dirs"]:
437+
if pagename.startswith(gallery_dir):
438+
# Get corresponding examples directory
439+
examples_dir = sphinx_gallery_conf["examples_dirs"][
440+
sphinx_gallery_conf["gallery_dirs"].index(gallery_dir)
441+
]
442+
443+
# Calculate relative path within the gallery
444+
rel_path = (
445+
pagename[len(gallery_dir) + 1 :] if pagename != gallery_dir else ""
446+
)
447+
448+
# Check for .py file in examples directory
449+
py_path = os.path.join(app.srcdir, examples_dir, rel_path + ".py")
450+
451+
# If a .py file exists, this page was generated from Python
452+
if os.path.exists(py_path):
453+
context["display_github"] = False
454+
return
455+
456+
# Enable for all other pages
457+
context["display_github"] = True
458+
397459

398460
def setup(app):
399461
app.connect("source-read", handle_jinja_templates)
462+
app.connect("html-page-context", html_page_context)

0 commit comments

Comments
 (0)