Turbocharge your Python prompt and Django shell with IPython Notebook

IPython is an enhanced Python shell, the shell being the prompt which appears when you start python command in your terminal. IPython comes with notebook flavour, where IPython Notebook presents the modern HTML-based shell for Python.

1. Introduction

Python is so-called dynamic programming language where much can be achieved by piloting your code in an interactive interpreter or tuning your production server from the shell. Basing the read-evaluate-print-loop (REPL) to web browser technology, instead of terminal technology, brings shell usability to the modern age, boosting your working efficiency considerably.

IPython notebook, as being based on HTML, can also output graphics and dynamic clickable and navigable graphics. For graphics, diagrams and plotting, check out Bokeh and Pandas libraries.

 

IPython

2. Installation

First create a virtualenv. Then continue in the activated virtualenv.

Install IPython with IPython Notebook extensions:

pip install "ipython[notebook]"

Starting IPython in terminal:

ipython

Developers like their terminals either white-on-black or black-on-white. Try switching the color scheme:

ipython --colors=linux

Screen Shot 2014-08-13 at 13.51.45

3. Enter the HTML

Now comes the big one. Start IPython notebook for a web browser based shells:

ipython notebook

This will start IPython kernel running in the port localhost:8888 and automatically connects your default web browser into it.

Screen Shot 2014-08-13 at 13.49.03

Yay! Finally you can

  • Use shift and arrows to select your text, unlike in most of the terminals
  • Copy-paste that works 100%
  • Click things with mouse and place your edit cursor
  • Save and replay your shell sessions
  • Output graphics directly from the shell

4. Django integration

First, you need to a have running Django project. If you are new to Django I highly recommend reading DjangoGirls’s Django tutorial – one of the best tutorials to modern web development covering HTTP, Python, databases, HTML, CSS and JavaScript.

Install Django Extensions package. Django Extensions, with many other features, gives you the ability to automatically import all of your application models on the start up to have quicker access to them. We do the example below using DjangoGirls website checkout.

pip install django_extensions

Add django_extensions to Django settings INSTALLED_APPS list:

INSTALLED_APPS = (
     ....
    'django_extensions'
)

Start IPython-enabled Django shell through Django extensions command:

python manage.py shell_plus

And you can straight away play with your Django models:

Screen Shot 2014-08-13 at 14.12.25

Now, try entering to the Notebook Django shell:

python manage.py shell_plus --notebook

… and you get the same, but with all IPython Notebook goodies:

IPython

5. Running IPython Notebook on your server

IPython Notebook uses, by default, uses localhost TCP/IP sockets to allow web browsers to connect to IPython Notebook kernel.

If you want to run IPython Notebook on a non-local computer you can use SSH tunnels. Please first read SSH basics for developers, then proceed to how to build SSH tunnels for port forwarding.

 Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

2 thoughts on “Turbocharge your Python prompt and Django shell with IPython Notebook

  1. Hello.
    I think you have typo in link inside chapter “4. Django integration” in sentence “First, you need to a have running Django project.”
    “Django project” link leads to http://pandas.pydata.org/

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>