Skip to content

Commit 3f7e3c7

Browse files
authored
Merge pull request #68 from CCPBioSim/nglview-fix
fix the nglview mismatch error in a graceful way
2 parents 2baa25c + 3826f94 commit 3f7e3c7

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎docker/Dockerfile‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@ USER $NB_USER
99
WORKDIR $HOME
1010

1111
# Install workshop deps
12-
RUN mamba install matplotlib numpy pandas nglview ipywidgets -y
13-
RUN pip install mdtraj
12+
RUN conda install -y -c conda-forge \
13+
matplotlib=3.11.1 \
14+
numpy=2.5.3 \
15+
pandas=3.0.5 \
16+
nglview=4.0.1 \
17+
ipywidgets=8.1.9
18+
RUN pip install --no-cache-dir \
19+
mdtraj==1.11.1
20+
21+
COPY docker/fix-nglview.sh /tmp/fix-nglview.sh
22+
RUN bash /tmp/fix-nglview.sh
1423

1524
# Get workshop files and move them to jovyan directory.
1625
COPY --chown=1000:100 . .

‎docker/fix-nglview.sh‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Locate nglview's Python frontend file
5+
NGLVIEW_DIR=$(python -c "import nglview, os; print(os.path.dirname(nglview.__file__))")
6+
FRONTEND_FILE="$NGLVIEW_DIR/_frontend.py"
7+
8+
if [[ ! -f "$FRONTEND_FILE" ]]; then
9+
echo "nglview _frontend.py not found, skipping patch" >&2
10+
exit 0
11+
fi
12+
13+
# Locate the installed labextension's package.json by searching
14+
# every labextensions directory Jupyter knows about (handles conda's
15+
# $CONDA_PREFIX/share/jupyter/labextensions layout automatically)
16+
LABEXT_PKG=$(python -c "
17+
from jupyter_core.paths import jupyter_path
18+
import os, sys
19+
for d in jupyter_path('labextensions'):
20+
candidate = os.path.join(d, 'nglview-js-widgets', 'package.json')
21+
if os.path.isfile(candidate):
22+
print(candidate)
23+
sys.exit(0)
24+
sys.exit(1)
25+
") || { echo "nglview-js-widgets labextension not found, skipping patch" >&2; exit 0; }
26+
27+
PY_VERSION=$(python -c "import re; print(re.search(r\"__frontend_version__ = '([^']+)'\", open('$FRONTEND_FILE').read()).group(1))")
28+
JS_VERSION=$(python -c "import json; print(json.load(open('$LABEXT_PKG'))['version'])")
29+
30+
# If there is a version mismatch then make them the same python side.
31+
if [[ "$PY_VERSION" != "$JS_VERSION" ]]; then
32+
echo "nglview mismatch: python=$PY_VERSION js=$JS_VERSION (found at $LABEXT_PKG) -- patching"
33+
sed -i "s/__frontend_version__ = '$PY_VERSION'/__frontend_version__ = '$JS_VERSION'/" "$FRONTEND_FILE"
34+
else
35+
echo "nglview versions already match ($PY_VERSION), skipping patch"
36+
fi

0 commit comments

Comments
 (0)