Over the past few months,I use ITorch / IPython notebooks frequently. Combining interactive programing with markdown documentation and portability, IPython notebooks are a powerful scientific-computing tool. Working within a notebook gives you the ability to experiment like you would in a terminal based interpreter and to save your work when you’re done.
IPython notebooks are web-based, which means you interact with them through a browser. When invoked, IPython starts a local webserver that hosts your notebooks. If you are like me, you are often working on remote machines (i.e. I’m at home working on our research groups cluster which is not at my home), so it’s not super easy to access that local web server.
There are, however, a couple pretty slick tricks that allow you to remotely access the private IPython web server hosted on remote machines, allowing you to work in a browser on your local machine, while all the computing gets done on the remote machine.
We can start a IPython notebook session by running the following command in the directory we want our notebooks to be stored in.
By running this command, we have started an IPython notebook webserver running at http://127.0.0.1:8889/
The IPython Notebook is running at: http://127.0.0.1:8889/
You could just run the above command and your browser would likely open in an x-window. That is all fine and good except, for a number of reasons, this is a painfully slow way to use IPython notebooks.
Here we are interested in running IPython notebooks on a remote machine. A better way to run IPython notebooks remotely is by using a ssh tunnel. When we create a ssh tunnel, we associate a specific port on the remote system to one on the local system. Here’s how to do it.
First, start an IPython notebook session on your remote machine, specifying the –no-browser and –port=7777 options:
ipython notebook --no-browser --port=7777
Now we want to setup the ssh tunnel from the local machine to the remtoe machine.
ssh -N -f -L localhost:7778:localhost:7777 username@hostname
The first option -N tells SSH that no remote commands will be executed, and is useful for port forwarding. The second option -f has the effect that SSH will go to background, so the local tunnel-enabling terminal remains usable. The last option -L lists the port forwarding configuration (remote port 7777 to local port 7778).
Alternatively, you can start the tunnel without the -f option. The process will then remain in the foreground and can be killed with ctrl-c.
Open localhost:7778 in your local browser and get started using remotely hosted IPython notebooks.
Again, the beauty of this is that I interact only with the browser on my local machine, while having access to the computing power and file system of the remote machine.