Saturday, 24 November 2012

Hide ipython notebook input-boxes

Last week I analyzed data for a customer, I used pandas to extract the information from a large collection of XMLs, I converted the result to html using nbconvert. I sent the html file to the customer without explaining what ipython notebook is.

The answer from my customer was: "That's a great analysis-document, nice tables, but the stuff between the tables is distracting."

On the Toggle Input Test page, you see how I removed that distracting stuff ;-). Click Toggle Input. (Will only work on blogger.com)

html_settings() extended_styles() creates the Toggle Input link and is part of my ipython notebook database display tools. It loads jQuery from CDN if not available, so in the converted html Toggle Input will only work when you're online.




Monday, 12 November 2012

Using pandas to print tables in ipython notebook

I blogged about the functions I wrote to print tables in the IPython notebook. Today a realized: pandas already has this functionality, actually it has everything I need for my data-analysis-tasks and a lot more. Here is how I print a sqlite table using pandas. It works with any kind of "table" data-structure.

In [6]:
import sys
sys.path.append("/Users/ganwell/Documents/Programming/edubs-import.src/import/lib/")
from adsy.ipython import *
import sqlite3
import pandas
import sys
In [7]:
db = sqlite3.connect("/Users/ganwell/Downloads/nw.db")
nbconvert will print part of the table outside the window. This fixes that.
In [8]:
HTML("""
        <style type="text/css">
            table.nowrap {
                margin-right: 80px;
            }
            table.dataframe {
                margin-right: 80px;
            }
        </style>""")
Out [8]:
Standard printformat for pandas is already good enough.
In [9]:
c = db.execute("""
SELECT * FROM regions
""")

df = pandas.DataFrame(list(c), columns=[x[0] for x in c.description])
df
Out [9]:
RegionID RegionDescription
0 1 Eastern is very long Region and a lot, meaning...
1 2 Westerns ...
2 3 Northern ...
3 4 Southern ...
Sometimes I need to see all data:
In [10]:
pandas.set_printoptions(max_colwidth=-1)
HTML(df.to_html())
Out [10]:
RegionID RegionDescription
0 1 Eastern is very long Region and a lot, meaning a awful lot of text. So long an thanks for the fish. DNA is the best!
1 2 Westerns
2 3 Northern
3 4 Southern
Here is what my printers look like.
In [13]:
c = db.execute("""
SELECT * FROM regions
""")
print_html(c)
Out [13]:
RegionIDRegionDescription
1Eastern is very long Region and a lot, meaning a awful lot of text. So long an thanks for the fish. DNA is the best!
2Westerns
3Northern
4Southern

Monday, 5 November 2012

IPython table display tools (updated)

I did the following changes on the IPython display module:

  • If parameter tight is True, it won't use <pre>, __repr() nor textwrapping. Which also means that non ASCII characters are printed as unicode.
  • print_html() knows about html_multi_dict() and will use it, if you pass something like [{'a' : 1}, {'a' : 2}]
  • enc() is now compatible with both python2 and python3
See the original post