Skip to content

Commit 54f23e3

Browse files
author
Aoife
committed
Add download button for Jupyter notebooks in tutorial pages
1 parent 9b10aed commit 54f23e3

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jobs:
8383
sh assets/scripts/versions.sh
8484
8585
- name: Render Quarto site
86-
run: quarto render
86+
run: quarto render --to html,ipynb
8787

8888
- name: Rename original search index
8989
run: mv _site/search.json _site/search_original.json

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ venv
2828
site_libs
2929
.DS_Store
3030
index_files
31-
digest.txt
31+
digest.txt
32+
**/*.quarto_ipynb

_includes/download-notebook.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<script>
2+
document.addEventListener('DOMContentLoaded', function() {
3+
// Only add button if we're on a tutorial page
4+
const path = window.location.pathname;
5+
if (path.includes('/tutorials/') || path.includes('/usage/')) {
6+
// Extract the notebook name from the URL
7+
const pathParts = path.split('/');
8+
const section = pathParts[pathParts.length - 2];
9+
10+
// Create download button
11+
const downloadBtn = document.createElement('a');
12+
downloadBtn.className = 'btn btn-primary download-notebook-btn';
13+
downloadBtn.innerHTML = '<i class="bi bi-download"></i> Download as Notebook';
14+
15+
// Generate GitHub raw URL for the notebook
16+
const baseUrl = 'https://raw.githubusercontent.com/TuringLang/docs/main';
17+
const notebookPath = path.replace('.html', '.ipynb');
18+
downloadBtn.href = baseUrl + notebookPath;
19+
downloadBtn.download = section + '.ipynb';
20+
21+
// Insert button after title or in TOC area
22+
const tocTitle = document.querySelector('.toc-title');
23+
if (tocTitle) {
24+
tocTitle.parentNode.insertBefore(downloadBtn, tocTitle.nextSibling);
25+
} else {
26+
const articleHeader = document.querySelector('header.quarto-title-block');
27+
if (articleHeader) {
28+
articleHeader.appendChild(downloadBtn);
29+
}
30+
}
31+
}
32+
});
33+
</script>
34+
35+
<style>
36+
.download-notebook-btn {
37+
display: inline-block;
38+
margin: 1rem 0;
39+
padding: 0.5rem 1rem;
40+
background-color: var(--bs-primary);
41+
color: white;
42+
text-decoration: none;
43+
border-radius: 0.25rem;
44+
font-size: 0.9rem;
45+
}
46+
47+
.download-notebook-btn:hover {
48+
background-color: var(--bs-primary-dark);
49+
color: white;
50+
text-decoration: none;
51+
}
52+
53+
.download-notebook-btn i {
54+
margin-right: 0.5rem;
55+
}
56+
</style>

_quarto.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ format:
153153
toc-title: "Table of Contents"
154154
code-fold: false
155155
code-overflow: scroll
156+
code-tools:
157+
source: true
158+
toggle: false
159+
caption: none
160+
downloads: [ipynb]
156161
include-in-header:
157162
- text: |
158163
<style>
@@ -163,6 +168,7 @@ format:
163168
text-decoration: underline;
164169
}
165170
</style>
171+
- _includes/download-notebook.html
166172
include-after-body:
167173
- text: |
168174
<footer class="custom-footer">

0 commit comments

Comments
 (0)