IPython Notebook: Tips

For python developers, IPython Notebook has become an indispensable tool. Below are few tricks I found that 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

One of the fundamental tenet of object oriented programming is to avoid duplication of code. That was one of issues I always had with IPython Notebook. There are always few classes/functions that you use across different notebooks. Initially I use to copy these functions in each notebook. However, using %run magic function I finally found a solution to the above problem. Magic function %run allows you to run another notebook in the context of current python kernel.

Assuming you defined all the common classes/functions in “common.ipynb” and you want to incorporate those in another notebook (say projectA.ipynb), then invoke the below command to make them available in projectA.ipynb.

%run -i common.ipynb

2. Progress Bars: Keep a check on your iterators.
Progress bars are nice way to keep track of processing time remaining. As shown below, IPython Notebook makes it pretty easy to include a nice-looking progress bar in your notebooks.

from ipywidgets import FloatProgress
from IPython.display import display
f = FloatProgress(min=0, max=100)

# Increment value of the progress bar within the iterator
from time import sleep
for i in xrange(100):
   f.value = i

(Yikes!!!.. so much code to get a progress bar). If you feel like me then you should install tqdm package. It makes adding a progress bar with minimal code a breeze.

from tqdm import trange
for i in trange(100):

3. Unit Testing: Make sure your functions/classes are working fine
Testing code is important and its easy to include unit test in your ipython notebook. Below is an example of how to incorporate unittest

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)

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

4. Use R’s ggplot to visualize data
Both Python and R have there own pros and cons. Luckily you can have goodness of both the worlds within IPython notebook. Using rpy2 python package you can seamlessly transform data/objects between python and R environment. Checkout more about this in one of my another blog post over here.

Posted in General, Programming, Python | Tagged | Leave a comment