Skip to content

Commit 70025d0

Browse files
committed
πŸ§‘β€πŸ’»(project) define notebook kernel dependencies in groups
Instead of having to modify notebook service Dockerfile to add new ipython kernels along with their dependencies, automate kernels generation by defining a new dependency group.
1 parent c81026d commit 70025d0

File tree

4 files changed

+840
-23
lines changed

4 files changed

+840
-23
lines changed

β€Žsrc/notebook/Dockerfileβ€Ž

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
11
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim
22

3+
# Paths
4+
ENV KERNELS_PATH=/home/notebook/kernels
5+
ENV WORKDIR=/home/notebook/app
6+
37
# Create notebook user with configurable UID/GID
8+
# Note hat GID 20 already exists in docker image
49
ARG DOCKER_UID=1000
5-
6-
# GID 20 already exists in docker image
710
RUN useradd -m -u ${DOCKER_UID} -s /bin/bash notebook
811

912
# Switch to the notebook user
1013
USER notebook
1114

1215
# Set working directory
13-
WORKDIR /home/notebook/app
16+
WORKDIR ${WORKDIR}
1417

15-
# Create directory structure for app and thematic kernels
16-
RUN mkdir -p /home/notebook/app && \
17-
mkdir -p /home/notebook/kernels/nlp
18+
# Create directory for thematic kernels
19+
RUN mkdir -p "${KERNELS_PATH}"
1820

1921
# Copy dependency files
20-
COPY pyproject.toml uv.lock ./
22+
COPY pyproject.toml uv.lock list-kernels.py ./
2123

2224
# Install Python dependencies
2325
RUN uv sync --frozen
2426

25-
# Create NLP kernel
26-
RUN cd /home/notebook/kernels/nlp && \
27-
uv init --name csplab-nlp && \
28-
uv add pandas numpy matplotlib ipykernel spacy transformers
29-
30-
# Now create all kernels as the notebook user with specific environment variables
27+
# Create the base kernel
3128
RUN uv run \
3229
ipython kernel install \
3330
--user \
3431
--name=csplab-base \
3532
--display-name="CSPLab Base (pandas, numpy, matplotlib)"
3633

37-
RUN cd /home/notebook/kernels/nlp && \
38-
uv run \
39-
ipython kernel install \
40-
--user \
41-
--env VIRTUAL_ENV $(pwd)/.venv \
42-
--name=csplab-nlp \
43-
--display-name="CSPLab NLP (spacy, transformers)"
34+
# Now create all specific kernels
35+
RUN for kernel in $(python list-kernels.py); do \
36+
KERNEL_PATH="${KERNELS_PATH}/${kernel}"; \
37+
VIRTUAL_ENV="${KERNEL_PATH}/.venv"; \
38+
mkdir -p "${KERNEL_PATH}" && \
39+
cp pyproject.toml uv.lock "${KERNEL_PATH}" && \
40+
cd "${KERNEL_PATH}" && \
41+
uv sync --frozen --only-group "${kernel}" && \
42+
VIRTUAL_ENV="${VIRTUAL_ENV}" uv run \
43+
ipython kernel install \
44+
--user \
45+
--env VIRTUAL_ENV "${VIRTUAL_ENV}" \
46+
--name="csplab-${kernel}" \
47+
--display-name="CSPLab ${kernel}"; \
48+
done
4449

4550
CMD [ \
4651
"uv", \

β€Žsrc/notebook/list-kernels.pyβ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
List ipython kernels as defined in the pyproject.toml configuration.
3+
4+
Note: all dependency group is considered as a kernel except for the `dev` group.
5+
"""
6+
7+
import tomllib
8+
from pathlib import Path
9+
10+
pyproject = tomllib.load(Path("./pyproject.toml").open("rb"))
11+
kernels = [k for k in pyproject["dependency-groups"] if k != "dev"]
12+
13+
for kernel in kernels:
14+
print(kernel)

β€Žsrc/notebook/pyproject.tomlβ€Ž

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ dependencies = [
1313
"numpy>=1.24.0",
1414
"matplotlib>=3.7.0",
1515
"requests>=2.31.0",
16-
"elasticsearch>=9.1.1",
1716
]
1817

1918
[dependency-groups]
@@ -22,5 +21,15 @@ dev = [
2221
"ruff>=0.0.280"
2322
]
2423

24+
# -- Jupyter kernels
25+
es = [
26+
"elasticsearch>=9.1.1",
27+
]
28+
29+
nlp = [
30+
"spacy>=3.8.7",
31+
"transformers>=4.57.0",
32+
]
33+
2534
[tool.jupytext]
2635
formats = "ipynb,md"

0 commit comments

Comments
Β (0)