Graph Gmail inbox data with IPython notebook


In [29]:
from IPython.display import Image

In [31]:
Image('http://i.imgur.com/SYija2N.png')


Out[31]:
Download your Gmail inbox as a ".mbox" file by clicking on "Account" under your Gmail user menu, then "Download data"
Install the Python libraries mailbox and dateutils with sudo pip install mailbox and sudo pip install dateutils

In [12]:
import mailbox
from email.utils import parsedate
from dateutil.parser import parse
import itertools
import plotly.plotly as py
from plotly.graph_objs import *

In [13]:
path = '/Users/jack/Desktop/All mail Including Spam and Trash.mbox'
Open your ".mbox" file with mailbox

In [22]:
mbox = mailbox.mbox(path)
Sort your mailbox by date

In [23]:
def extract_date(email):
    date = email.get('Date')
    return parsedate(date)

sorted_mails = sorted(mbox, key=extract_date)
mbox.update(enumerate(sorted_mails))
mbox.flush()
Organize dates of email receipt as a list

In [24]:
all_dates = []
mbox = mailbox.mbox(path)
for message in mbox:
    all_dates.append( str( parse( message['date'] ) ).split(' ')[0] )
Count and graph emails received per day

In [25]:
email_count = [(g[0], len(list(g[1]))) for g in itertools.groupby(all_dates)]

In [26]:
email_count[0]


Out[26]:
('2013-11-05', 1)

In [27]:
x = []
y = []
for date, count in email_count:
    x.append(date)
    y.append(count)

In [28]:
py.iplot( Data([ Scatter( x=x, y=y ) ]) )


Out[28]:
Restyle the chart in Plotly's GUI

In [10]:
import plotly.tools as tls
tls.embed('https://plot.ly/~jackp/3266')


Out[10]:
Custom css styling

In [11]:
from IPython.core.display import HTML
import urllib2
HTML(urllib2.urlopen('http://bit.ly/1Bf5Hft').read())


Out[11]: