Skip to content

Instantly share code, notes, and snippets.

@puzzledqs
Last active August 29, 2015 13:58
Show Gist options
  • Save puzzledqs/10118763 to your computer and use it in GitHub Desktop.
Save puzzledqs/10118763 to your computer and use it in GitHub Desktop.
Remote an IPython Notebook

IPython Notebook has always been handy and cool for prototyping algorithms and running quick simulations, for it brings the best of two worlds -- the powerful interactive capability of IPython terminal, and the beautiful documentation enabled by Markdown. You can always check out the IPython website for existing and coming cool new features.

Simply put, IPython Notebook runs in a server-client mode. A IPython kernel (server) runs at the backend, and you connect to and interact with the kernel from your web browser through a localhost TCP port. The initialization is automated so you don't have to bother with setting up the whole enviroment by hand. Everytime you'd lide to start an IPython notebook, just type ipython notebook in the command line and the script will take care of the rest for you. That's pretty much the way I have been using ipython notebook up to now.

Last night, I started to think of the possibility of remote a IPython notebook, or strictly speaking, remote an IPython kernel from a local browser tab. The motivation for doing this is rather straightforward -- recently our lab has bought a bunch of industrial-level workstations equipped with GPUs to handle heavy parallel computations. The workstations are running great so far except for a slightly annoying troublesome -- the CUDA will just not run if we remote desktop the workstations. As a result, students who are intended to run CUDA codes will have to rush to the workstation and start their simulations right from there. Then yesterday, it suddenly occurred to me that remoting a IPython kernel from a IPython notebook might be a workaround to this issue. Since remoting a IPython kernel only involves a TCP connection and no display settings, it is not likely to affect the CUDA. So I tried this out tonight, and it turned out the CUDA worked smoothly when I remote to the workstation from an IPython notebook in my browser. The following is how I did this.

Note: Both our workstation as well as my local desktop run on Win 7. I guess things will be a bit easier for linux platforms.

  1. Install IPython on the workstation. I used the Anaconda distribution.
  2. Launch ssh service on the workstation. This requires a fair amount of work. The reason we need this step is because we will rely on ssh tunneling to connect to the port of the remote IPython kernel.
  3. Once the ssh service is ready, we are almost done. The rest rests are quite easy. First launch a IPython kernel on the workstation by running the command
    ipython notebook --no-browser --port=7777
    ```.
    

Here you can go with another port number that is most convenient for you. 4. Let's now move to my local desktop. The work here is way too easy compared to the server side. The only thing we need is to ssh tunnel to the workstation, and forward the ipython kernel port to a local port. Under Cygwin, this can be done in a single line of command ssh -N -f -L localhost:your_port:localhost:ipython_port username@ip_workstation, where 'your_port', 'ipython_port', 'username', and 'ip_workstation' are to be filled in based on your own case. Dumping it into a .sh file saves you from typing the command over and again. In case you don't have Cygwin installed on you local machine, Putty would be another way to go. Check out this post for a step-by-step guide to ssh tunneling with Putty. 5. Finally, we are ready to connect to the remote IPython kernel from our local web browser! Open a new tab and go to localhost:your_port, and you will be directed to the remote kernel and an IPython notebook dashboard will pop out. Congrats! Try creating a new notebook, and run some python codes there! 6. There we are! We have came a long way towards our ultimate goal -- running the CUDA remotely. Now it's time to do the final test. %run a python CUDA codes from the notebook, and Woo-Hoo, it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment