IPython Notebook: Tips

For python developers, IPython Notebook has become an indispensable tool. Below are few tricks to make it even more attractive. You can checkout actual notebook over here

1. %run -i: Running another notebook in the context of current python kernel

There are always few classes/functions that you want to use across different notebooks. You can keep these common functions in a notebook (say common.ipynb) and run it in the context of existing notebook by invoking the following command in your existing notebook.

%run -i common.ipynb

2. Progress Bars

Ever wondered how much more time your iterator will take to complete. There are multiple ways to easily include a progress bar along with your iterators.

Option 1: Using inbuilt IPython utilities

from ipywidgets import FloatProgress
from IPython.display import display
f = FloatProgress(min=0, max=MAX_VALUE)
for i in xrange(MAX_VALUE):
    f.value = i  # increment value of the progress bar

Option 2: Using tqdm library

I prefer tqdm as it makes adding a progress bar a breeze.

from tqdm import tqdm
from time import sleep
for i in tqdm(range(100)):

3. Unit Testing

Below is a simple example of showing how to write unit test in the ipython notebook. The main lines are the two bottom most lines where we load the unit test suite and run it.

import unittest

# Define Person class
class Person(object):

    def __init__(self, name, age):
        self.__name = name
        self.__age = age

    def name(self):
        return self.__name

    def age(self):
        return self.__age

    def __str__(self):
        return "{} ({})".format(self.name, self.age)

    def __eq__(self, other):
        return self.name == other.name and self.age == other.age

# Define unit test
class PersonTest(unittest.TestCase):

    def test_initialization(self):
        p1 = Person("xyz", 10)
        self.assertEqual("xyz", p1.name)
        self.assertEqual(10, p1.age)

    def test_equality(self):
        p1 = Person("xyz", 10)
        p2 = Person("xyz", 10)
        self.assertEqual(p1, p2)

## 4. Run unit test
suite = unittest.TestLoader().loadTestsFromTestCase( PersonTest )

5. Using R’s ggplot to visualize data

I love R’s ggplot package for visualizing data. Luckily using Ipython notebook I can do all the data processing in python and visualize it using R’s ggplot. Checkout more about over here

6. Using Sublime like editor

Of all the text editors, I find Sublime to be the best. It offers multiple functionality but the best is multicursor editing. Interestingly you can have the same editing capability within your ipython notebook. As mentioned over here, you need to add some code to your custom.js file. If you are missing custom.js file, then follow this link and complete the “hello world…” exercise to make sure that custom.js is being loaded properly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s