Skip to content

Commit cf8da85

Browse files
committed
feat: Move download link to sidebar
1 parent b10da66 commit cf8da85

11 files changed

+50
-199
lines changed

docs/_static/custom.css

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
/* Change colour of sphinx-gallery download buttons */
2-
div.sphx-glr-download a {
3-
background-color: #eef5fa !important;
4-
background-image: none !important;
5-
border: 1px solid #c9c9c9 !important;
1+
/* Hide sphinx-gallery signature */
2+
p.sphx-glr-signature a.reference.external {
3+
font-size: 0% !important;
64
}
75

8-
/* Change size of sphinx-gallery thumbnails */
9-
.sphx-glr-thumbcontainer {
10-
min-width: 250px !important;
11-
margin: 20px !important;
12-
padding-bottom: 50px !important;
6+
/* backreference links: restore hover decoration that SG removes */
7+
a.sphx-glr-backref-instance:hover {
8+
text-decoration: underline;
139
}
14-
.sphx-glr-thumbcontainer img {
15-
max-height: 250px !important;
16-
width: 250px !important;
10+
/* backreference links: make non-MRI-NUFFT func/meth calls resemble regular code */
11+
a[class^="sphx-glr-backref-module"] {
12+
color: var(--pst-color-text-base);
1713
}
18-
.sphx-glr-thumbcontainer a.internal {
19-
padding: 180px 10px 0 !important;
14+
/* backreference links: make MRI-NUFFT calls bold and colorful */
15+
a[class^="sphx-glr-backref-module-mrinufft"] {
16+
color: var(--pst-color-link);
17+
font-weight: var(--mne-font-weight-semibold);
2018
}
2119

22-
/* Hide sphinx-gallery signature */
23-
p.sphx-glr-signature a.reference.external {
24-
font-size: 0% !important;
20+
/* hide download link note */
21+
22+
.sphx-glr-download-link-note, /* Download link note in header */
23+
.binder-badge, /* Binder launch badge in footer */
24+
.lite-badge, /* Lite launch badge in footer */
25+
.sphx-glr-download-jupyter, /* Download Jupyter notebook link in footer */
26+
.sphx-glr-download-python, /* Download Python script link in footer */
27+
.sphx-glr-download-zip /* Download zipped link in footer */
28+
{
29+
display: none;
2530
}

docs/_templates/colab_link.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% set colab_link = get_colab_link() %}
2+
3+
<a href="{{ colab_link }}" target="_blank">
4+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"/>
5+
</a>
6+
7+

docs/conf.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"within_subsection_order": "ExampleTitleSortKey",
9090
"filename_pattern": "/example_",
9191
"ignore_pattern": r"(__init__|conftest|utils).py",
92-
"prefer_full_module": {r".*"},
92+
"prefer_full_module": {r"mrinufft"},
9393
"nested_sections": True,
9494
"binder": {
9595
"org": "mind-inria",
@@ -105,6 +105,9 @@
105105
},
106106
"parallel": True,
107107
"matplotlib_animations": (True, "mp4"),
108+
"first_notebook_cell": (
109+
"!pip install mri-nufft[cufinufft,finufft,gpunufft,extra,autodiff]"
110+
), # for binder and colab
108111
}
109112

110113
intersphinx_mapping = {
@@ -134,6 +137,14 @@
134137

135138
html_theme_options = {
136139
"use_edit_page_button": True,
140+
"secondary_sidebar_items": {
141+
"generated/autoexamples/**": [
142+
"page-toc",
143+
"sg_download_links",
144+
"sg_launcher_links",
145+
"colab_link",
146+
],
147+
},
137148
"header_links_before_dropdown": 4,
138149
}
139150

docs/sphinx_add_colab_link.py

Lines changed: 8 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -6,157 +6,21 @@
66
import json
77

88

9-
class ColabLinkNode(nodes.General, nodes.Element):
10-
"""A custom docutils node to represent the Colab link."""
9+
def setup_colab_link_getter(app, pagename, templatename, context, doctree):
10+
"""Add a function to the HTML context to get the Colab link for a notebook."""
1111

12+
def get_colab_link() -> str:
13+
"""Assume that the notebook path is the same as the pagename"""
14+
return f"https://colab.research.google.com/github/mind-inria/mri-nufft/blob/colab-examples/examples/{pagename}.ipynb"
1215

13-
def visit_colab_link_node_html(self, node):
14-
self.body.append(node["html"])
15-
16-
17-
def depart_colab_link_node_html(self, node):
18-
pass
19-
20-
21-
class ColabLinkDirective(SphinxDirective):
22-
"""Directive to insert a link to open a notebook in Google Colab."""
23-
24-
has_content = True
25-
option_spec = {
26-
"needs_gpu": int,
27-
}
28-
29-
def run(self):
30-
"""Run the directive."""
31-
# Determine the path of the current .rst file
32-
rst_file_path = self.env.doc2path(self.env.docname)
33-
rst_file_dir = os.path.dirname(rst_file_path)
34-
35-
# Determine the notebook file path assuming it is in the same directory as the .rst file
36-
notebook_filename = os.path.basename(rst_file_path).replace(".rst", ".ipynb")
37-
38-
# Full path to the notebook
39-
notebook_full_path = os.path.join(rst_file_dir, notebook_filename)
40-
41-
# Convert the full path back to a relative path from the repo root
42-
# repo_root = self.config.project_root_dir
43-
notebook_repo_relative_path = os.path.relpath(
44-
notebook_full_path, os.path.join(os.getcwd(), "docs")
45-
)
46-
47-
# Generate the Colab URL based on GitHub repo information
48-
self.colab_url = f"https://colab.research.google.com/github/mind-inria/mri-nufft/blob/colab-examples/examples/{notebook_repo_relative_path}"
49-
50-
# Create the HTML button or link
51-
self.html = f"""<div class="colab-button">
52-
<a href="{self.colab_url}" target="_blank">
53-
<img src="https://colab.research.google.com/assets/colab-badge.svg"
54-
alt="Open In Colab"/>
55-
</a>
56-
</div>
57-
"""
58-
self.notebook_modifier(notebook_full_path, "\n".join(self.content))
59-
60-
# Create the node to insert the HTML
61-
node = ColabLinkNode(html=self.html)
62-
return [node]
63-
64-
def notebook_modifier(self, notebook_path, commands):
65-
"""Modify the notebook to add a warning about GPU requirement."""
66-
with open(notebook_path) as f:
67-
notebook = json.load(f)
68-
if "cells" not in notebook:
69-
notebook["cells"] = []
70-
71-
# Add a cell to install the required libraries at the position where we have
72-
# colab link
73-
idx = self.find_index_of_colab_link(notebook)
74-
code_lines = ["# Install libraries"]
75-
code_lines.append(commands)
76-
code_lines.append("!pip install brainweb-dl # Required for data")
77-
dummy_notebook_content = {"cells": []}
78-
add_code_cell(
79-
dummy_notebook_content,
80-
"\n".join(code_lines),
81-
)
82-
notebook["cells"][idx] = dummy_notebook_content["cells"][0]
83-
84-
needs_GPU = self.options.get("needs_gpu", False)
85-
if needs_GPU:
86-
# Add a warning cell at the top of the notebook
87-
warning_template = "\n".join(
88-
[
89-
"<div class='alert alert-{message_class}'>",
90-
"",
91-
"# Need GPU warning",
92-
"",
93-
"{message}",
94-
"</div>",
95-
self.html,
96-
]
97-
)
98-
message_class = "warning"
99-
message = (
100-
"Running this mri-nufft example requires a GPU, and hence is NOT "
101-
"possible on binder currently We request you to kindly run this notebook "
102-
"on Google Colab by clicking the link below. Additionally, please make "
103-
"sure to set the runtime on Colab to use a GPU and install the below "
104-
"libraries before running."
105-
)
106-
idx = 0
107-
else:
108-
# Add a warning cell at the top of the notebook
109-
warning_template = "\n".join(
110-
[
111-
"<div class='alert alert-{message_class}'>",
112-
"",
113-
"# Install libraries needed for Colab",
114-
"",
115-
"{message}",
116-
"</div>",
117-
self.html,
118-
]
119-
)
120-
message_class = "info"
121-
message = (
122-
"The below installation commands are needed to be run only on "
123-
"Google Colab."
124-
)
125-
126-
dummy_notebook_content = {"cells": []}
127-
add_markdown_cell(
128-
dummy_notebook_content,
129-
warning_template.format(message_class=message_class, message=message),
130-
)
131-
notebook["cells"] = (
132-
notebook["cells"][:idx]
133-
+ dummy_notebook_content["cells"]
134-
+ notebook["cells"][idx:]
135-
)
136-
137-
# Write back updated notebook
138-
with open(notebook_path, "w", encoding="utf-8") as f:
139-
json.dump(notebook, f, ensure_ascii=False, indent=2)
140-
141-
def find_index_of_colab_link(self, notebook):
142-
"""Find the index of the cell containing the Colab link."""
143-
for idx, cell in enumerate(notebook["cells"]):
144-
if cell["cell_type"] == "markdown" and ".. colab-link::" in "".join(
145-
cell.get("source", "")
146-
):
147-
return idx
148-
return 0
16+
context["get_colab_link"] = get_colab_link
14917

15018

15119
def setup(app):
152-
"""Set up the Sphinx extension."""
153-
app.add_node(
154-
ColabLinkNode, html=(visit_colab_link_node_html, depart_colab_link_node_html)
155-
)
156-
app.add_directive("colab-link", ColabLinkDirective)
20+
app.connect("html-page-context", setup_colab_link_getter)
15721

15822
return {
159-
"version": "0.1",
23+
"version": "0.4",
16024
"parallel_read_safe": True,
16125
"parallel_write_safe": True,
16226
}

examples/GPU/example_density.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
of the operator roughly equal to 1.
1212
1313
"""
14-
# %%
15-
# .. colab-link::
16-
# :needs_gpu: 1
17-
#
18-
# !pip install mri-nufft[gpunufft] finufft
1914

2015
# %%
2116
# Imports

examples/GPU/example_fastMRI_UNet.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
.. warning::
3434
We train on a single image here. In practice, this should be done on a database like fastMRI [fastmri]_.
3535
"""
36-
# %%
37-
# .. colab-link::
38-
# :needs_gpu: 1
39-
#
40-
# !pip install mri-nufft[gpunufft] scikit-image fastmri
4136

4237
# %%
4338
# Imports

examples/GPU/example_learn_samples.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
This example only showcases the autodiff capabilities, the learned sampling pattern is not scanner compliant as the scanner gradients required to implement it violate the hardware constraints. In practice, a projection :math:`\Pi_\mathcal{Q}(\mathbf{K})` into the scanner constraints set :math:`\mathcal{Q}` is recommended (see [Proj]_). This is implemented in the proprietary SPARKLING package [Sparks]_. Users are encouraged to contact the authors if they want to use it.
2121
"""
2222

23-
# %%
24-
# .. colab-link::
25-
# :needs_gpu: 1
26-
#
27-
# !pip install mri-nufft[gpunufft] scikit-image
2823

2924
import os
3025

examples/GPU/example_learn_samples_multicoil.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
.. warning::
2727
This example only showcases the autodiff capabilities, the learned sampling pattern is not scanner compliant as the scanner gradients required to implement it violate the hardware constraints. In practice, a projection :math:`\Pi_\mathcal{Q}(\mathbf{K})` into the scanner constraints set :math:`\mathcal{Q}` is recommended (see [Proj]_). This is implemented in the proprietary SPARKLING package [Sparks]_. Users are encouraged to contact the authors if they want to use it.
2828
"""
29-
# %%
30-
# .. colab-link::
31-
# :needs_gpu: 1
32-
#
33-
# !pip install mri-nufft[gpunufft] cufinufft sigpy scikit-image
3429

3530
# %%
3631
# Imports

examples/GPU/example_learn_straight_line_readouts.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
2020
"""
2121

22-
# %%
23-
# .. colab-link::
24-
# :needs_gpu: 1
25-
#
26-
# !pip install mri-nufft[gpunufft]
27-
2822
# %%
2923
# Imports
3024
# -------

examples/GPU/example_pinv.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
1111
"""
1212

13-
# %%
14-
# .. colab-link::
15-
# :needs_gpu: 1
16-
#
17-
# !pip install mri-nufft[gpunufft] scikit-image
18-
1913
import os
2014
import time
2115

0 commit comments

Comments
 (0)