Setup a remote IPython notebook server with Numpy/Scipy/Maltplotlib/Pandas in a virtualenv on Ubuntu Server

I'm currently reading the book Python for data analysis and I have struggled to setup a remote IPython notebook server with all the scientific python stack, in a virtualenv, on Ubuntu Server. That's why I share the steps I followed to get everything running, without the headhaches.

This guide will show you how to setup a remote IPython notebook server with the following features:

  • a full featured remote Python REPL
  • everything installed in a virtualenv
  • password protected
  • inline charts (thanks to Matplotlib)

And we will install the following libraries: Numpy, Scipy, Pandas and Matplotlib.

I assume you have working virtualenvs.

Pylab

pylab is the combination of NumPy, Matplotlib, and IPython, so when you start IPython in pylab mode, all these module are already imported.

The only way to run IPython in pylab mode on a remote server is to run the IPython notebook server, you also need to install a backend to generate the chart, I choose to use python-qt4.

Installation

First we need to install some dependencies.

$ sudo apt-get install libatlas-dev libpng12-dev libfreetype6 libfreetype6-dev g++ libzmq-dev liblapack-dev gfortran python-dev build-essential python-qt4

Then, in your virtualenv:

$ pip install numpy scipy tornado pyzmq pandas ipython pygments matplotlib

Inline charts

We need to get pyqt4 installed in the virtualenv, since it doesn't works with pip, I found a gist that do the job:

I found this gist to get pyqt4 working inside a virtualenv.

#!/bin/bash
# This hook is run after a new virtualenv is activated.
# ~/.virtualenvs/postmkvirtualenv

libs=( PyQt4 sip.so )

python_version=python$(python -c "import sys; print (str(sys.version_info[0])+'.'+str(sys.version_info[1]))")
var=( $(which -a $python_version) )

get_python_lib_cmd="from distutils.sysconfig import get_python_lib; print (get_python_lib())"
lib_virtualenv_path=$(python -c "$get_python_lib_cmd")
lib_system_path=$(${var[-1]} -c "$get_python_lib_cmd")

for lib in ${libs[@]}
do
    ln -s $lib_system_path/$lib $lib_virtualenv_path/$lib 
done

With the target virtualenv activated.

$ wget https://raw.github.com/gist/2042882/160eb22a86f9c5a120033b9b05213b2aab8f79f6/postmkvirtualenv
$ chmod +x postmkvirtualenv
$ ./postmkvirtualenv

And finally, you need to add that line in ~/.matplotlib/matplotlibrc.

$ vim .matplotlib/matplotlibrc

We just need to add this line:

backend: Qt4Agg

Configuring the notebook server

You should check out the IPython notebook docs.

First, get your password hashed with IPython utility.

In [1]: from IPython.lib import passwd
In [2]: passwd()
Enter password:
Verify password:

Next, create a new IPython configuration profile.

$ ipython profile create myserver
$ vim ~/.ipython/profile_myserver/ipython_notebook_config.py

Now we can edit the file:

c = get_config()

c.IPKernelApp.pylab = 'inline'
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:yourhashedpassword'
c.NotebookApp.port = 9999

Running the server

Now, you can run the server, to keep it running, you can manage the process with Supervisor.

$ ipython notebook --profile=myserver

To check if inline chart works, just type that line in new notebook:

plot(arange(10))

That's it, now you are able to access your notebook from everywhere.

And you ?

Don't hesitate if you have any questions or tips !

You should follow me on Twitter

Share this article

Tip with Bitcoin

Tip me with Bitcoin and vote for this post!

1FKdaZ75Ck8Bfc3LgQ8cKA8W7B86fzZBe2

Leave a comment

© Thomas Sileo. Powered by Pelican and hosted by DigitalOcean.