Skip to content

Commit 2a396e4

Browse files
author
AoifeHughes
committed
patched to insert pkg local usage over global.
1 parent e06a333 commit 2a396e4

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

assets/scripts/qmd_to_ipynb.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,18 @@ def parse(self) -> None:
6767
code_lines.append(lines[i])
6868
i += 1
6969

70-
# Add code cell (with options as comments at the top)
71-
full_code = cell_options + code_lines
72-
self._add_code_cell(full_code, lang)
70+
# Check if this is the Pkg.instantiate() cell that we want to skip
71+
code_content = '\n'.join(code_lines).strip()
72+
is_pkg_instantiate = (
73+
'using Pkg' in code_content and
74+
'Pkg.instantiate()' in code_content and
75+
len(code_content.split('\n')) <= 3 # Only skip if it's just these lines
76+
)
77+
78+
# Add code cell (with options as comments at the top) unless it's the Pkg.instantiate cell
79+
if not is_pkg_instantiate:
80+
full_code = cell_options + code_lines
81+
self._add_code_cell(full_code, lang)
7382

7483
i += 1 # Skip closing ```
7584
else:
@@ -129,8 +138,20 @@ def _add_code_cell(self, lines: List[str], lang: str) -> None:
129138

130139
def to_notebook(self) -> Dict[str, Any]:
131140
"""Convert parsed cells to Jupyter notebook format."""
141+
# Add package activation cell at the top for Julia notebooks
142+
cells = self.cells
143+
if self.kernel_name.startswith("julia"):
144+
pkg_cell = {
145+
"cell_type": "code",
146+
"execution_count": None,
147+
"metadata": {},
148+
"outputs": [],
149+
"source": "using Pkg; Pkg.activate(; temp=true)"
150+
}
151+
cells = [pkg_cell] + self.cells
152+
132153
notebook = {
133-
"cells": self.cells,
154+
"cells": cells,
134155
"metadata": {
135156
"kernelspec": {
136157
"display_name": "Julia 1.11",

0 commit comments

Comments
 (0)