IPython Notebook server on Raspberry Pi

ipython_notebook

IPython Notebook is a great Matlab-like/Mathematica-like thing. You can write and run scripts written in python by using a web browser only. This post describes how to set up IPython Notebook server.

Important notice!
IPython is now Jupyter project. If you would like to read about it go to my recent post about it (Jupyter Notebook server on Raspberry Pi).

IPython installation

First of all you need a few prerequisites that has to be installed on your Raspberry Pi. To install them type below command in console. Keep in mind that this process will take some time — probably around half an hour.

sudo apt-get -y install ipython-notebook python-matplotlib python-scipy python-pandas python-sympy python-nose

IPython configuration

The next step is to create a profile. Substitute <name> phrase with a more meaningful name, i.e. ipserver.

ipython profile create <name>

For ipython notebook to work with authentication you will need to generate a hash sum from a password. To do this you need to start python console

python

After that import passwd from IPython lib and call passwd() function.

from IPython.lib import passwd         
passwd()

It will prompt for password. After that a hash sum will be generated. You need to copy it for further configuration.
Open previously generated configuration file

vim /home/user/.ipython/profile_<name>/ipython_notebook_config.py

At the very beginning you will find

c = get_config()

Just after that add following lines:

c.IPKernelApp.pylab = ‘inline’
c.NotebookApp.ip = '*'
c.NotebookApp.port = 8888
c.NotebookApp.password = 'hash'
c.NotebookApp.open_browser = False

First option configures the IPython kernel. The second line allows all IP addresses to connect to the server and the following line sets IPython server to listen on 8888 port. In the place of hash put previously generated hash sum. This will set up password for the Notebook. The last line prevents IPython to start a web browser.

Testing IPython

When everything is set up you can run the server with following command:

ipython notebook –profile=<name>

From now on you should be able to connect to the server by typing following URL in a browser:
http://raspberry_ip:8888
where raspberry_ip is RPi’s IP address.
When everything is working properly after entering above URL you should see a login form, similar to this one:
ipython_login
Below you can see all notebooks which are currently in your working directory:
ipython_tree
To shutdown down the IPython Notebook server you have to terminate the process by pressing Ctrl+C key combination.

Start IPython Notebook on boot

One way of starting up the server on system boot is to edit rc.local file.

sudo vim /etc/rc.local

Put the following line just before exit(0) in rc.local:

su pi -c "ipython notebook --quiet --ipython-dir=/home/pi/ipython --profile=<name> &> /dev/null" &

This command will start ipython server as pi user and:

  • force ipython to limit the output to the minimum,
  • set /home/pi/ipython path as the default path for Notebook scripts,
  • use <name> profile.

The command output, both stdout and stderr, is redirected to the /dev/null device and the command itself is started in background.

IPython Notebook accessible from outside network

To make the IPython server accessible from the outside of local network you have to set up port forwarding on your router. But before doing that make sure that RPi has a static IP address inside the local network, so to prevent it from changing. After setting static IP address select a port number, i.e. 9999, on which you want RPi to be reached from outside. By configuring router to pass the requests on 9999 to RPi’s local IP address on 8888 port number. By doing this the RPi’s IPython service is available on port 9999 from outside local area network.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.