[IPython-dev] Visual representation of SQL extracted data

Simon Cropper simoncropper at fossworkflowguides.com
Thu May 15 21:39:03 EDT 2014

On 16/05/14 11:11, Mark Voorhies wrote:
> On 05/14/2014 10:55 PM, Simon Cropper wrote:
>> Hi,
>> New to iPython and the standard set of tools bound to the server (e.g.
>> Pandas, etc).
>> What I was wondering is there a library, or a notebook showing the use
>> of the standard libraries bound to iPython, that allows tabular data to
>> be formatted.
>> Most examples I can find are essentially text dumps to the notebook. In
>> contrast, graphs and images have great formatting ability.
>> Why? I am looking at using iPython to data wrangle or munge large
>> biological datasets (plant names, biological and ecological attributes,
>> locational data). The notebook feature will allow others to visualize
>> and follow steps used to collate, convert, merge, summarize and analyze
>> this data.
>> Is this doable, or am I trying to use the notebook in the wrong way?
>> Any feedback would be appreciated.
> I think Pandas is good at this, but I haven't played with it.
> A general strategy is to use HTML from IPython.core.display to render pretty HTML tables.
> Here's an example for tabular display of protein sequence alignments (with a horizontal
> scrollbar and green/red coloring of gaps/mutations):
> def tabformat(seqs, names = None):
>       """Return the given sequence alignment as a pretty HTML table.
>       seqs = list of strings with a common length (one letter amino-acid codes or "-" for gap)
>       names = sequence names as list of strings, same length as seqs
>       """
>       if(names is None):
>           names = [str(i) for i in xrange(len(seqs))]
>       retval = '<DIV style="font-family: monospace; font-size: small">\n'
>       retval += '<DIV><TABLE style="overflow: auto; position: relative; float: left; width: 8em">\n'
>       for n in names:
>           retval += "<TR><TD>%s</TD></TR>\n" % n
>       retval += "</TABLE></DIV>\n"
>       retval += '<DIV style="overflow: auto; width: 90em; position: relative; float: left"><TABLE>\n'
>       for i in seqs:
>           retval += "<TR>\n"
>           for (j,k) in zip(i,seqs[0]):
>               if(j != k):
>                   if(j == "-"):
>                       c = 'style="background-color: green"'
>                   else:
>                       c = 'style="background-color: red"'
>               else:
>                   c = ""
>               retval += '<TD %s>%s</TD>' % (c,j)
>           retval += "</TR>\n"
>       retval += "</TABLE></DIV>\n"
>       retval += "</DIV>\n"
>       return retval
> from IPython.core.display import HTML
> HTML(data = tabformat([i*100 for i in ("SPAM", "SPAM", "SCAM", "SPAM", "SPA-", "SPAM", "SPAN")]))
> --Mark
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev

Thanks Mark,

I am looking at Pandas at the moment.

I tried your script and it works really well. To date I have generally 
coded my own HTML code like yours, saved it in a file then opened it in 
a browser. The problem was that the data was static -- that is you could 
not poke the browser to refresh once you reran the script (at least in 
Linux; in Windows you would use automation to open an instance then use 
the refresh method using VBA or something similar but I am looking for 
cross-platform solutions).

What I really like about your example and the iPython Notebook is that I 
can change the sequence and have the HTML table dynamically update. This 
allows for errors to be detected quite quickly.

Just a follow on question. I note the HTML table rendered within a 
'frame'. That is, the horizontal scroll bar appeared. Is this 
object/frame able to be changed or controlled?

Cheers Simon

    Simon Cropper - Open Content Creator

    Free and Open Source Software Workflow Guides
    Introduction               http://www.fossworkflowguides.com
    GIS Packages           http://www.fossworkflowguides.com/gis
    bash / Python    http://www.fossworkflowguides.com/scripting

More information about the IPython-dev mailing list