Skip to content

Commit bbe196c

Browse files
committed
Add a condition for tutorials
1 parent c80ab8c commit bbe196c

File tree

7 files changed

+117
-88
lines changed

7 files changed

+117
-88
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ dist/
1212
scss/vendor/*
1313
*.pyc
1414
docs/auto_examples/*
15+
docs/sg_execution_times.rst

docs/conf.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
sys.path.insert(0, os.path.abspath(".."))
55

66
import pytorch_sphinx_theme2
7-
import torch
7+
# import torch # Temporarily commented out for testing
88

99
html_theme = "pytorch_sphinx_theme2"
1010
html_theme_path = [pytorch_sphinx_theme2.get_html_theme_path()]
@@ -41,12 +41,15 @@
4141
sphinx_gallery_conf = {
4242
"examples_dirs": "examples", # path to your example scripts
4343
"gallery_dirs": "auto_examples", # where to save gallery generated output
44-
"filename_pattern": "/example_", # Only run files that match this pattern
44+
"filename_pattern": "/(example_|template_)", # Process files starting with example_ or template_
4545
"plot_gallery": True, # Generate plots for examples
46+
"promote_jupyter_magic": True,
47+
"first_notebook_cell": ("%matplotlib inline"),
4648
}
4749

4850
# pytorch_project = "tutorials"
49-
torch_version = str(torch.__version__)
51+
# torch_version = str(torch.__version__) # Temporarily commented out for testing
52+
torch_version = "2.0.0" # Hardcoded for testing
5053
version = "main (" + torch_version + " )"
5154
# The full version, including alpha/beta/rc tags.
5255
release = "main"
@@ -58,7 +61,7 @@
5861
# Note: the release candidates should no longer have the aHASH suffix, but in any
5962
# case we wish to leave only major.minor, even for rc builds.
6063
version = ".".join(torch_version.split(".")[:2])
61-
html_title = " ".join((project, version, "documentation"))
64+
html_title = " ".join(("PyTorch Sphinx Theme2", version, "documentation"))
6265
release = version
6366

6467
# Do not warn about external images (status badges in README.rst)
@@ -105,6 +108,26 @@
105108

106109
html_title = " ".join((project, version, "documentation"))
107110

111+
# Tutorial repository configuration using existing html_context variables
112+
theme_variables = pytorch_sphinx_theme2.get_theme_variables()
113+
html_context = {
114+
"theme_variables": theme_variables,
115+
"library_links": theme_variables.get("library_links", []),
116+
"feedback_url": "https://github.com/pytorch/pytorch_sphinx_theme",
117+
"github_url": "https://github.com",
118+
"github_user": "pytorch",
119+
"github_repo": "tutorials", # Changed to tutorials for tutorial buttons
120+
"github_version": "main", # Changed to main branch for tutorials
121+
"colab_branch": "gh-pages", # New variable for Colab branch
122+
"doc_path": "docs",
123+
"extra_project_links": theme_variables.get("extra_project_links", []),
124+
"date_info": {
125+
"paths_to_skip": ["installing"],
126+
},
127+
"version": version,
128+
"has_sphinx_gallery": True,
129+
}
130+
108131
# There are two options for replacing |today|: either, you set today to some
109132
# non-false value, then it is used:
110133
# today = ''
@@ -201,25 +224,10 @@
201224
# },
202225
# "canonical_url": "https://pytorch.org/docs/stable/",
203226
"canonical_url": "http://localhost:8000",
204-
"pytorch_project": "tutorials",
205-
}
227+
#"pytorch_project": "tutorials",
228+
}
229+
206230

207-
theme_variables = pytorch_sphinx_theme2.get_theme_variables()
208-
html_context = {
209-
"theme_variables": theme_variables,
210-
"library_links": theme_variables.get("library_links", []),
211-
"feedback_url": "https://github.com/pytorch/pytorch_sphinx_theme",
212-
"github_url": "https://github.com",
213-
"github_user": "pytorch",
214-
"github_repo": "pytorch_sphinx_theme",
215-
"github_version": "master",
216-
"doc_path": "docs",
217-
"extra_project_links": theme_variables.get("extra_project_links", []),
218-
"date_info": {
219-
"paths_to_skip": ["installing"],
220-
},
221-
"version": version,
222-
}
223231
# Add any paths that contain custom themes here, relative to this directory.
224232
html_theme_path = ["../"]
225233

docs/sg_execution_times.rst

Lines changed: 0 additions & 37 deletions
This file was deleted.

pytorch_sphinx_theme2/static/js/theme.js

Lines changed: 22 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pytorch_sphinx_theme2/static/js/tutorial-buttons-call-to-action.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,22 @@ document.addEventListener('DOMContentLoaded', function() {
77
var tutorialUrlArray = $("#tutorial-type").text().split('/');
88
tutorialUrlArray[0] = tutorialUrlArray[0] + "_source"
99

10-
var githubLink = "https://github.com/pytorch/tutorials/blob/main/" + tutorialUrlArray.join("/") + ".py";
10+
// Get configurable repository settings from conf.py
11+
12+
// Default to PyTorch tutorials for backward compatibility
13+
var defaultGithubRepo = "pytorch/tutorials";
14+
var defaultGithubBranch = "main";
15+
var defaultColabRepo = "pytorch/tutorials";
16+
var defaultColabBranch = "gh-pages";
17+
18+
// Use configured values from window.repoConfig or fallback to defaults
19+
// This ensures backward compatibility when tutorial_repo_config is not defined
20+
var githubRepo = (window.repoConfig && window.repoConfig.github_repo) || defaultGithubRepo;
21+
var githubBranch = (window.repoConfig && window.repoConfig.github_branch) || defaultGithubBranch;
22+
var colabRepo = (window.repoConfig && window.repoConfig.colab_repo) || defaultColabRepo;
23+
var colabBranch = (window.repoConfig && window.repoConfig.colab_branch) || defaultColabBranch;
24+
25+
var githubLink = "https://github.com/" + githubRepo + "/blob/" + githubBranch + "/" + tutorialUrlArray.join("/") + ".py";
1126

1227
// Find the notebook download link by checking for .ipynb extension
1328
var notebookLinks = $(".reference.download");
@@ -24,11 +39,11 @@ document.addEventListener('DOMContentLoaded', function() {
2439
}
2540

2641
var notebookDownloadPath = notebookLink.split('_downloads')[1],
27-
colabLink = "https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads" + notebookDownloadPath;
42+
colabLink = "https://colab.research.google.com/github/" + colabRepo + "/blob/" + colabBranch + "/_downloads" + notebookDownloadPath;
2843

29-
$("#google-colab-link").wrap("<a href=" + colabLink + " data-behavior='call-to-action-event' data-response='Run in Google Colab' target='_blank'/>");
30-
$("#download-notebook-link").wrap("<a href=" + notebookLink + " data-behavior='call-to-action-event' data-response='Download Notebook'/>");
31-
$("#github-view-link").wrap("<a href=" + githubLink + " data-behavior='call-to-action-event' data-response='View on Github' target='_blank'/>");
44+
$("#colab-link").attr("href", colabLink);
45+
$("#notebook-link").attr("href", notebookLink);
46+
$("#github-link").attr("href", githubLink);
3247

3348
// Hide the original download links and signature
3449
$(".sphx-glr-footer").hide();

pytorch_sphinx_theme2/templates/layout.html

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,21 @@
2929
});
3030
</script>
3131
<!-- Conditional CSS for header and footer height adjustment -->
32-
{% if not theme_show_lf_header or not theme_show_lf_footer %}
32+
{% if not theme_show_lf_header %}
3333
<style>
34-
{
35-
% if not theme_show_lf_header %
36-
}
37-
3834
:root {
3935
--header-height: 0px !important;
4036
--header-height-desktop: 0px !important;
4137
}
42-
43-
{
44-
% endif %
45-
}
46-
47-
{
48-
% if not theme_show_lf_footer %
49-
}
50-
38+
</style>
39+
{% endif %}
40+
{% if not theme_show_lf_footer %}
41+
<style>
5142
@media (min-width: 1100px) {
5243
.site-footer {
5344
height: 300px !important;
5445
}
5546
}
56-
57-
{
58-
% endif %
59-
}
6047
</style>
6148
{% endif %}
6249
<link rel="stylesheet" type="text/css" href="{{ pathto('_static/css/theme.css', 1) }}" crossorigin="anonymous">
@@ -68,7 +55,7 @@
6855
<link rel="stylesheet" href="{{ pathto('_static/webfonts/all.min.css', 1) }}" crossorigin="anonymous">
6956
<meta http-equiv="Content-Security-Policy"
7057
content="default-src * 'unsafe-inline' 'unsafe-eval' data: blob:; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval' blob:;">
71-
<meta name="pytorch_project" content="{{ pytorch_project }}">
58+
<meta name="pytorch_project" content="{{ theme_pytorch_project }}">
7259
<!-- Google Tag Manager (noscript) -->
7360
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={{ theme_analytics_id }}" height="0" width="0"
7461
style="display:none;visibility:hidden"></iframe></noscript>
@@ -116,6 +103,20 @@
116103
}
117104
</script>
118105
<!-- End Facebook Pixel Code -->
106+
<!-- Repository configuration for tutorials -->
107+
{% if (theme_pytorch_project == 'tutorials' or has_sphinx_gallery) and github_user and github_repo %}
108+
<script>
109+
// Define repository configuration for tutorial buttons using existing html_context variables
110+
// Only injected when tutorial buttons are shown AND github variables are defined
111+
// If either condition is false, JavaScript will fallback to default PyTorch tutorial links
112+
window.repoConfig = {
113+
github_repo: "{{ github_user }}/{{ github_repo }}",
114+
github_branch: "{{ github_version }}",
115+
colab_repo: "{{ github_user }}/{{ github_repo }}",
116+
colab_branch: "{{ colab_branch }}"
117+
};
118+
</script>
119+
{% endif %}
119120
<!-- Script to Fix scrolling -->
120121
<script>
121122
document.addEventListener('DOMContentLoaded', function () {

pytorch_sphinx_theme2_migration_guide.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,30 @@ If your site includes tutorials generated by sphinx-gallery, note that pytorch_s
279279
No additional configuration is needed for tutorial functionality that was previously handled through custom overrides.
280280
`custom_directives.py` along with various overrides in layout.html can be safely removed.
281281
282+
### Confgiuring Download links
283+
284+
The PyTorch Sphinx Theme 2 supports configurable tutorial repository
285+
links for the call-to-action buttons (GitHub, Colab, Download). This
286+
allows different projects to customize the repository links while
287+
maintaining backward compatibility.
288+
289+
290+
Define the repository configuration in your `conf.py`:
291+
292+
```python
293+
# Tutorial repository configuration
294+
295+
```
296+
html_context = {
297+
"github_repo": "your-org/your-repo",
298+
"github_branch": "main",
299+
"colab_repo": "your-org/your-colab-repo",
300+
"colab_branch": "gh-pages"
301+
}
302+
```
303+
304+
The `layout.html` template automatically injects the configuration as JavaScript:
305+
282306
## Using `sphinx.ext.linkcode`
283307
284308
We highly recommend that you configure `sphinx.ext.linkcode` on your site if you generate Python API

0 commit comments

Comments
 (0)