Skip to content

Commit

Permalink
Detect jupyter correctly in script
Browse files Browse the repository at this point in the history
* Remove after_install hook because it is redundant now
  • Loading branch information
appleparan committed Jan 22, 2024
1 parent e0eeca4 commit a34f140
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
31 changes: 16 additions & 15 deletions bin/pyenv-register-kernel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
# it will use the current active version. If there is more than one
# active version, it will use the first one.

# Function to check if a Python package is installed
check_python_package_installed() {
local package=$1
PYENV_VERSION=$name pyenv exec pip list --disable-pip-version-check | grep -F "$package" > /dev/null
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}

set -e
[ -n "$PYENV_DEBUG" ] && set -x

Expand All @@ -20,28 +31,18 @@ fi
python=$(PYENV_VERSION=$name pyenv which python)

# Check if Jupyter is installed
if PYENV_VERSION=$name pyenv exec which jupyter >/dev/null; then
if check_python_package_installed "jupyter"; then
jupyter_dir=$(PYENV_VERSION=$name pyenv exec jupyter --data-dir)
kernel_dir=${jupyter_dir}/kernels/pyenv_${name}

echo "Installing jupyter kernel file $name for $python to $kernel_dir ..."

echo "Upgrading (or installing) the ipykernel package for $name ..."
PYENV_VERSION=$name pyenv exec pip install -U ipykernel
PYENV_VERSION=$name pyenv exec pip install -U --no-deps ipywidgets

mkdir -p $kernel_dir

kernel_file=${kernel_dir}/kernel.json

cat > $kernel_file <<EOF
{
"argv": [ "$python", "-m", "ipykernel", "-f", "{connection_file}" ],
"display_name": "$name",
"language": "python"
}
EOF
PYENV_VERSION=$name pyenv exec pip install -U --disable-pip-version-check ipykernel
PYENV_VERSION=$name pyenv exec pip install -U --no-deps --disable-pip-version-check ipywidgets

echo "Creating kernel..."
PYENV_VERSION=$name pyenv exec python -m ipykernel install --user --name pyenv_$name --display-name "pyenv_$name"
echo "Kernel file created."
else
echo "Jupyter is not installed in the pyenv environment '$name'."
Expand Down
7 changes: 0 additions & 7 deletions etc/pyenv.d/install/register-jupyter-kernel.bash

This file was deleted.

20 changes: 18 additions & 2 deletions etc/pyenv.d/uninstall/remove-jupyter-kernel.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
#!/usr/bin/env bash
env_name=$1

if declare -Ff after_uninstall >/dev/null; then
after_uninstall 'rm -rf $(jupyter --data-dir)/kernels/${VERSION_NAME}'
check_python_package_installed() {
local package=$1
PYENV_VERSION=$env_name pyenv exec pip list --disable-pip-version-check | grep -F "$package" > /dev/null
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}

if declare -Ff before_uninstall >/dev/null; then
if check_python_package_installed "jupyter"; then
jupyter_data_dir=$(PYENV_VERSION=$env_name pyenv exec jupyter --data-dir)
before_uninstall 'rm -rf ${jupyter_data_dir}/kernels/pyenv_${env_name}'
else
echo "Jupyter is not installed in the pyenv environment '${env_name}'."
fi
else
echo "pyenv: pyenv-jupyter-kernel plugin requires pyenv v0.1.0 or later" >&2
fi

0 comments on commit a34f140

Please sign in to comment.