Skip to Content

Technology Blog

Technology Blog

Django and IPython Notebook

Recently updated on

<p>The IPython Notebook is a really cool application, and I&#39;ve always wanted to use it during Django development and debugging. &nbsp;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. &nbsp;Specifically, the http version of IPython Notebook requires jinja2, tornado, pyzmq (and libzmq3), and markupsafe. &nbsp;</p>

<p>Ideally, I&#39;d be able to install these dependencies only for development, and recently I figured out a workable way to accomplish this. &nbsp;I figured that I could create a &quot;toolkit&quot; directory of python packages that I can add to and remove from the python path. &nbsp;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. &nbsp;However, I can &quot;attach&quot; this toolkit module directory to any virtualenv when I want to enable something like the IPython Notebook, and &quot;detach&quot; it when I no longer need it.</p>

<p>To install the IPython Notebook in this way, I need to first install the system-level apt package needed for IPython Notebook. &nbsp;This system-level package is the ZeroMQ library.</p>

sudo aptitude install libzmq3</pre>

<p>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 &quot;toolkit.&quot; &nbsp;I arbitrarily chose the /opt/python_packages directory, and I installed all of the basic IPython Notebook dependencies. &nbsp;As an aside, I had problems installing these packages when they were installed elsewhere on my system path. &nbsp;Uninstalling first and reinstalling with the below syntax seemed to do the trick. &nbsp;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. &nbsp;I passed the --no-use-wheel option, as shown below, to disable the wheel download.</p>

sudo pip install --install-option=&quot;--prefix=$PREFIX_PATH&quot; jinja2
sudo pip install --install-option=&quot;--prefix=$PREFIX_PATH&quot; tornado
sudo pip install --install-option=&quot;--prefix=$PREFIX_PATH&quot; pyzmq
sudo pip install --install-option=&quot;--prefix=$PREFIX_PATH&quot; markupsafe
sudo pip install --no-use-wheel --install-option=&quot;--prefix=$PREFIX_PATH&quot; ipython</pre>

<p>Third, I activated the virtualenv that I wanted to work on.&nbsp;</p>

. ./bin/activate</pre>

<p>Fourth, I set the PYTHONPATH and the shell PATH to point to the &#39;toolkit&#39; module directory that I created. &nbsp;Note, I&#39;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.</p>

export PATH=&quot;/opt/python_packages/bin:$PATH&quot;
export PYTHONPATH=&quot;/opt/python_packages/lib/python2.7/site-packages:$PYTHONPATH&quot;</pre>

<p>Penultimately, I need to install django_extensions. &nbsp;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. &nbsp;The django_extensions module needs to be installed and added to the INSTALLED_APPS in</p>

<p>Finally, I am able to run ipython notebook server from the django_extensions shell_plus management command. &nbsp;</p>

./ shell_plus --notebook</pre>

<p>Running this command automatically opens up a browser window and loads the url &nbsp;This is the ipython notebook.</p>

<p>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 &quot;toolkit&quot; directory from the python path. &nbsp;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. &nbsp;</p>

<p>Anyway, I&#39;d be interested to see how others integrate IPython Notebook with their Django workflow. &nbsp;</p>


Share , ,
If you're getting even a smidge of value from this post, would you please take a sec and share it? It really does help.