Wednesday, September 16, 2015

Using IPython Notebook with Django on a remote server

IPython Notebook (or Jupyter, whatver) is great. Django is useful and I use it for both work and play. The combination would be greatly useful

My setup is something like this: I have an EC2 machine running Ubuntu, which powers a Django site. I want to be able to launch IPython Notebook on the remote server such that:
  1. It knows about Django and can import my models properly
  2. I can just write the code in a web browser on my local machine
  3. It's secure in that there's not just a stray notebook on the web that anyone can look at

My mom mentioned the Django extension 'shell_plus' the other day, and that turned out to be key to this solution!

Installing stuff:

 (I did this a few days ago and don't remember all the details, I *think* it was this simple.)
  • IPython Notebook
    • http://ipython.org/install.html
    • pip install "ipython[notebook]"
  • shell_plus via django-extensions
    • http://django-extensions.readthedocs.org/en/latest/installation_instructions.html
    • pip install django-extensions

Running stuff:

There are a couple steps that I wrote down for myself (and now you) since they are hard to remember until you've done them many times.

First, I connect to the server through an SSH tunnel. On my local machine, I run

ssh -L 9999:localhost:8888 mycoolserver.com

The second port (the 8888) is the remote port. That needs to be 8888 for IPython Notebook, unless you can convince it to run on a different port. The first port (9999) is the local one, and you can change it if you want. 

On the remote server, (in the SSH connection I just made, or a separate one, doesn't matter) I launch IPython Notebook using Django's manage.py and shell_plus, with an extra argument to stop it from trying to launch a browser. I want to use a local browser, not a remote one.

./manage.py shell_plus --notebook --no-browser

Back on my local machine, that 9999 I had in the first command comes in to play. I go to Chrome and visit:

http://localhost:9999

And from there, I can do all the normal IPython Notebook and interactive Django shell things! At the same time!

To stop things, I kill the IPython Notebook by killing the ./manage.py process, and then I kill the SSH connection.