{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###Conditional Formatting and CSS support for HTML in pandas\n", "\n", "This is a preview of functionality planned for inclusion in pandas 0.14.\n", "\n", "####Overview\n", "\n", "* We render HTML output using a template engine, Jinja2.\n", "* The template engine renders a data object using the specified template. \n", "* The data object (\"context\") we pass in contains a structured representation of the dataframe and, importantly, the css class names to attach to any given cell.\n", "* The classes attached to any given cell are the union of two sources:\n", " 1. *Required classes* are attached to table cells by default, as part of the standard context object constructed by pandas for a specific dataframe. Examples of classes are: \"col_heading\"/\"row_heading\"/\"data\", \"row0\",\"col3\". etc\n", " 2. An additional nested dict (`cell_context`, provisional name) is passed in which can optionally attach classes to specific cells. e.g. `{data:{0:{2:[\"foo\"]}` will attach the class \"foo\" to the the data cell at df.iloc[0,2].\n", "* since `cell_context` is constructed dynamically, we can construct \"styler\" functions that inspect the dataframe and attach arbitrary classes to various table cells by whatever logic we can express in code (i.e. anything).\n", "* The actual styling is done using inline css which is included in the rendered HTML. The template context contains a style property, also produced dynamically by code, which can specify any selector + css property/value pair to be included in the inline `\n", "
\n", " \n", " | \n", " \n", " | \n", " \n", " | C_l0_g0 | \n", " \n", "C_l0_g1 | \n", " \n", "C_l0_g2 | \n", " \n", "C_l0_g3 | \n", " \n", "C_l0_g4 | \n", " \n", "
---|---|---|---|---|---|---|---|
\n", " \n", " | \n", " \n", " | \n", " \n", " | C_l1_g0 | \n", " \n", "C_l1_g1 | \n", " \n", "C_l1_g2 | \n", " \n", "C_l1_g3 | \n", " \n", "C_l1_g4 | \n", " \n", "
R_l0_g0 | \n", " \n", "R_l1_g0 | \n", " \n", "R_l2_g0 | \n", " \n", "R0C0\n", " \n", " | R0C1\n", " \n", " | R0C2\n", " \n", " | R0C3\n", " \n", " | R0C4\n", " \n", " |
R_l0_g1 | \n", " \n", "R_l1_g1 | \n", " \n", "R_l2_g1 | \n", " \n", "R1C0\n", " \n", " | R1C1\n", " \n", " | R1C2\n", " \n", " | R1C3\n", " \n", " | R1C4\n", " \n", " |
R_l0_g2 | \n", " \n", "R_l1_g2 | \n", " \n", "R_l2_g2 | \n", " \n", "R2C0\n", " \n", " | R2C1\n", " \n", " | R2C2\n", " \n", " | R2C3\n", " \n", " | R2C4\n", " \n", " |
R_l0_g3 | \n", " \n", "R_l1_g3 | \n", " \n", "R_l2_g3 | \n", " \n", "R3C0\n", " \n", " | R3C1\n", " \n", " | R3C2\n", " \n", " | R3C3\n", " \n", " | R3C4\n", " \n", " |
R_l0_g4 | \n", " \n", "R_l1_g4 | \n", " \n", "R_l2_g4 | \n", " \n", "R4C0\n", " \n", " | R4C1\n", " \n", " | R4C2\n", " \n", " | R4C3\n", " \n", " | R4C4\n", " \n", " |
R_l0_g5 | \n", " \n", "R_l1_g5 | \n", " \n", "R_l2_g5 | \n", " \n", "R5C0\n", " \n", " | R5C1\n", " \n", " | R5C2\n", " \n", " | R5C3\n", " \n", " | R5C4\n", " \n", " |
R_l0_g6 | \n", " \n", "R_l1_g6 | \n", " \n", "R_l2_g6 | \n", " \n", "R6C0\n", " \n", " | R6C1\n", " \n", " | R6C2\n", " \n", " | R6C3\n", " \n", " | R6C4\n", " \n", " |
R_l0_g7 | \n", " \n", "R_l1_g7 | \n", " \n", "R_l2_g7 | \n", " \n", "R7C0\n", " \n", " | R7C1\n", " \n", " | R7C2\n", " \n", " | R7C3\n", " \n", " | R7C4\n", " \n", " |
R_l0_g8 | \n", " \n", "R_l1_g8 | \n", " \n", "R_l2_g8 | \n", " \n", "R8C0\n", " \n", " | R8C1\n", " \n", " | R8C2\n", " \n", " | R8C3\n", " \n", " | R8C4\n", " \n", " |
R_l0_g9 | \n", " \n", "R_l1_g9 | \n", " \n", "R_l2_g9 | \n", " \n", "R9C0\n", " \n", " | R9C1\n", " \n", " | R9C2\n", " \n", " | R9C3\n", " \n", " | R9C4\n", " \n", " |
\n", " \n", " | \n", " \n", " | \n", " \n", " | C_l0_g0 | \n", " \n", "C_l0_g1 | \n", " \n", "C_l0_g2 | \n", " \n", "C_l0_g3 | \n", " \n", "C_l0_g4 | \n", " \n", "
---|---|---|---|---|---|---|---|
\n", " \n", " | \n", " \n", " | \n", " \n", " | C_l1_g0 | \n", " \n", "C_l1_g1 | \n", " \n", "C_l1_g2 | \n", " \n", "C_l1_g3 | \n", " \n", "C_l1_g4 | \n", " \n", "
R_l0_g0 | \n", " \n", "R_l1_g0 | \n", " \n", "R_l2_g0 | \n", " \n", "R0C0\n", " \n", " | R0C1\n", " \n", " | R0C2\n", " \n", " | R0C3\n", " \n", " | R0C4\n", " \n", " |
R_l0_g1 | \n", " \n", "R_l1_g1 | \n", " \n", "R_l2_g1 | \n", " \n", "R1C0\n", " \n", " | R1C1\n", " \n", " | R1C2\n", " \n", " | R1C3\n", " \n", " | R1C4\n", " \n", " |
R_l0_g2 | \n", " \n", "R_l1_g2 | \n", " \n", "R_l2_g2 | \n", " \n", "R2C0\n", " \n", " | R2C1\n", " \n", " | R2C2\n", " \n", " | R2C3\n", " \n", " | R2C4\n", " \n", " |
R_l0_g3 | \n", " \n", "R_l1_g3 | \n", " \n", "R_l2_g3 | \n", " \n", "R3C0\n", " \n", " | R3C1\n", " \n", " | R3C2\n", " \n", " | R3C3\n", " \n", " | R3C4\n", " \n", " |
R_l0_g4 | \n", " \n", "R_l1_g4 | \n", " \n", "R_l2_g4 | \n", " \n", "R4C0\n", " \n", " | R4C1\n", " \n", " | R4C2\n", " \n", " | R4C3\n", " \n", " | R4C4\n", " \n", " |
R_l0_g5 | \n", " \n", "R_l1_g5 | \n", " \n", "R_l2_g5 | \n", " \n", "R5C0\n", " \n", " | R5C1\n", " \n", " | R5C2\n", " \n", " | R5C3\n", " \n", " | R5C4\n", " \n", " |
R_l0_g6 | \n", " \n", "R_l1_g6 | \n", " \n", "R_l2_g6 | \n", " \n", "R6C0\n", " \n", " | R6C1\n", " \n", " | R6C2\n", " \n", " | R6C3\n", " \n", " | R6C4\n", " \n", " |
R_l0_g7 | \n", " \n", "R_l1_g7 | \n", " \n", "R_l2_g7 | \n", " \n", "R7C0\n", " \n", " | R7C1\n", " \n", " | R7C2\n", " \n", " | R7C3\n", " \n", " | R7C4\n", " \n", " |
R_l0_g8 | \n", " \n", "R_l1_g8 | \n", " \n", "R_l2_g8 | \n", " \n", "R8C0\n", " \n", " | R8C1\n", " \n", " | R8C2\n", " \n", " | R8C3\n", " \n", " | R8C4\n", " \n", " |
R_l0_g9 | \n", " \n", "R_l1_g9 | \n", " \n", "R_l2_g9 | \n", " \n", "R9C0\n", " \n", " | R9C1\n", " \n", " | R9C2\n", " \n", " | R9C3\n", " \n", " | R9C4\n", " \n", " |
\n", " \n", " | \n", " \n", " | \n", " \n", " | C_l0_g0 | \n", " \n", "C_l0_g1 | \n", " \n", "C_l0_g2 | \n", " \n", "C_l0_g3 | \n", " \n", "C_l0_g4 | \n", " \n", "
---|---|---|---|---|---|---|---|
\n", " \n", " | \n", " \n", " | \n", " \n", " | C_l1_g0 | \n", " \n", "C_l1_g1 | \n", " \n", "C_l1_g2 | \n", " \n", "C_l1_g3 | \n", " \n", "C_l1_g4 | \n", " \n", "
R_l0_g0 | \n", " \n", "R_l1_g0 | \n", " \n", "R_l2_g0 | \n", " \n", "R0C0\n", " \n", " | R0C1\n", " \n", " | R0C2\n", " \n", " | R0C3\n", " \n", " | R0C4\n", " \n", " |
R_l0_g1 | \n", " \n", "R_l1_g1 | \n", " \n", "R_l2_g1 | \n", " \n", "R1C0\n", " \n", " | R1C1\n", " \n", " | R1C2\n", " \n", " | R1C3\n", " \n", " | R1C4\n", " \n", " |
R_l0_g2 | \n", " \n", "R_l1_g2 | \n", " \n", "R_l2_g2 | \n", " \n", "R2C0\n", " \n", " | R2C1\n", " \n", " | R2C2\n", " \n", " | R2C3\n", " \n", " | R2C4\n", " \n", " |
R_l0_g3 | \n", " \n", "R_l1_g3 | \n", " \n", "R_l2_g3 | \n", " \n", "R3C0\n", " \n", " | R3C1\n", " \n", " | R3C2\n", " \n", " | R3C3\n", " \n", " | R3C4\n", " \n", " |
R_l0_g4 | \n", " \n", "R_l1_g4 | \n", " \n", "R_l2_g4 | \n", " \n", "R4C0\n", " \n", " | R4C1\n", " \n", " | R4C2\n", " \n", " | R4C3\n", " \n", " | R4C4\n", " \n", " |
R_l0_g5 | \n", " \n", "R_l1_g5 | \n", " \n", "R_l2_g5 | \n", " \n", "R5C0\n", " \n", " | R5C1\n", " \n", " | R5C2\n", " \n", " | R5C3\n", " \n", " | R5C4\n", " \n", " |
R_l0_g6 | \n", " \n", "R_l1_g6 | \n", " \n", "R_l2_g6 | \n", " \n", "R6C0\n", " \n", " | R6C1\n", " \n", " | R6C2\n", " \n", " | R6C3\n", " \n", " | R6C4\n", " \n", " |
R_l0_g7 | \n", " \n", "R_l1_g7 | \n", " \n", "R_l2_g7 | \n", " \n", "R7C0\n", " \n", " | R7C1\n", " \n", " | R7C2\n", " \n", " | R7C3\n", " \n", " | R7C4\n", " \n", " |
R_l0_g8 | \n", " \n", "R_l1_g8 | \n", " \n", "R_l2_g8 | \n", " \n", "R8C0\n", " \n", " | R8C1\n", " \n", " | R8C2\n", " \n", " | R8C3\n", " \n", " | R8C4\n", " \n", " |
R_l0_g9 | \n", " \n", "R_l1_g9 | \n", " \n", "R_l2_g9 | \n", " \n", "R9C0\n", " \n", " | R9C1\n", " \n", " | R9C2\n", " \n", " | R9C3\n", " \n", " | R9C4\n", " \n", " |
\n", " \n", " | \n", " \n", " | \n", " \n", " | C_l0_g0 | \n", " \n", "C_l0_g1 | \n", " \n", "C_l0_g2 | \n", " \n", "C_l0_g3 | \n", " \n", "C_l0_g4 | \n", " \n", "
---|---|---|---|---|---|---|---|
\n", " \n", " | \n", " \n", " | \n", " \n", " | C_l1_g0 | \n", " \n", "C_l1_g1 | \n", " \n", "C_l1_g2 | \n", " \n", "C_l1_g3 | \n", " \n", "C_l1_g4 | \n", " \n", "
R_l0_g0 | \n", " \n", "R_l1_g0 | \n", " \n", "R_l2_g0 | \n", " \n", "R0C0\n", " \n", " | R0C1\n", " \n", " | R0C2\n", " \n", " | R0C3\n", " \n", " | R0C4\n", " \n", " |
R_l0_g1 | \n", " \n", "R_l1_g1 | \n", " \n", "R_l2_g1 | \n", " \n", "R1C0\n", " \n", " | R1C1\n", " \n", " | R1C2\n", " \n", " | R1C3\n", " \n", " | R1C4\n", " \n", " |
R_l0_g2 | \n", " \n", "R_l1_g2 | \n", " \n", "R_l2_g2 | \n", " \n", "R2C0\n", " \n", " | R2C1\n", " \n", " | R2C2\n", " \n", " | R2C3\n", " \n", " | R2C4\n", " \n", " |
R_l0_g3 | \n", " \n", "R_l1_g3 | \n", " \n", "R_l2_g3 | \n", " \n", "R3C0\n", " \n", " | R3C1\n", " \n", " | R3C2\n", " \n", " | R3C3\n", " \n", " | R3C4\n", " \n", " |
R_l0_g4 | \n", " \n", "R_l1_g4 | \n", " \n", "R_l2_g4 | \n", " \n", "R4C0\n", " \n", " | R4C1\n", " \n", " | R4C2\n", " \n", " | R4C3\n", " \n", " | R4C4\n", " \n", " |
R_l0_g5 | \n", " \n", "R_l1_g5 | \n", " \n", "R_l2_g5 | \n", " \n", "R5C0\n", " \n", " | R5C1\n", " \n", " | R5C2\n", " \n", " | R5C3\n", " \n", " | R5C4\n", " \n", " |
R_l0_g6 | \n", " \n", "R_l1_g6 | \n", " \n", "R_l2_g6 | \n", " \n", "R6C0\n", " \n", " | R6C1\n", " \n", " | R6C2\n", " \n", " | R6C3\n", " \n", " | R6C4\n", " \n", " |
R_l0_g7 | \n", " \n", "R_l1_g7 | \n", " \n", "R_l2_g7 | \n", " \n", "R7C0\n", " \n", " | R7C1\n", " \n", " | R7C2\n", " \n", " | R7C3\n", " \n", " | R7C4\n", " \n", " |
R_l0_g8 | \n", " \n", "R_l1_g8 | \n", " \n", "R_l2_g8 | \n", " \n", "R8C0\n", " \n", " | R8C1\n", " \n", " | R8C2\n", " \n", " | R8C3\n", " \n", " | R8C4\n", " \n", " |
R_l0_g9 | \n", " \n", "R_l1_g9 | \n", " \n", "R_l2_g9 | \n", " \n", "R9C0\n", " \n", " | R9C1\n", " \n", " | R9C2\n", " \n", " | R9C3\n", " \n", " | R9C4\n", " \n", " |
\n", " \n", " | C_l0_g0 | \n", " \n", "C_l0_g1 | \n", " \n", "C_l0_g2 | \n", " \n", "C_l0_g3 | \n", " \n", "C_l0_g4 | \n", " \n", "
---|---|---|---|---|---|
R_l0_g0 | \n", " \n", "0.211801747127\n", " \n", " | 0.208803651856\n", " \n", " | 0.743716623422\n", " \n", " | 0.962017341688\n", " \n", " | 0.176015213664\n", " \n", " |
R_l0_g1 | \n", " \n", "0.745491761131\n", " \n", " | 0.820494510091\n", " \n", " | 0.503137612941\n", " \n", " | 0.238019828149\n", " \n", " | 0.634250742128\n", " \n", " |
R_l0_g2 | \n", " \n", "0.569301755126\n", " \n", " | 0.507672767903\n", " \n", " | 0.309202712181\n", " \n", " | 0.830732729127\n", " \n", " | 0.617108870585\n", " \n", " |
R_l0_g3 | \n", " \n", "0.685515863149\n", " \n", " | 0.985786341696\n", " \n", " | 0.534988628544\n", " \n", " | 0.900187936559\n", " \n", " | 0.602678947775\n", " \n", " |
R_l0_g4 | \n", " \n", "0.521370083872\n", " \n", " | 0.0110884722823\n", " \n", " | 0.752014325914\n", " \n", " | 0.520270090798\n", " \n", " | 0.0095252937789\n", " \n", " |
R_l0_g5 | \n", " \n", "0.29865049643\n", " \n", " | 0.1156016189\n", " \n", " | 0.16250389458\n", " \n", " | 0.1162681165\n", " \n", " | 0.0624890733322\n", " \n", " |
R_l0_g6 | \n", " \n", "0.24039917756\n", " \n", " | 0.831887982065\n", " \n", " | 0.139444606684\n", " \n", " | 0.87434203059\n", " \n", " | 0.214273512954\n", " \n", " |
R_l0_g7 | \n", " \n", "0.0066042287924\n", " \n", " | 0.333568312336\n", " \n", " | 0.860660066709\n", " \n", " | 0.273793599297\n", " \n", " | 0.0247286858822\n", " \n", " |
R_l0_g8 | \n", " \n", "0.0372715722521\n", " \n", " | 0.996144666822\n", " \n", " | 0.258524096378\n", " \n", " | 0.0665443054498\n", " \n", " | 0.993389023817\n", " \n", " |