Django and IPython Notebook

Recently updated on

The IPython Notebook is a really cool application, and I've always wanted to use it during Django development and debugging.  The only problem is that it requires a lot of dependencies and I feel no need to encumber my production projects with those dependencies for a feature that I only plan to use in development on my desktop.  Specifically, the http version of IPython Notebook requires jinja2, tornado, pyzmq (and libzmq3), and markupsafe.  

Ideally, I'd be able to install these dependencies only for development, and recently I figured out a workable way to accomplish this.  I figured that I could create a "toolkit" directory of python packages that I can add to and remove from the python path.  This toolkit directory will reside outside of my virtualenv and the python packages inside of it will NOT be modules that I add to my requirements file.  However, I can "attach" this toolkit module directory to any virtualenv when I want to enable something like the IPython Notebook, and "detach" it when I no longer need it.

To install the IPython Notebook in this way, I need to first install the system-level apt package needed for IPython Notebook.  This system-level package is the ZeroMQ library.

sudo aptitude install libzmq3

Second, using the --prefix pip install option shown below, I can specify an alternate location to install the python modules that I want to include in my "toolkit."  I arbitrarily chose the /opt/python_packages directory, and I installed all of the basic IPython Notebook dependencies.  As an aside, I had problems installing these packages when they were installed elsewhere on my system path.  Uninstalling first and reinstalling with the below syntax seemed to do the trick.  Note, on Ubuntu 14.04, I noticed that pip installed IPython from a wheel file. This prevented it from being compiled into the proper prefix directory.  I passed the --no-use-wheel option, as shown below, to disable the wheel download.

sudo pip install --install-option="--prefix=$PREFIX_PATH" jinja2
sudo pip install --install-option="--prefix=$PREFIX_PATH" tornado
sudo pip install --install-option="--prefix=$PREFIX_PATH" pyzmq
sudo pip install --install-option="--prefix=$PREFIX_PATH" markupsafe
sudo pip install --no-use-wheel --install-option="--prefix=$PREFIX_PATH" ipython

Third, I activated the virtualenv that I wanted to work on. 

. ./bin/activate

Fourth, I set the PYTHONPATH and the shell PATH to point to the 'toolkit' module directory that I created.  Note, I'll need to do this every time I open a new terminal and want to run the IPython Notebook server in it or script it.

export PATH="/opt/python_packages/bin:$PATH"
export PYTHONPATH="/opt/python_packages/lib/python2.7/site-packages:$PYTHONPATH"

Penultimately, I need to install django_extensions.  The shell_plus management command that we will use to run the IPython Notebook is provided by the django_extensions module; this management command has built-in support for ipython notebook and properly sets the Django environment context.  The django_extensions module needs to be installed and added to the INSTALLED_APPS in

Finally, I am able to run ipython notebook server from the django_extensions shell_plus management command.  

./ shell_plus --notebook

Running this command automatically opens up a browser window and loads the url  This is the ipython notebook.

If I no longer need to use the ipython notebook, I can either reset my PATH and PYTHONPATH or I can just close and reopen my shell which will reset those environment variables and remove the "toolkit" directory from the python path.  A caveat to this approach is that this will likely only work if the python version for the toolkit directory to matches that of each virtualenv that you use this in.  

Anyway, I'd be interested to see how others integrate IPython Notebook with their Django workflow.  

Share Twitter, LinkedIn, Facebook

Hey there...
Would you mind sharing this post? We would greatly appreciate it.
Thank you.