Using JQuery DataTables with IPython

I thought this might be interesting enough to share with you. Every time I'm working with DataFrames I somehow miss the search feature: I'd like to search for certain patterns inside the columns and rows. I used to use JQuery DataTables for netgrafio. But I couldn't find any simple way to integrate it with IPython. Well it was easier than I thought.

In [ ]:
 

Extensions

In [236]:
Expand Code

DataTable function

In [259]:
Expand Code
In [250]:
 

I know the code is not perfect, but at least it works for me. Now let's create some random DataFrame:

In [247]:
import pandas as pd
import urllib2
from yurl import URL


# Fetch list of random URLs (found using Google)
response = urllib2.urlopen('http://files.ianonavy.com/urls.txt')
targets_row = response.read()

# Create DataFrame
targets = pd.DataFrame([t for t in targets_row.splitlines()], columns=["Target"])

# Join root domain + suffix
extract_root_domain =  lambda x: '.'.join(tldextract.extract(x)[1:3])

target_columns = ['scheme', 'userinfo', 'host', 'port', 'path', 'query', 'fragment', 'decoded']
target_component = [list(URL(t)) for t in targets['Target']]

# Create data frame
df_targets = pd.DataFrame(target_component, columns=target_columns)

Classic HTML output

In [239]:
df_targets[:20]
Out[239]:
scheme userinfo host port path query fragment decoded
0 http www.altpress.org / False
1 http www.nzfortress.co.nz False
2 http www.evillasforsale.com False
3 http www.playingenemy.com / False
4 http www.richardsonscharts.com False
5 http www.xenith.net False
6 http www.tdbrecords.com False
7 http www.electrichumanproject.com / False
8 http tweekerchick.blogspot.com / False
9 http www.besound.com /pushead/home.html False
10 http www.porkchopscreenprinting.com / False
11 http www.kinseyvisual.com False
12 http www.rathergood.com False
13 http www.lepoint.fr / False
14 http www.revhq.com False
15 http www.poprocksandcoke.com False
16 http www.samuraiblue.com / False
17 http www.openbsd.org /cgi-bin/man.cgi False
18 http www.sysblog.com False
19 http www.voicesofsafety.com False

JQuery DataTables output

In [260]:
DataTable(df_targets[:20])
Out[260]:
scheme userinfo host port path query fragment decoded
http www.altpress.org / False
http www.nzfortress.co.nz False
http www.evillasforsale.com False
http www.playingenemy.com / False
http www.richardsonscharts.com False
http www.xenith.net False
http www.tdbrecords.com False
http www.electrichumanproject.com / False
http tweekerchick.blogspot.com / False
http www.besound.com /pushead/home.html False
http www.porkchopscreenprinting.com / False
http www.kinseyvisual.com False
http www.rathergood.com False
http www.lepoint.fr / False
http www.revhq.com False
http www.poprocksandcoke.com False
http www.samuraiblue.com / False
http www.openbsd.org /cgi-bin/man.cgi False
http www.sysblog.com False
http www.voicesofsafety.com False

Jinja2 cellmagic

In [242]:
html_output = DataTable(df_targets[:20])
In [211]:
%%jinja html
<div id="table-container">
    {{ html_output }}
</div>
Out[211]:
In [243]:
html_output
Out[243]:
scheme userinfo host port path query fragment decoded
http www.altpress.org / False
http www.nzfortress.co.nz False
http www.evillasforsale.com False
http www.playingenemy.com / False
http www.richardsonscharts.com False
http www.xenith.net False
http www.tdbrecords.com False
http www.electrichumanproject.com / False
http tweekerchick.blogspot.com / False
http www.besound.com /pushead/home.html False
http www.porkchopscreenprinting.com / False
http www.kinseyvisual.com False
http www.rathergood.com False
http www.lepoint.fr / False
http www.revhq.com False
http www.poprocksandcoke.com False
http www.samuraiblue.com / False
http www.openbsd.org /cgi-bin/man.cgi False
http www.sysblog.com False
http www.voicesofsafety.com False

Easy isn't it? Thx for sharing.


Prev: HowTo: Debug Android APKs with Eclipse and DDMS
Next: HowTo: Androguard with Docker

comments powered by Disqus
Published:
2014-08-28 00:00
category:
Tag: