Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change register-kernel to manual kernel registration #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pyenv-jupyter-kernel

Pyenv plugin to create a jupyter kernel for every installed pyenv version. Inspiration from [this gist](https://gist.github.com/thvitt/9072336288921f57ec8741eb4b8b024e)
Pyenv plugin to create a jupyter kernel for installed pyenv version. Inspiration from [this gist](https://gist.github.com/thvitt/9072336288921f57ec8741eb4b8b024e)

## Installation

Expand All @@ -10,7 +10,7 @@ $ git clone https://github.com/aiguofer/pyenv-jupyter-kernel $(pyenv root)/plugi

## Usage

New kernels are automatically installed for every new version and virtualenv that you install. However, if you want to install the kernel for the current version (if using multiple versions, the top one) you can run:
If you want to install the kernel for the current version (if using multiple versions, the top one) you run:

```shell
$ pyenv register-kernel
Expand Down
44 changes: 25 additions & 19 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 @@ -19,25 +30,20 @@ fi

python=$(PYENV_VERSION=$name pyenv which python)

jupyter_dir=$(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
# Check if Jupyter is installed
if check_python_package_installed "jupyter"; then
jupyter_dir=$(PYENV_VERSION=$name pyenv exec jupyter --data-dir)
kernel_dir=${jupyter_dir}/kernels/pyenv_${name}

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

kernel_file=${kernel_dir}/kernel.json
echo "Upgrading (or installing) the ipykernel package for $name ..."
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

cat > $kernel_file <<EOF
{
"argv": [ "$python", "-m", "ipykernel", "-f", "{connection_file}" ],
"display_name": "$name",
"language": "python"
}
EOF

echo "Kernel file created."
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'."
fi
7 changes: 0 additions & 7 deletions etc/pyenv.d/install/register-jupyter-kernel.bash

This file was deleted.

18 changes: 16 additions & 2 deletions etc/pyenv.d/uninstall/remove-jupyter-kernel.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#!/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}'
fi
else
echo "pyenv: pyenv-jupyter-kernel plugin requires pyenv v0.1.0 or later" >&2
fi
7 changes: 0 additions & 7 deletions etc/pyenv.d/virtualenv/register-jupyter-kernel.bash

This file was deleted.