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. 

5 comments:

  1. This is very cool! Would this expose your entire Django data model to your IPython notebook in order to run computational analysis on your data?

    ReplyDelete
    Replies
    1. Yes! All the django models are exposed. The only thing to be careful of is that your notebook files are in the same directory as manage.py, otherwise import paths are messed up.

      Delete
    2. Thanks again for the info, Kathleen. You really opened my mind to an idea for a project!

      Delete
  2. Very handy, thank you! I used to achieve the same thing by exposing port "8888" on the production server, so I could connect to the socket - your method is much simpler and more secure.

    ReplyDelete
  3. As an developer I have come across this post and i have analysis the topic. Using iPython notebook with Django on a remote server is necessary. So I definitely think this post is much useful.

    ReplyDelete