Skip to content

Commit d7dd5a7

Browse files
committed
Add new theme
1 parent 03afbb2 commit d7dd5a7

12 files changed

+273
-514
lines changed

add-cards.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import os
2+
3+
import sphinx.addnodes
4+
5+
from docutils import nodes
6+
from docutils.parsers.rst import Directive
7+
from sphinx.util import logging
8+
from sphinx.util.docutils import SphinxDirective
9+
10+
11+
logger = logging.getLogger(__name__)
12+
13+
14+
class AddCardsDirective(SphinxDirective):
15+
"""
16+
Directive to automatically add cards based on toctree entries.
17+
"""
18+
19+
has_content = False
20+
required_arguments = 0
21+
optional_arguments = 0
22+
final_argument_whitespace = False
23+
option_spec = {}
24+
25+
def run(self):
26+
env = self.env
27+
toctrees = env.tocs.get(env.docname, None)
28+
29+
if not toctrees:
30+
logger.warning(f"No toctrees found in document {env.docname}")
31+
return []
32+
33+
# Find all toctrees in the document
34+
all_cards_container = nodes.container()
35+
all_cards_container["classes"] = ["all-tutorial-cards"]
36+
37+
# Process each toctree
38+
for toctreenode in toctrees.traverse(addnodes.toctree):
39+
# Get caption
40+
caption = toctreenode.get("caption", "")
41+
42+
# Create section container
43+
section_container = nodes.container()
44+
section_container["classes"] = ["tutorial-section"]
45+
46+
# Add section title if caption exists
47+
if caption:
48+
title_node = nodes.paragraph()
49+
title_node["classes"] = ["tutorial-section-title"]
50+
title_node += nodes.Text(caption)
51+
section_container += title_node
52+
53+
# Create cards container
54+
cards_container = nodes.container()
55+
cards_container["classes"] = ["tutorial-cards-container"]
56+
57+
# Find all entries in this toctree
58+
for entry in toctreenode["entries"]:
59+
doc_name = entry[1]
60+
title = env.titles.get(doc_name, nodes.title()).astext() or doc_name
61+
62+
# Try to get description from the document
63+
description = ""
64+
doc_path = os.path.join(self.env.srcdir, doc_name + ".rst")
65+
if os.path.exists(doc_path):
66+
try:
67+
with open(doc_path, "r") as f:
68+
content = f.read()
69+
# Extract first paragraph after title as description
70+
lines = content.split("\n")
71+
for i, line in enumerate(lines):
72+
if i > 2 and line.strip() and not line.startswith(".."):
73+
description = line.strip()
74+
break
75+
except Exception as e:
76+
logger.warning(f"Error reading {doc_path}: {e}")
77+
78+
# Create card
79+
card = nodes.container()
80+
card["classes"] = ["tutorial-card"]
81+
82+
# Add link
83+
card_link = nodes.reference("", "")
84+
card_link["refuri"] = entry["refuri"]
85+
card_link["classes"] = ["card-link"]
86+
87+
# Add title
88+
title_node = nodes.paragraph()
89+
title_node["classes"] = ["card-title"]
90+
title_node += nodes.Text(title)
91+
card_link += title_node
92+
93+
# Add description if available
94+
if description:
95+
desc_node = nodes.paragraph()
96+
desc_node["classes"] = ["card-description"]
97+
desc_node += nodes.Text(
98+
description[:100] + "..."
99+
if len(description) > 100
100+
else description
101+
)
102+
card_link += desc_node
103+
104+
card += card_link
105+
cards_container += card
106+
107+
section_container += cards_container
108+
all_cards_container += section_container
109+
110+
return [all_cards_container]
111+
112+
113+
def setup(app):
114+
app.add_directive("add-cards", AddCardsDirective)
115+
app.add_css_file("tutorial_cards.css")
116+
117+
return {
118+
"version": "0.1",
119+
"parallel_read_safe": True,
120+
"parallel_write_safe": True,
121+
}

beginner_source/index.rst renamed to beginner_source/intro.rst

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ understand PyTorch's core functionality through step-by-step guidance.
1515
:includehidden:
1616
:caption: Learn the Basics
1717

18-
beginner/basics/intro
18+
basics/intro
1919

2020

2121
.. toctree::
@@ -24,17 +24,17 @@ understand PyTorch's core functionality through step-by-step guidance.
2424
:includehidden:
2525
:caption: Introduction to PyTorch - YouTube Series
2626

27-
beginner/introyt/introyt_index
27+
introyt/introyt_index
2828

2929
.. toctree::
3030
:maxdepth: 1
3131
:hidden:
3232
:includehidden:
3333
:caption: Learning PyTorch
3434

35-
beginner/deep_learning_60min_blitz
36-
beginner/pytorch_with_examples
37-
beginner/nn_tutorial
38-
intermediate/nlp_from_scratch_index
39-
intermediate/tensorboard_tutorial
40-
intermediate/pinmem_nonblock
35+
deep_learning_60min_blitz
36+
pytorch_with_examples
37+
nn_tutorial
38+
nlp_from_scratch_index
39+
tensorboard_tutorial
40+
pinmem_nonblock

conf.py

+3-27
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@
5050
import pypandoc
5151
import torch
5252

53-
from custom_directives import (
54-
CustomCalloutItemDirective,
55-
CustomCardItemDirective,
56-
CustomGalleryItemDirective,
57-
GalleryItemDirective,
58-
IncludeDirective,
59-
)
6053
from get_sphinx_filenames import SPHINX_SHOULD_RUN
6154

6255
pio.renderers.default = "sphinx_gallery"
@@ -97,6 +90,8 @@
9790
"sphinx_design",
9891
"sphinx_sitemap",
9992
"sphinxcontrib.mermaid",
93+
"add-cards",
94+
"pytorch_sphinx_theme2"
10095
]
10196

10297
myst_enable_extensions = [
@@ -166,20 +161,6 @@ def reset_seeds(gallery_conf, fname):
166161
"logo": {
167162
"text": "Home",
168163
},
169-
"language_bindings_links": [
170-
{
171-
"url": "https://pytorch.org/docs/stable/cpp_index.html",
172-
"name": "C++",
173-
},
174-
{
175-
"url": "https://pytorch.org/javadoc/",
176-
"name": "Javadoc",
177-
},
178-
{
179-
"url": "https://github.com/pytorch/multipy",
180-
"name": "torch.multiply",
181-
},
182-
],
183164
"icon_links": [
184165
{
185166
"name": "X",
@@ -205,6 +186,7 @@ def reset_seeds(gallery_conf, fname):
205186
"use_edit_page_button": True,
206187
"logo": {
207188
"text": "Home",
189+
"header_links_before_dropdown": 9,
208190
},
209191
}
210192

@@ -408,9 +390,3 @@ def handle_jinja_templates(app, docname, source):
408390

409391
def setup(app):
410392
app.connect("source-read", handle_jinja_templates)
411-
# Custom directives
412-
app.add_directive("includenodoc", IncludeDirective)
413-
app.add_directive("galleryitem", GalleryItemDirective)
414-
app.add_directive("customgalleryitem", CustomGalleryItemDirective)
415-
app.add_directive("customcarditem", CustomCardItemDirective)
416-
app.add_directive("customcalloutitem", CustomCalloutItemDirective)

0 commit comments

Comments
 (0)