{ "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", "{% if caption %}\n", "\n", "{% endif %}\n", "\n", " \n", "{% for r in head %}\n", " \n", " {% for c in r %} \n", " <{{c.type}} class=\"{{c.class}}\">{{c.value}}\n", " {% endfor %}\n", " \n", "{% endfor %}\n", " \n", " \n", " {% for r in body %}\n", " \n", " {% for c in r %} \n", " <{{c.type}} class=\"{{c.class}}\">{{c.value}}\n", " {% endfor %}\n", " \n", "{% endfor %}\n", " \n", "\n", "
\n", "{{caption}}\n", "
\n", "\n", "\"\"\")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# the implementation code. very small.\n", "ROW_HEADING_CLASS=\"row_heading\"\n", "COL_HEADING_CLASS=\"col_heading\"\n", "DATA_CLASS=\"data\"\n", "BLANK_CLASS=\"blank\"\n", "BLANK_VALUE=\"\"\n", "def translate(df,cell_context=None):\n", " import uuid\n", " cell_context = cell_context or dict()\n", "\n", " n_rlvls =df.index.nlevels\n", " n_clvls =df.columns.nlevels\n", " rlabels=df.index.tolist()\n", " clabels=df.columns.tolist()\n", " if n_rlvls == 1:\n", " rlabels = [[x] for x in rlabels]\n", " if n_clvls == 1:\n", " clabels = [[x] for x in clabels]\n", " clabels=zip(*clabels) \n", " head=[]\n", " for r in range(n_clvls): \n", " row_es = [{\"type\":\"th\",\"value\":BLANK_VALUE ,\"class\": \" \".join([BLANK_CLASS])}]*n_rlvls\n", " for c in range(len(clabels[0])):\n", " cs = [COL_HEADING_CLASS,\"level%s\" % r,\"col%s\" %c]\n", " cs.extend(cell_context.get(\"col_headings\",{}).get(r,{}).get(c,[]))\n", " row_es.append({\"type\":\"th\",\"value\": clabels[r][c],\"class\": \" \".join(cs)})\n", " head.append(row_es)\n", " body=[]\n", " for r in range(len(df)): \n", " cs = [ROW_HEADING_CLASS,\"level%s\" % c,\"row%s\" % r]\n", " cs.extend(cell_context.get(\"row_headings\",{}).get(r,{}).get(c,[]))\n", " row_es = [{\"type\":\"th\",\"value\": rlabels[r][c],\"class\": \" \".join(cs)} \n", " for c in range(len(rlabels[r]))]\n", " for c in range(len(df.columns)):\n", " cs = [DATA_CLASS,\"row%s\" % r,\"col%s\" %c]\n", " cs.extend(cell_context.get(\"data\",{}).get(r,{}).get(c,[]))\n", " row_es.append({\"type\":\"td\",\"value\": df.iloc[r][c],\"class\": \" \".join(cs)})\n", " body.append(row_es)\n", "\n", " # uuid required to isolate table styling from others \n", " # in same notebook in ipnb\n", " u = str(uuid.uuid1()).replace(\"-\",\"_\")\n", " return dict(head=head, body=body,uuid=u)\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Examples" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# first, vanilla\n", "df=mkdf(10,5,r_idx_nlevels=3,c_idx_nlevels=2)\n", "from IPython.display import HTML,display\n", "ctx= translate(df)\n", "ctx['caption']=\"Just a table, but rendered using a template with lots of classes to style against\"\n", "display(HTML(t.render(**ctx)))\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", "
\n", "Just a table, but rendered using a template with lots of classes to style against\n", "
C_l0_g0C_l0_g1C_l0_g2C_l0_g3C_l0_g4
C_l1_g0C_l1_g1C_l1_g2C_l1_g3C_l1_g4
R_l0_g0R_l1_g0R_l2_g0R0C0\n", " \n", " R0C1\n", " \n", " R0C2\n", " \n", " R0C3\n", " \n", " R0C4\n", " \n", "
R_l0_g1R_l1_g1R_l2_g1R1C0\n", " \n", " R1C1\n", " \n", " R1C2\n", " \n", " R1C3\n", " \n", " R1C4\n", " \n", "
R_l0_g2R_l1_g2R_l2_g2R2C0\n", " \n", " R2C1\n", " \n", " R2C2\n", " \n", " R2C3\n", " \n", " R2C4\n", " \n", "
R_l0_g3R_l1_g3R_l2_g3R3C0\n", " \n", " R3C1\n", " \n", " R3C2\n", " \n", " R3C3\n", " \n", " R3C4\n", " \n", "
R_l0_g4R_l1_g4R_l2_g4R4C0\n", " \n", " R4C1\n", " \n", " R4C2\n", " \n", " R4C3\n", " \n", " R4C4\n", " \n", "
R_l0_g5R_l1_g5R_l2_g5R5C0\n", " \n", " R5C1\n", " \n", " R5C2\n", " \n", " R5C3\n", " \n", " R5C4\n", " \n", "
R_l0_g6R_l1_g6R_l2_g6R6C0\n", " \n", " R6C1\n", " \n", " R6C2\n", " \n", " R6C3\n", " \n", " R6C4\n", " \n", "
R_l0_g7R_l1_g7R_l2_g7R7C0\n", " \n", " R7C1\n", " \n", " R7C2\n", " \n", " R7C3\n", " \n", " R7C4\n", " \n", "
R_l0_g8R_l1_g8R_l2_g8R8C0\n", " \n", " R8C1\n", " \n", " R8C2\n", " \n", " R8C3\n", " \n", " R8C4\n", " \n", "
R_l0_g9R_l1_g9R_l2_g9R9C0\n", " \n", " R9C1\n", " \n", " R9C2\n", " \n", " R9C3\n", " \n", " R9C4\n", " \n", "
\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "def zebra(color1, color2):\n", " return [dict(selector=\"td.data:nth-child(2n)\" ,\n", " props=[(\"background-color\",color1)]),\n", " dict(selector=\"td.data:nth-child(2n+1)\" ,\n", " props=[(\"background-color\",color2)])]\n", "\n", "ctx= translate(df)\n", "style=[]\n", "style.extend(zebra(\"#aaa\",\"#ddd\"))\n", "ctx['style']=style\n", "ctx['caption']=\"A zebra table\"\n", "display(HTML(t.render(**ctx)))\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", "
\n", "A zebra table\n", "
C_l0_g0C_l0_g1C_l0_g2C_l0_g3C_l0_g4
C_l1_g0C_l1_g1C_l1_g2C_l1_g3C_l1_g4
R_l0_g0R_l1_g0R_l2_g0R0C0\n", " \n", " R0C1\n", " \n", " R0C2\n", " \n", " R0C3\n", " \n", " R0C4\n", " \n", "
R_l0_g1R_l1_g1R_l2_g1R1C0\n", " \n", " R1C1\n", " \n", " R1C2\n", " \n", " R1C3\n", " \n", " R1C4\n", " \n", "
R_l0_g2R_l1_g2R_l2_g2R2C0\n", " \n", " R2C1\n", " \n", " R2C2\n", " \n", " R2C3\n", " \n", " R2C4\n", " \n", "
R_l0_g3R_l1_g3R_l2_g3R3C0\n", " \n", " R3C1\n", " \n", " R3C2\n", " \n", " R3C3\n", " \n", " R3C4\n", " \n", "
R_l0_g4R_l1_g4R_l2_g4R4C0\n", " \n", " R4C1\n", " \n", " R4C2\n", " \n", " R4C3\n", " \n", " R4C4\n", " \n", "
R_l0_g5R_l1_g5R_l2_g5R5C0\n", " \n", " R5C1\n", " \n", " R5C2\n", " \n", " R5C3\n", " \n", " R5C4\n", " \n", "
R_l0_g6R_l1_g6R_l2_g6R6C0\n", " \n", " R6C1\n", " \n", " R6C2\n", " \n", " R6C3\n", " \n", " R6C4\n", " \n", "
R_l0_g7R_l1_g7R_l2_g7R7C0\n", " \n", " R7C1\n", " \n", " R7C2\n", " \n", " R7C3\n", " \n", " R7C4\n", " \n", "
R_l0_g8R_l1_g8R_l2_g8R8C0\n", " \n", " R8C1\n", " \n", " R8C2\n", " \n", " R8C3\n", " \n", " R8C4\n", " \n", "
R_l0_g9R_l1_g9R_l2_g9R9C0\n", " \n", " R9C1\n", " \n", " R9C2\n", " \n", " R9C3\n", " \n", " R9C4\n", " \n", "
\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "def tag_col(n,c=\"grey10\", with_headings=False):\n", " selector=\"td.col%d\" % n\n", " if not with_headings:\n", " selector+=\".data\"\n", " return [dict(selector=selector,\n", " props=[(\"background-color\",c)])]\n", "\n", "def tag_row(n,c=\"grey10\", with_headings=False):\n", " selector=\"td.row%d\" % n\n", " if not with_headings:\n", " selector+=\".data\"\n", " return [dict(selector=selector,\n", " props=[(\"background-color\",c)])]\n", "\n", "ctx= translate(df)\n", "style=[]\n", "style.extend(tag_col(2,\"beige\"))\n", "style.extend(tag_row(3,\"purple\"))\n", "ctx['style']=style\n", "ctx['caption']=\"Highlight rows/cols by index\"\n", "display(HTML(t.render(**ctx)))\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", "
\n", "Highlight rows/cols by index\n", "
C_l0_g0C_l0_g1C_l0_g2C_l0_g3C_l0_g4
C_l1_g0C_l1_g1C_l1_g2C_l1_g3C_l1_g4
R_l0_g0R_l1_g0R_l2_g0R0C0\n", " \n", " R0C1\n", " \n", " R0C2\n", " \n", " R0C3\n", " \n", " R0C4\n", " \n", "
R_l0_g1R_l1_g1R_l2_g1R1C0\n", " \n", " R1C1\n", " \n", " R1C2\n", " \n", " R1C3\n", " \n", " R1C4\n", " \n", "
R_l0_g2R_l1_g2R_l2_g2R2C0\n", " \n", " R2C1\n", " \n", " R2C2\n", " \n", " R2C3\n", " \n", " R2C4\n", " \n", "
R_l0_g3R_l1_g3R_l2_g3R3C0\n", " \n", " R3C1\n", " \n", " R3C2\n", " \n", " R3C3\n", " \n", " R3C4\n", " \n", "
R_l0_g4R_l1_g4R_l2_g4R4C0\n", " \n", " R4C1\n", " \n", " R4C2\n", " \n", " R4C3\n", " \n", " R4C4\n", " \n", "
R_l0_g5R_l1_g5R_l2_g5R5C0\n", " \n", " R5C1\n", " \n", " R5C2\n", " \n", " R5C3\n", " \n", " R5C4\n", " \n", "
R_l0_g6R_l1_g6R_l2_g6R6C0\n", " \n", " R6C1\n", " \n", " R6C2\n", " \n", " R6C3\n", " \n", " R6C4\n", " \n", "
R_l0_g7R_l1_g7R_l2_g7R7C0\n", " \n", " R7C1\n", " \n", " R7C2\n", " \n", " R7C3\n", " \n", " R7C4\n", " \n", "
R_l0_g8R_l1_g8R_l2_g8R8C0\n", " \n", " R8C1\n", " \n", " R8C2\n", " \n", " R8C3\n", " \n", " R8C4\n", " \n", "
R_l0_g9R_l1_g9R_l2_g9R9C0\n", " \n", " R9C1\n", " \n", " R9C2\n", " \n", " R9C3\n", " \n", " R9C4\n", " \n", "
\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "def round_corners(radius):\n", " props_bl=[ \n", " (\"-moz-border-radius-bottomleft\", \"%dpx\" % radius ),\n", " (\"-webkit-border-bottom-left-radius\", \"%dpx\" % radius ),\n", " (\"border-bottom-left-radius\", \"%dpx\" % radius )\n", " ]\n", " props_br=[ \n", " (\"-moz-border-radius-bottomright\", \"%dpx\" % radius ),\n", " (\"-webkit-border-bottom-right-radius\", \"%dpx\" % radius ),\n", " (\"border-bottom-right-radius\", \"%dpx\" % radius )\n", " ]\n", " props_tl=[ \n", " (\"-moz-border-radius-topleft\", \"%dpx\" % radius ),\n", " (\"-webkit-border-top-left-radius\", \"%dpx\" % radius ),\n", " (\"border-top-left-radius\", \"%dpx\" % radius )\n", " ]\n", " props_tr=[ \n", " (\"-moz-border-radius-topright\", \"%dpx\" % radius ),\n", " (\"-webkit-border-top-right-radius\", \"%dpx\" % radius ),\n", " (\"border-top-right-radius\", \"%dpx\" % radius )\n", " ] \n", " \n", " \n", " return [dict(selector=\"td\",\n", " props=[(\"border-width\",\"1px\")]),\n", " dict(selector=\"\",\n", " props=[(\"border-collapse\",\"separate\")]),\n", " dict(selector=\"tr:last-child th:first-child\",\n", " props=props_bl),\n", " dict(selector=\"tr:last-child td:last-child\",\n", " props=props_br),\n", " dict(selector=\"tr:first-child th.col0\",\n", " props=props_tl),\n", " dict(selector=\"tr:first-child th.row0:first-child\",\n", " props=props_tl), \n", " dict(selector=\"tr:first-child th:last-child\",\n", " props=props_tr),\n", " ]\n", " \n", "ctx= translate(df)\n", "style=[]\n", "style.extend(round_corners(5))\n", "\n", "ctx['caption']=\"Rounded corners. CSS skills beginning to fail.\"\n", "ctx['style']=style\n", "display(HTML(t.render(**ctx)))\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", "
\n", "Rounded corners. CSS skills beginning to fail.\n", "
C_l0_g0C_l0_g1C_l0_g2C_l0_g3C_l0_g4
C_l1_g0C_l1_g1C_l1_g2C_l1_g3C_l1_g4
R_l0_g0R_l1_g0R_l2_g0R0C0\n", " \n", " R0C1\n", " \n", " R0C2\n", " \n", " R0C3\n", " \n", " R0C4\n", " \n", "
R_l0_g1R_l1_g1R_l2_g1R1C0\n", " \n", " R1C1\n", " \n", " R1C2\n", " \n", " R1C3\n", " \n", " R1C4\n", " \n", "
R_l0_g2R_l1_g2R_l2_g2R2C0\n", " \n", " R2C1\n", " \n", " R2C2\n", " \n", " R2C3\n", " \n", " R2C4\n", " \n", "
R_l0_g3R_l1_g3R_l2_g3R3C0\n", " \n", " R3C1\n", " \n", " R3C2\n", " \n", " R3C3\n", " \n", " R3C4\n", " \n", "
R_l0_g4R_l1_g4R_l2_g4R4C0\n", " \n", " R4C1\n", " \n", " R4C2\n", " \n", " R4C3\n", " \n", " R4C4\n", " \n", "
R_l0_g5R_l1_g5R_l2_g5R5C0\n", " \n", " R5C1\n", " \n", " R5C2\n", " \n", " R5C3\n", " \n", " R5C4\n", " \n", "
R_l0_g6R_l1_g6R_l2_g6R6C0\n", " \n", " R6C1\n", " \n", " R6C2\n", " \n", " R6C3\n", " \n", " R6C4\n", " \n", "
R_l0_g7R_l1_g7R_l2_g7R7C0\n", " \n", " R7C1\n", " \n", " R7C2\n", " \n", " R7C3\n", " \n", " R7C4\n", " \n", "
R_l0_g8R_l1_g8R_l2_g8R8C0\n", " \n", " R8C1\n", " \n", " R8C2\n", " \n", " R8C3\n", " \n", " R8C4\n", " \n", "
R_l0_g9R_l1_g9R_l2_g9R9C0\n", " \n", " R9C1\n", " \n", " R9C2\n", " \n", " R9C3\n", " \n", " R9C4\n", " \n", "
\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "def color_class(cls, color):\n", " return [dict(selector=\"td.%s\" % cls ,\n", " props=[(\"background-color\",color)])]\n", "def rank_col(n,ranking,u):\n", " data = {i: {n: [\"%s-%s\" % (u,ranking[i])]} for i in range(len(ranking))}\n", " return {\"data\": data}\n", "\n", "import uuid\n", "u = \"U\"+str(uuid.uuid1()).replace(\"-\",\"_\")\n", "df=mkdf(9,5,data_gen_f=lambda r,c:np.random.random())\n", "\n", "ranking=df.iloc[:,1].argsort().tolist()\n", "cell_context=rank_col(1, ranking, u)\n", "\n", "ctx= translate(df,cell_context)\n", "style=[]\n", "# http://colorbrewer2.org/\n", "color_scale=[\"#fff7ec\",\"#fee8c8\",\"#fdd49e\",\"#fdbb84\",\"#fc8d59\",\"#ef6548\",\"#d7301f\",\"#b30000\",\"#7f0000\"]\n", "for intensity in range(9):\n", " style.extend(color_class(\"%s-%s\" % (u,intensity),color_scale[intensity]))\n", " \n", "ctx['style']=style\n", "ctx['caption']=\"And finally, a heatmap based on values\"\n", "display(HTML(t.render(**ctx)))\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", "
\n", "And finally, a heatmap based on values\n", "
C_l0_g0C_l0_g1C_l0_g2C_l0_g3C_l0_g4
R_l0_g00.211801747127\n", " \n", " 0.208803651856\n", " \n", " 0.743716623422\n", " \n", " 0.962017341688\n", " \n", " 0.176015213664\n", " \n", "
R_l0_g10.745491761131\n", " \n", " 0.820494510091\n", " \n", " 0.503137612941\n", " \n", " 0.238019828149\n", " \n", " 0.634250742128\n", " \n", "
R_l0_g20.569301755126\n", " \n", " 0.507672767903\n", " \n", " 0.309202712181\n", " \n", " 0.830732729127\n", " \n", " 0.617108870585\n", " \n", "
R_l0_g30.685515863149\n", " \n", " 0.985786341696\n", " \n", " 0.534988628544\n", " \n", " 0.900187936559\n", " \n", " 0.602678947775\n", " \n", "
R_l0_g40.521370083872\n", " \n", " 0.0110884722823\n", " \n", " 0.752014325914\n", " \n", " 0.520270090798\n", " \n", " 0.0095252937789\n", " \n", "
R_l0_g50.29865049643\n", " \n", " 0.1156016189\n", " \n", " 0.16250389458\n", " \n", " 0.1162681165\n", " \n", " 0.0624890733322\n", " \n", "
R_l0_g60.24039917756\n", " \n", " 0.831887982065\n", " \n", " 0.139444606684\n", " \n", " 0.87434203059\n", " \n", " 0.214273512954\n", " \n", "
R_l0_g70.0066042287924\n", " \n", " 0.333568312336\n", " \n", " 0.860660066709\n", " \n", " 0.273793599297\n", " \n", " 0.0247286858822\n", " \n", "
R_l0_g80.0372715722521\n", " \n", " 0.996144666822\n", " \n", " 0.258524096378\n", " \n", " 0.0665443054498\n", " \n", " 0.993389023817\n", " \n", "
\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }