29
29
#
30
30
import os
31
31
import sys
32
+
32
33
sys .path .insert (0 , os .path .abspath ("." ))
33
34
sys .path .insert (0 , os .path .abspath ("./.jenkins" ))
34
35
import pytorch_sphinx_theme2
36
+
35
37
html_theme = "pytorch_sphinx_theme2"
36
38
html_theme_path = [pytorch_sphinx_theme2 .get_html_theme_path ()]
37
- import torch
39
+ import distutils . file_util
38
40
import glob
39
41
import random
40
- import shutil
41
- import distutils .file_util
42
42
import re
43
- from get_sphinx_filenames import SPHINX_SHOULD_RUN
43
+ import shutil
44
+ from pathlib import Path
45
+
44
46
import pandocfilters
45
- import pypandoc
46
47
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
50
51
51
- import sphinx_gallery . gen_rst
52
+ pio . renderers . default = " sphinx_gallery"
52
53
import multiprocessing
53
54
55
+ import sphinx_gallery .gen_rst
56
+ from redirects import redirects
57
+
58
+
54
59
# Monkey patch sphinx gallery to run each example in an isolated process so that
55
60
# we don't need to worry about examples changing global state.
56
61
#
@@ -70,12 +75,12 @@ def call_fn(func, args, kwargs, result_queue):
70
75
except Exception as e :
71
76
result_queue .put ((False , str (e )))
72
77
78
+
73
79
def call_in_subprocess (func ):
74
80
def wrapper (* args , ** kwargs ):
75
81
result_queue = multiprocessing .Queue ()
76
82
p = multiprocessing .Process (
77
- target = call_fn ,
78
- args = (func , args , kwargs , result_queue )
83
+ target = call_fn , args = (func , args , kwargs , result_queue )
79
84
)
80
85
p .start ()
81
86
p .join ()
@@ -84,20 +89,29 @@ def wrapper(*args, **kwargs):
84
89
return result
85
90
else :
86
91
raise RuntimeError (f"Error in subprocess: { result } " )
92
+
87
93
return wrapper
88
94
95
+
89
96
# Windows does not support multiprocessing with fork and mac has issues with
90
97
# 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
+ )
93
106
94
107
try :
95
108
import torchvision
96
109
except ImportError :
97
110
import warnings
111
+
98
112
warnings .warn ('unable to load "torchvision" package' )
99
113
100
- rst_epilog = """
114
+ rst_epilog = """
101
115
.. |edit| image:: /_static/pencil-16.png
102
116
:width: 16px
103
117
:height: 16px
@@ -110,23 +124,23 @@ def wrapper(*args, **kwargs):
110
124
# needs_sphinx = '1.0'
111
125
112
126
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" ,
116
130
}
117
131
118
132
# Add any Sphinx extension module names here, as strings. They can be
119
133
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
120
134
# ones.
121
135
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" ,
130
144
]
131
145
132
146
intersphinx_mapping = {
@@ -151,22 +165,30 @@ def wrapper(*args, **kwargs):
151
165
# -- Sphinx-gallery configuration --------------------------------------------
152
166
153
167
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" ],
166
188
},
167
189
}
168
190
169
- html_baseurl = ' https://pytorch.org/tutorials/' # needed for sphinx-sitemap
191
+ html_baseurl = " https://pytorch.org/tutorials/" # needed for sphinx-sitemap
170
192
sitemap_locales = [None ]
171
193
sitemap_excludes = [
172
194
"search.html" ,
@@ -226,17 +248,19 @@ def wrapper(*args, **kwargs):
226
248
}
227
249
228
250
229
- if os .getenv (' GALLERY_PATTERN' ):
251
+ if os .getenv (" GALLERY_PATTERN" ):
230
252
# GALLERY_PATTERN is to be used when you want to work on a single
231
253
# tutorial. Previously this was fed into filename_pattern, but
232
254
# if you do that, you still end up parsing all of the other Python
233
255
# files which takes a few seconds. This strategy is better, as
234
256
# ignore_pattern also skips parsing.
235
257
# See https://github.com/sphinx-gallery/sphinx-gallery/issues/721
236
258
# 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
+ )
238
262
239
- for i in range (len (sphinx_gallery_conf [' examples_dirs' ])):
263
+ for i in range (len (sphinx_gallery_conf [" examples_dirs" ])):
240
264
gallery_dir = Path (sphinx_gallery_conf ["gallery_dirs" ][i ])
241
265
source_dir = Path (sphinx_gallery_conf ["examples_dirs" ][i ])
242
266
@@ -257,15 +281,15 @@ def wrapper(*args, **kwargs):
257
281
# You can specify multiple suffix as a list of string:
258
282
#
259
283
# source_suffix = ['.rst', '.md']
260
- source_suffix = ' .rst'
284
+ source_suffix = " .rst"
261
285
262
286
# The master toctree document.
263
- master_doc = ' index'
287
+ master_doc = " index"
264
288
265
289
# 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"
269
293
270
294
# The version info for the project you're documenting, acts as replacement for
271
295
# |version| and |release|, also used in various other places throughout the
@@ -281,23 +305,30 @@ def wrapper(*args, **kwargs):
281
305
#
282
306
# This is also used if you do content translation via gettext catalogs.
283
307
# Usually you set "language" from the command line for these cases.
284
- language = 'en'
308
+ language = "en"
285
309
286
310
# List of patterns, relative to source directory, that match files and
287
311
# directories to ignore when looking for source files.
288
312
# 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
+
292
322
293
323
# Handling for HuggingFace Hub jinja templates
294
324
def handle_jinja_templates (app , docname , source ):
295
325
if "huggingface_hub/templates" in docname :
296
326
# Replace Jinja templates with quoted strings
297
327
source [0 ] = re .sub (r"(\{\{.*?\}\})" , r'"\1"' , source [0 ])
298
328
329
+
299
330
# The name of the Pygments (syntax highlighting) style to use.
300
- pygments_style = ' sphinx'
331
+ pygments_style = " sphinx"
301
332
302
333
# If true, `todo` and `todoList` produce output, else they produce nothing.
303
334
todo_include_todos = False
@@ -325,7 +356,7 @@ def handle_jinja_templates(app, docname, source):
325
356
# # Add any paths that contain custom static files (such as style sheets) here,
326
357
# # relative to this directory. They are copied after the builtin static files,
327
358
# # so a file named "default.css" will overwrite the builtin "default.css".
328
- html_static_path = [' _static' ]
359
+ html_static_path = [" _static" ]
329
360
330
361
# # Custom sidebar templates, maps document names to template names.
331
362
# html_sidebars = {
@@ -334,11 +365,10 @@ def handle_jinja_templates(app, docname, source):
334
365
# }
335
366
336
367
337
-
338
368
# -- Options for HTMLHelp output ------------------------------------------
339
369
340
370
# Output file base name for HTML help builder.
341
- htmlhelp_basename = ' PyTorchTutorialsdoc'
371
+ htmlhelp_basename = " PyTorchTutorialsdoc"
342
372
343
373
344
374
# -- Options for LaTeX output ---------------------------------------------
@@ -347,15 +377,12 @@ def handle_jinja_templates(app, docname, source):
347
377
# The paper size ('letterpaper' or 'a4paper').
348
378
#
349
379
# 'papersize': 'letterpaper',
350
-
351
380
# The font size ('10pt', '11pt' or '12pt').
352
381
#
353
382
# 'pointsize': '10pt',
354
-
355
383
# Additional stuff for the LaTeX preamble.
356
384
#
357
385
# 'preamble': '',
358
-
359
386
# Latex figure (float) alignment
360
387
#
361
388
# 'figure_align': 'htbp',
@@ -365,19 +392,21 @@ def handle_jinja_templates(app, docname, source):
365
392
# (source start file, target name, title,
366
393
# author, documentclass [howto, manual, or own class]).
367
394
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
+ ),
370
402
]
371
403
372
404
373
405
# -- Options for manual page output ---------------------------------------
374
406
375
407
# One entry per manual page. List of tuples
376
408
# (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 )]
381
410
382
411
383
412
# -- Options for Texinfo output -------------------------------------------
@@ -386,14 +415,48 @@ def handle_jinja_templates(app, docname, source):
386
415
# (source start file, target name, title, author,
387
416
# dir menu entry, description, category)
388
417
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
+ ),
392
427
]
393
428
394
429
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
+
397
459
398
460
def setup (app ):
399
461
app .connect ("source-read" , handle_jinja_templates )
462
+ app .connect ("html-page-context" , html_page_context )
0 commit comments