python4oceanographers

Turning ripples into waves

Jupyter/IPython kernels for venvs

This is a short but very useful post.

The new Jupyter/IPython architecture accepts multiples kernels, one can run R, Julia, Haskell and many other languages. Here is list of kernels that are available for installation:

It is possible to run all those languages taking advantage of the rich literate programming capabilities of the notebook.

One cool feature is the possibility to switch from one language to another using a drop down menu (upper right of the notebook).

However, I do not use any of those in my everyday work. I do use a variety of python installations though. And that drop down menu looks very interesting to me.

In my current setup I use Virtual environments created with virtualenvwrapper, conda, system python3 (my default), system python2 and many others.

With Jupyter/IPython new kernel logic it is possible to roll your own kernel. Here, instead of creating my own, I just created some kernels.json files that point to my custom python installations:

In [3]:
%%bash
# Default.
ipython_config_dir=$(ipython locate config)
cat $ipython_config_dir/kernels/python3/kernel.json
{
 "display_name": "IPython (Python 3)",
 "language": "python",
 "codemirror_mode": {
  "version": 3,
  "name": "ipython"
 },
 "argv": [
  "python3",
  "-c",
  "from IPython.kernel.zmq.kernelapp import main; main()",
  "-f",
  "{connection_file}"
 ]
}

In [4]:
%%bash
# Virtualenvwarpper python 2 with iris.
ipython_config_dir=$(ipython locate config)
cat $ipython_config_dir/kernels/iris_python2/kernel.json
{
 "display_name": "Iris (Python 2)",
 "language": "python",
 "codemirror_mode": {
  "version": 3,
  "name": "ipython"
 },
 "argv": [
  "/home/filipe/.virtualenvs/iris/bin/python",
  "-c",
  "from IPython.kernel.zmq.kernelapp import main; main()",
  "-f",
  "{connection_file}"
 ]
}

In [5]:
%%bash
# Miniconda python 2 with ioos channel.
ipython_config_dir=$(ipython locate config)
cat $ipython_config_dir/kernels/ioos_python2//kernel.json
{
 "display_name": "IOOS (Python 2)",
 "language": "python",
 "codemirror_mode": {
  "version": 3,
  "name": "ipython"
 },
 "argv": [
  "/home/filipe/miniconda/envs/ioos/bin/python",
  "-c",
  "from IPython.kernel.zmq.kernelapp import main; main()",
  "-f",
  "{connection_file}"
 ]
}

And now I can switch from one environment to another with the drop down menu. It is that simple! There is no need to install anything extra because they are all python environments.

Here is the result:

In [6]:
from IPython.display import Image

Image('../../figures/jupyter_kernels.png')
Out[6]:

Now I do not need to open several notebook instances to change from one environment to another ever again!

In [7]:
HTML(html)
Out[7]:

This post was written as an IPython notebook. It is available for download or as a static html.

Creative Commons License
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://ocefpaf.github.io/.

Comments