Graph Gmail Inbox Data in Python/v3
Learn how to graph your Gmail inbox data with plotly and IPython Notebook
Note: this page is part of the documentation for version 3 of Plotly.py, which is not the most recent version.
See our Version 4 Migration Guide for information about how to upgrade.
See our Version 4 Migration Guide for information about how to upgrade.
In [31]:
from IPython.display import Image
Image('http://i.imgur.com/SYija2N.png')
Out[31]:
Installations¶
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 plotly.plotly as py
import plotly.graph_objs as go
import mailbox
from email.utils import parsedate
from dateutil.parser import parse
import itertools
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 [26]:
email_count = [(g[0], len(list(g[1]))) for g in itertools.groupby(all_dates)]
email_count[0]
Out[26]:
In [28]:
x = []
y = []
for date, count in email_count:
x.append(date)
y.append(count)
py.iplot( [ go.Scatter( x=x, y=y ) ] )
Out[28]:
Restyle the chart in Plotly's GUI¶
In [10]:
import plotly.tools as tls
tls.embed('https://plotly.com/~jackp/3266')
Out[10]: