Skip to content

Commit 0355f82

Browse files
author
Aoife
committed
added better using statements
1 parent 6dcb42e commit 0355f82

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

assets/scripts/add_notebook_links.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ find _site/tutorials _site/usage _site/developers -name "index.html" 2>/dev/null
2828
relative_path="${html_file#_site/}"
2929
relative_path="${relative_path%/index.html}"
3030

31+
# Extract notebook name from parent folder
32+
notebook_name=$(basename "$relative_path")
33+
3134
# Construct Colab URL
3235
if [ -n "$COLAB_PATH_PREFIX" ]; then
3336
colab_url="https://colab.research.google.com/github/${COLAB_REPO}/blob/${COLAB_BRANCH}/${COLAB_PATH_PREFIX}/${relative_path}/index.ipynb"
3437
else
3538
colab_url="https://colab.research.google.com/github/${COLAB_REPO}/blob/${COLAB_BRANCH}/${relative_path}/index.ipynb"
3639
fi
3740

38-
# Insert both download and Colab links AFTER the "Report an issue" link
39-
# The download="index.ipynb" attribute forces browser to download instead of navigate
40-
perl -i -pe 's|(<a href="[^"]*issues/new"[^>]*><i class="bi[^"]*"></i>Report an issue</a></li>)|$1<li><a href="index.ipynb" class="toc-action" download="index.ipynb"><i class="bi bi-journal-code"></i>'"$DOWNLOAD_TEXT"'</a></li><li><a href="'"$colab_url"'" class="toc-action" target="_blank" rel="noopener"><i class="bi bi-google"></i>'"$COLAB_TEXT"'</a></li>|g' "$html_file"
41+
# Insert both download and Colab links BEFORE the "Edit this page" link
42+
# The download attribute forces browser to download with custom filename instead of navigate
43+
perl -i -pe 's|(<li><a href="[^"]*edit[^"]*"[^>]*><i class="bi[^"]*"></i>Edit this page</a></li>)|<li><a href="index.ipynb" class="toc-action" download="'"$notebook_name"'.ipynb"><i class="bi bi-journal-code"></i>'"$DOWNLOAD_TEXT"'</a></li><li><a href="'"$colab_url"'" class="toc-action" target="_blank" rel="noopener"><i class="bi bi-google"></i>'"$COLAB_TEXT"'</a></li>$1|g' "$html_file"
4144
echo " ✓ Added notebook links to $html_file"
4245
fi
4346
fi

assets/scripts/qmd_to_ipynb.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ def __init__(self, qmd_path: str):
1616
self.qmd_path = Path(qmd_path)
1717
self.cells: List[Dict[str, Any]] = []
1818
self.kernel_name = "julia" # Default kernel
19+
self.packages: set = set() # Track packages found in using statements
20+
21+
def _extract_packages_from_line(self, line: str) -> None:
22+
"""Extract package names from a 'using' statement and add to self.packages."""
23+
line = line.strip()
24+
if not line.startswith('using '):
25+
return
26+
27+
# Remove 'using ' prefix and any trailing semicolon/whitespace
28+
remainder = line[6:].rstrip(';').strip()
29+
30+
# Handle 'using Package: item1, item2' format - extract just the package name
31+
if ':' in remainder:
32+
package = remainder.split(':')[0].strip()
33+
if package and package != 'Pkg':
34+
self.packages.add(package)
35+
else:
36+
# Handle 'using Package1, Package2, ...' format
37+
packages = [pkg.strip() for pkg in remainder.split(',')]
38+
for pkg in packages:
39+
if pkg and pkg != 'Pkg':
40+
self.packages.add(pkg)
1941

2042
def parse(self) -> None:
2143
"""Parse the .qmd file and extract cells."""
@@ -113,6 +135,11 @@ def _add_markdown_cell(self, lines: List[str]) -> None:
113135

114136
def _add_code_cell(self, lines: List[str], lang: str) -> None:
115137
"""Add a code cell."""
138+
# Extract packages from Julia code cells
139+
if lang == 'julia':
140+
for line in lines:
141+
self._extract_packages_from_line(line)
142+
116143
content = '\n'.join(lines)
117144

118145
# For non-Julia code blocks (like dot/graphviz), add as markdown with code formatting
@@ -141,12 +168,19 @@ def to_notebook(self) -> Dict[str, Any]:
141168
# Add package activation cell at the top for Julia notebooks
142169
cells = self.cells
143170
if self.kernel_name.startswith("julia"):
171+
# Build the source code for the setup cell
172+
pkg_source_lines = ["using Pkg; Pkg.activate(; temp=true)"]
173+
174+
# Add Pkg.add() calls for each package found in the document
175+
for package in sorted(self.packages):
176+
pkg_source_lines.append(f'Pkg.add("{package}")')
177+
144178
pkg_cell = {
145179
"cell_type": "code",
146180
"execution_count": None,
147181
"metadata": {},
148182
"outputs": [],
149-
"source": "using Pkg; Pkg.activate(; temp=true)"
183+
"source": "\n".join(pkg_source_lines)
150184
}
151185
cells = [pkg_cell] + self.cells
152186

0 commit comments

Comments
 (0)