Skip to content

Commit 72941a0

Browse files
authored
Merge pull request #70 from TUW-GEO/fix-kernel
add set kernel to cookbook processing chain
2 parents 077ffb0 + 1eca6a7 commit 72941a0

7 files changed

Lines changed: 77 additions & 83 deletions

File tree

Makefile

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CONDA_ACTIVATE := source $(CONDA_ENV)/etc/profile.d/conda.sh ; \
1313
conda activate ; conda activate
1414
PREFIX = $(CURDIR)/.conda_envs
1515
CONDA_ENV_DIR := $(foreach i,$(REQ),$(PREFIX)/$(i))
16-
KERNEL_DIR != $(CONDA_ACTIVATE) eo-datascience; jupyter --data-dir
16+
KERNEL_DIR != jupyter --data-dir
1717
KERNEL_DIR := $(foreach i,$(REQ),$(KERNEL_DIR)/kernels/$(i))
1818

1919
help:
@@ -31,10 +31,6 @@ help:
3131
temporary files"
3232
@echo " make help - Display this help message"
3333

34-
$(CONDA_ENV)/envs/eo-datascience:
35-
- conda update -n base -c conda-forge conda -y
36-
conda env create --file environment.yml
37-
3834
$(CONDA_ENV_DIR):
3935
$(foreach f, $(YML), \
4036
conda env create --file $(f) \
@@ -49,7 +45,7 @@ $(KERNEL_DIR):
4945
python -m ipykernel install --user --name $(f) --display-name $(f); \
5046
conda deactivate; )
5147

52-
kernel: $(CONDA_ENV)/envs/eo-datascience $(CONDA_ENV_DIR) $(KERNEL_DIR)
48+
kernel: $(CONDA_ENV_DIR) $(KERNEL_DIR)
5349
@echo -e "jupyter kernels are ready."
5450

5551
post-render:
@@ -63,8 +59,7 @@ convert:
6359
quarto convert $(f); \
6460
mv $(subst .ipynb,.qmd, $(f)) $(subst notebooks,chapters,$(subst .ipynb,.qmd,$(f))); )
6561

66-
preview: $(CONDA_ENV)/envs/eo-datascience $(CONDA_ENV_DIR) $(KERNEL_DIR)
67-
$(CONDA_ACTIVATE) $(PREFIX)/eo-datascience
62+
preview: $(CONDA_ENV_DIR) $(KERNEL_DIR)
6863
- mkdir -p _preview/notebooks
6964
python -m pip install .
7065
cp ./chapters/references.bib ./_preview/notebooks/
@@ -73,8 +68,11 @@ preview: $(CONDA_ENV)/envs/eo-datascience $(CONDA_ENV_DIR) $(KERNEL_DIR)
7368
wget https://raw.githubusercontent.com/TUW-GEO/eo-datascience-cookbook/refs/heads/main/_config.yml -nc -P ./_preview
7469
wget https://raw.githubusercontent.com/TUW-GEO/eo-datascience-cookbook/refs/heads/main/notebooks/how-to-cite.md -nc -P ./_preview/notebooks
7570
render_sfinx_toc ./_preview
71+
merge_envs --out ./_preview/environment.yml --name eo-datascience-dev
72+
conda env create --file _preview/environment.yml --prefix $(PREFIX)/eo-datascience-cookbook-dev
73+
$(CONDA_ACTIVATE) $(PREFIX)/eo-datascience-cookbook-dev
74+
python -m ipykernel install --user --name eo-datascience-cookbook-dev --display-name eo-datascience-cookbook-dev
7675
clean_nb ./notebooks ./_preview/notebooks
77-
merge_envs --out environment.yml --name eo-datascience-dev
7876
jupyter-book build ./_preview
7977
jupyter-book build ./_preview
8078

@@ -85,7 +83,6 @@ clean:
8583
**/**/*.quarto_ipynb **/__pycache__ **/**/__pycache__ __pycache__
8684

8785
teardown:
88-
conda remove -n $(PREFIX)/eo-datascience --all -y
8986
$(foreach f, $(REQ), \
9087
$(CONDA_ACTIVATE) $(PREFIX)/$(f); \
9188
jupyter kernelspec uninstall -y $(f); \
@@ -95,4 +92,4 @@ teardown:
9592

9693
master:
9794
python -m pip install .
98-
merge_envs --out environment.yml --name eo-datascience-dev
95+
merge_envs --out environment.yml --name eo-datascience-cookbook-dev

environment.yml

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,14 @@ name: eo-datascience
22
channels:
33
- conda-forge
44
dependencies:
5-
- aiohttp
6-
- bokeh
7-
- cartopy
8-
- cmcrameri
9-
- dask
10-
- datashader
11-
- folium
12-
- geopandas
13-
- geoviews
14-
- graphviz
15-
- holoviews
16-
- huggingface_hub
17-
- hvplot
18-
- intake
19-
- intake-xarray
5+
- python=3.12
6+
- pip
7+
- mamba
208
- jupyter
21-
- jupyter-book
229
- jupyter-cache
23-
- jupyter_bokeh
24-
- mamba
25-
- matplotlib
2610
- nbformat
27-
- nbstripout
28-
- nodejs
29-
- numpy
30-
- odc-stac
31-
- openssl
32-
- pip
3311
- pre-commit
34-
- pyproj
35-
- pystac-client
12+
- nbstripout
3613
- pytest
37-
- python=3.12
38-
- rasterio
39-
- requests
40-
- rioxarray
41-
- scikit-image
42-
- scikit-learn
43-
- scipy
44-
- seaborn
45-
- snaphu
14+
- jupyter-book
4615
- sphinx-pythia-theme
47-
- stackstac
48-
- xarray
49-
- zarr=2.18.4

src/eo_datascience/clean_nb.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1-
import nbformat
2-
from pathlib import Path
3-
import re
41
import argparse
2+
import re
3+
from pathlib import Path
4+
5+
import nbformat
6+
7+
8+
def set_kernel_all_notebooks(
9+
dir="./notebooks", out=None, save=True, name="eo-datascience-cookbook-dev"
10+
):
11+
nb_paths = find_ipynb(dir)
12+
for nb_path in nb_paths:
13+
nb = nbformat.read(nb_path, as_version=4)
14+
nb.metadata.kernelspec.name = name
15+
nb.metadata.kernelspec.display_name = name
16+
# Save notebook
17+
nb_path = substitute_path(nb_path, dir, out)
18+
if save:
19+
nbformat.write(nb, nb_path)
20+
else:
21+
return nb
522

623

724
def clean_up_frontmatter(dir="./notebooks", out=None, save=True):
@@ -29,7 +46,7 @@ def clean_up_frontmatter(dir="./notebooks", out=None, save=True):
2946
i += 1
3047
line = fm[i]
3148

32-
new_text += fm[i + 1 :]
49+
new_text += fm[i + 1 :] # noqa
3350
nb.cells[0].source = "\n".join(new_text) + "\n"
3451
nb.cells[0].cell_type = "markdown"
3552
# Save notebook
@@ -44,8 +61,9 @@ def convert_bibliography(nb_path="./notebooks/references.ipynb", out=None, save=
4461
nb_path = Path(nb_path)
4562
if nb_path.exists():
4663
nb = nbformat.read(nb_path, as_version=4)
47-
nb.cells[0].source = \
48-
"""# References
64+
nb.cells[
65+
0
66+
].source = """# References
4967
```{bibliography}
5068
:style: plain
5169
```
@@ -150,6 +168,7 @@ def main():
150168
args = parser.parse_args()
151169

152170
clean_up_frontmatter(args.dir, args.out)
171+
set_kernel_all_notebooks(args.out)
153172
convert_callout_notes(args.out)
154173
convert_refs(args.out)
155174
convert_bibliography(out=args.out)

src/eo_datascience/merge_envs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def resolve_dependency_versions(
5757

5858

5959
def create_master_environment(
60-
final_dependencies: List | Set, name: str = "eo-datascience-dev"
60+
final_dependencies: List | Set, name: str = "eo-datascience-cookbook-dev"
6161
) -> Dict:
6262
master_env = {
6363
"name": name,
@@ -103,7 +103,7 @@ def main() -> None:
103103
"--name",
104104
type=str,
105105
help="Name of the environment",
106-
default="eo-datascience-dev",
106+
default="eo-datascience-cookbook-dev",
107107
)
108108
args = parser.parse_args()
109109

tests/mock.ipynb

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
11
{
22
"cells": [
33
{
4-
"cell_type": "raw",
4+
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
77
"---\n",
88
"title: This a mock Jupyter file\n",
99
"subtitle: We use it for testing\n",
1010
"author: anonymous\n",
11+
"jupyter:\n",
12+
" kernelspec:\n",
13+
" name: \"eo-datascience\"\n",
14+
" language: \"python\"\n",
15+
" display_name: \"eo-datascience\"\n",
1116
"---\n",
1217
"\n",
13-
"Some other text, which should not be deleted!"
14-
]
15-
},
16-
{
17-
"cell_type": "markdown",
18-
"metadata": {},
19-
"source": [
2018
"::: {.callout-note}\n",
2119
"This a callout note.\n",
22-
":::"
23-
]
24-
},
25-
{
26-
"cell_type": "markdown",
27-
"metadata": {},
28-
"source": [
29-
"lorem ipsum [@anon2024] and [@anon2025] and lorem ipsum @anon2024 and @anon2025"
20+
":::\n",
21+
"\n",
22+
"lorem ipsum [@anon2024] and [@anon2025] and lorem ipsum @anon2024 and @anon2025\n"
3023
]
3124
}
3225
],
3326
"metadata": {
34-
"language_info": {
35-
"name": "python"
27+
"kernelspec": {
28+
"display_name": "eo-datascience",
29+
"language": "python",
30+
"name": "eo-datascience"
3631
}
3732
},
3833
"nbformat": 4,
39-
"nbformat_minor": 2
34+
"nbformat_minor": 4
4035
}

tests/mock.qmd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
title: This a mock Jupyter file
33
subtitle: We use it for testing
44
author: anonymous
5-
jupyter: python3
5+
jupyter:
6+
kernelspec:
7+
name: "eo-datascience"
8+
language: "python"
9+
display_name: "eo-datascience"
610
---
711

812
::: {.callout-note}
913
This a callout note.
1014
:::
1115

1216
lorem ipsum [@anon2024] and [@anon2025] and lorem ipsum @anon2024 and @anon2025
13-

tests/test_quarto_nb_conversions.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
quarto_note_replace,
1212
quarto_ref_person_replace,
1313
quarto_ref_time_replace,
14+
set_kernel_all_notebooks,
1415
substitute_path,
1516
)
1617
from eo_datascience.render_sfinx_toc import (
@@ -95,10 +96,12 @@ def test_toc_conversion():
9596
"file": "notebooks/courses/microwave-remote-sensing",
9697
"sections": [
9798
{
98-
"file": "notebooks/courses/microwave-remote-sensing/01_in_class_exercise"
99+
"file": "notebooks/courses/microwave-remote-"
100+
+ "sensing/01_in_class_exercise"
99101
},
100102
{
101-
"file": "notebooks/courses/microwave-remote-sensing/02_in_class_exercise"
103+
"file": "notebooks/courses/microwave-remote-"
104+
+ "sensing/02_in_class_exercise"
102105
},
103106
],
104107
},
@@ -145,7 +148,8 @@ def test_remove_front_matter():
145148
)
146149
assert (
147150
clean_up_frontmatter("./tests", None, False)["cells"][0]["source"]
148-
== "# This a mock Jupyter file\n**We use it for testing**\n\nSome other text, which should not be deleted!\n"
151+
== "# This a mock Jupyter file\n**We use it for testing**\n\nSome"
152+
+ " other text, which should not be deleted!\n"
149153
)
150154

151155

@@ -174,11 +178,21 @@ def test_conversion_of_refs():
174178
]
175179
assert (
176180
convert_refs("./tests", None, False)["cells"][2]["source"]
177-
== r"lorem ipsum {cite:p}`anon2024` and {cite:p}`anon2025` and lorem ipsum {cite:t}`anon2024` and {cite:t}`anon2025`"
181+
== r"lorem ipsum {cite:p}`anon2024` and {cite:p}`anon2025` and lorem"
182+
+ " ipsum {cite:t}`anon2024` and {cite:t}`anon2025`"
178183
)
179184

180185

181186
def test_conversion_of_callout_notes():
182187
rst = ":::{note}\nThis a callout note.\n:::"
183188
assert quarto_note_replace(r"::: {.callout-note}\nThis a callout note.\n:::") == rst
184189
assert convert_callout_notes("./tests", None, False)["cells"][1]["source"] == rst
190+
191+
192+
def test_setting_kernelspec():
193+
meta_mock_nb = nbformat.read("tests/mock.ipynb", as_version=4).metadata
194+
kernel_display_name_mock_nb = meta_mock_nb.kernelspec.display_name
195+
kernel_name_mock_nb = meta_mock_nb.kernelspec.name
196+
new_meta_mock_nb = set_kernel_all_notebooks(dir="tests", save=False).metadata
197+
assert kernel_display_name_mock_nb != new_meta_mock_nb.kernelspec.display_name
198+
assert kernel_name_mock_nb != new_meta_mock_nb.kernelspec.name

0 commit comments

Comments
 (0)