@@ -158,6 +158,8 @@ def wrapper(*args, **kwargs):
158158 "author" : "PyTorch Contributors" ,
159159}
160160
161+
162+
161163# -- Sphinx-gallery configuration --------------------------------------------
162164
163165sphinx_gallery_conf = {
@@ -187,6 +189,11 @@ def wrapper(*args, **kwargs):
187189 },
188190}
189191
192+ html_additional_pages = {
193+ "404" : "404.html" ,
194+ }
195+
196+
190197html_baseurl = "https://pytorch.org/tutorials/" # needed for sphinx-sitemap
191198sitemap_locales = [None ]
192199sitemap_excludes = [
@@ -259,6 +266,22 @@ def wrapper(*args, **kwargs):
259266 r"/(?!" + re .escape (os .getenv ("GALLERY_PATTERN" )) + r")[^/]+$"
260267 )
261268
269+ # Add 404.html to each gallery directory and all subdirectories
270+ for i , gallery_dir in enumerate (sphinx_gallery_conf ["gallery_dirs" ]):
271+ # Add 404.html to the main gallery directory
272+ html_additional_pages [f"{ gallery_dir } /404" ] = "404.html"
273+
274+ # Add 404.html to all subdirectories in the corresponding source directory
275+ source_dir = Path (sphinx_gallery_conf ["examples_dirs" ][i ])
276+ if source_dir .exists ():
277+ # Find all subdirectories in the source directory
278+ for subdir in source_dir .rglob ("*" ):
279+ if subdir .is_dir () and subdir != source_dir :
280+ # Calculate the relative path and corresponding gallery path
281+ relative_path = subdir .relative_to (source_dir )
282+ gallery_subdir = f"{ gallery_dir } /{ relative_path } "
283+ html_additional_pages [f"{ gallery_subdir } /404" ] = "404.html"
284+
262285for i in range (len (sphinx_gallery_conf ["examples_dirs" ])):
263286 gallery_dir = Path (sphinx_gallery_conf ["gallery_dirs" ][i ])
264287 source_dir = Path (sphinx_gallery_conf ["examples_dirs" ][i ])
0 commit comments