{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# HTML Tables and Table Formatting\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a HTML Table from pandas.DataFrame\n", "The following is hopefully sufficient for most applications (feedback welcome!):\n", "```\n", "from pybloqs import Blockb = Block(df)\n", "```\n", "\n", "When called only with DataFrame as parameter, a set of default formatters is applied: \n", "```\n", "table_block = Block(df, formatters=None, use_default_formatters=True)\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "import pybloqs as abl\n", "from pybloqs import Block\n", "import pandas as pd\n", "import numpy as np\n", "df = pd.DataFrame(np.random.rand(4,4), index=['a','b','c','d'], columns = ['aa','bb','cc','dd'])\n", "df.index.name = 'ABC'\n", "Block(df)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NB: The grid between cells is from jupyter default CSS. It will not show if the block is displayed with b.show() ." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Formatting Tables with Table Formatters\n", "Formatters are functions which add a single specific formatting aspect (e.g. bold, font-size, alignment, multi-index display). Formatters can be stacked together as a list to produce desired layout. The list is then passed to ```HTMLJinjaTableBlock```.\n", "\n", "Use of default formatters can be disabled completely. Additional formatters can be used on top or instead of default formatters.\n", "Formatters change appearance by modifying cell values and adding CSS styles.\n", "\n", "'Exotic' formatters, which are used only in a single context, can be defined locally.\n", "A set of general use formatters can be found in pybloqs.block.table_formatters.\n", "\n", "All formatters take the following parameters:\n", "```\n", ":rows List of rows, where formatter should be applied\n", ":columns List of columns, where formatter should be applied\n", ":apply_to_header_and_index True/False, if set to True, formatter will be applied to all index and header cells\n", "```\n", "If rows and columns are both ```None```, formatter is applied to all cells in table.\n", "\n", "An example:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/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", " ABC\n", " \n", " aa\n", " \n", " bb\n", " \n", " cc\n", " \n", " dd\n", "
\n", " a\n", " \n", " 0.69\n", " \n", " 0.41\n", " \n", " 0.26\n", " \n", " 0.13\n", "
\n", " b\n", " \n", " 0.50\n", " \n", " 0.68\n", " \n", " 0.94\n", " \n", " 0.67\n", "
\n", " c\n", " \n", " 0.60\n", " \n", " 0.71\n", " \n", " 0.07\n", " \n", " 0.92\n", "
\n", " d\n", " \n", " 0.35\n", " \n", " 0.48\n", " \n", " 0.22\n", " \n", " 0.88\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " ABC\n", " \n", " aa\n", " \n", " bb\n", " \n", " cc\n", " \n", " dd\n", "
\n", " a\n", " \n", " 0.6852474251934583\n", " \n", " 0.41120411129050705\n", " \n", " 0.26466502351221766\n", " \n", " 0.13038676806147742\n", "
\n", " b\n", " \n", " 0.5009290828732462\n", " \n", " 0.6756706339375467\n", " \n", " 0.9411918781685857\n", " \n", " 0.6719157166512993\n", "
\n", " c\n", " \n", " 0.6010710732450969\n", " \n", " 0.7148296329613193\n", " \n", " 0.07250899622251417\n", " \n", " 0.9247174989893737\n", "
\n", " d\n", " \n", " 0.35060978076583094\n", " \n", " 0.4773133603344051\n", " \n", " 0.22375109653340475\n", " \n", " 0.878994223447428\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " ABC\n", " \n", " aa\n", " \n", " bb\n", " \n", " cc\n", " \n", " dd\n", "
\n", " a\n", " \n", " 0.69\n", " \n", " 41.1%\n", " \n", " 26.5%\n", " \n", " 0.13\n", "
\n", " b\n", " \n", " 0.50\n", " \n", " 67.6%\n", " \n", " 94.1%\n", " \n", " 0.67\n", "
\n", " c\n", " \n", " 0.60\n", " \n", " 71.5%\n", " \n", " 7.3%\n", " \n", " 0.92\n", "
\n", " d\n", " \n", " 0.35\n", " \n", " 47.7%\n", " \n", " 22.4%\n", " \n", " 0.88\n", "
\n", " Total\n", " \n", " 2.14\n", " \n", " \n", " \n", " 2.61\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " ABC\n", " \n", " aa\n", " \n", " bb\n", " \n", " cc\n", " \n", " dd\n", "
\n", " a\n", " \n", " 685,247\n", " \n", " 411,204\n", " \n", " 264,665\n", " \n", " 130,387\n", "
\n", " b\n", " \n", " 500,929\n", " \n", " 675,671\n", " \n", " 941,192\n", " \n", " 671,916\n", "
\n", " c\n", " \n", " 601,071\n", " \n", " 714,830\n", " \n", " 72,509\n", " \n", " 924,717\n", "
\n", " d\n", " \n", " 350,610\n", " \n", " 477,313\n", " \n", " 223,751\n", " \n", " 878,994\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pybloqs.block.table_formatters as tf\n", "table_block = Block(df)\n", "table_block_raw = Block(df, use_default_formatters=False)\n", "\n", "fmt_pct = tf.FmtPercent(1, columns=['bb','cc'], apply_to_header_and_index=False)\n", "fmt_totals = tf.FmtAppendTotalsRow(total_columns=['aa','dd'])\n", "fmt_highlight = tf.FmtHighlightText(columns=['bb'], rows=['d'], apply_to_header_and_index=False)\n", "formatters=[fmt_pct,fmt_totals, fmt_highlight]\n", "table_block_additional_formatters = Block(df, formatters=formatters)\n", "\n", "fmt_mult = tf.FmtMultiplyCellValue(1e6, '')\n", "fmt_sep = tf.FmtThousandSeparator()\n", "formatters=[fmt_mult, fmt_sep]\n", "table_block_new_formatters = Block(df, formatters=formatters, use_default_formatters=False)\n", "\n", "row1 = abl.HStack([table_block, table_block_raw])\n", "row2 = abl.HStack([table_block_additional_formatters, table_block_new_formatters])\n", "\n", "abl.VStack([row1,row2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### General formatters\n", "The following formatters handle miscalleneous general tasks\n", "#### Replace NaN\n", "```\n", "FmtReplaceNaN(value='')\n", "```\n", "Replaces all np.nan values in specified range with provided ```value```. By default uses empty string.\n", "\n", "#### FmtAlignCellContents\n", "```\n", "FmtAlignCellContents(alignment='center')\n", "```\n", "Aligns content inside cells within specified range. Valid values for ```alignment``` are ```left|right|center|justify|initial|inherit``` (anything that the CSS tag ```text-align``` understands).\n", "\n", "#### FmtAlignTable\n", "```\n", "FmtAlignTable(alignment)\n", "```\n", "Aligns the entire table relative to its entironment. Valid values for alignment are ```center```, ```right```, ```left```.\n", "\n", "#### FmtHeader\n", "```\n", "FmtHeader(fixed_width='100%', index_width=None, column_width=None, rotate_deg=0,top_padding=None, no_wrap=True)\n", "```\n", "Creates a table with fixed-width columns. \n", "```\n", ":fixed_width Total width of table\n", ":index_width Fixed width of index column\n", ":column_width Fixed width of all other columns\n", ":rotate_deg Value in degrees by which to rotate table header cells\n", ":top_padding: additional vertical space above table, may be necessary when using rotated headers\n", ":no_wrap True/False, disables line-breaks within header cell when set to True\n", "```\n", "An example (NB, jypiter ignores top-padding, which otherwise works in direkt browser display and PDF output):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/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", " ABC\n", " \n", " aa\n", " \n", " bb\n", " \n", " cc\n", " \n", " dd\n", "
\n", " a\n", " \n", " 0.69\n", " \n", " 0.41\n", " \n", " 0.26\n", " \n", " 0.13\n", "
\n", " b\n", " \n", " 0.50\n", " \n", " 0.68\n", " \n", " 0.94\n", " \n", " 0.67\n", "
\n", " c\n", " \n", " 0.60\n", " \n", " 0.71\n", " \n", " 0.07\n", " \n", " 0.92\n", "
\n", " d\n", " \n", " 0.35\n", " \n", " 0.48\n", " \n", " 0.22\n", " \n", " 0.88\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fmt_header = tf.FmtHeader(fixed_width='20cm',index_width='30%', top_padding='3cm', rotate_deg=30)\n", "Block(df, formatters=[fmt_header])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### FmtStripeBackground\n", "```\n", "FmtStripeBackground(first_color=colors.LIGHT_GREY, second_color=colors.WHITE, header_color=colors.WHITE,\n", "```\n", "Creates a repeating color patters in the background of the specified cells.\n", "```\n", ":first_color CSS color, for odd row numbers\n", ":second_color CSS color, for even row numbers\n", ":header_color CSS color applied to header row\n", "```\n", "#### FmtAddCellPadding\n", "```\n", "FmtAddCellPadding(left=None, right=None, top=None, bottom=None, length_unit='px')\n", "```\n", "Add space on sides of selected cells.\n", "#### FmtAppendTotalsRow\n", "```\n", "FmtAppendTotalsRow(row_name='Total', operator=OP_SUM, bold=True, background_color=colors.LIGHT_GREY, font_color=None, total_columns=None)\n", "```\n", "Adds a line at the end of the table filled with values computed columnwise. For an example, see section [Formatting Tables with Table Formatters]\n", "```\n", ":row_name Label for additional row shown in index\n", ":operator Computational operation to perform on columns, e.g. tf.OP_SUM, tf.OP_MEAN, tf.OP_NONE\n", ":total_columns Names of columns to apply operator to. If None, operator is applied to all columns.\n", ":bold True/False, applied bold font-style to cells in added row\n", ":font_color CSS color for cell text in added row\n", ":background_color CSS color for cell background in added row\n", "```\n", "#### FmtHideRows\n", "```\n", "FmtHideCells(rows=None, columns=None)\n", "```\n", "Hides cells in the intersection of rows and columns list. If only rows or columns is specified and the other is left None, the entire row or column is hidden.\n", "#### FmtPageBreak\n", "```\n", "FmtPageBreak(no_break=True, repeat_header=True)\n", "```\n", "Defines table behaviour around page-breaks. Please note that Webkit-based browsers (Chrome, Safari and wkhtmltopdf as well) do not handle the ```repeat-header``` property properly, especially when headers are rotated. This bug is reported and not resolved since 6 years. Functionality in Firefox is fine, including rotated headers.\n", "```\n", ":no_break True/False, avoids splitting table on page break\n", ":repeat_header True/False, when table is split accross page, repeat header on the next page\n", "```\n", "\n", "### Displaying text\n", "The following formatters modify the display of both text and numbers.\n", "#### FmtFontsize\n", "```\n", "FmtFontsize(fontsize, format='px')\n", "```\n", "Sets the font-size property of specified cells. A nice property is ```vw```, which gives the font-size as percent of viewport-width. This will scale the font with the witdh of the page and is thus suited for wide tables which should still look fine (but small) when output as PDF.\n", "\n", "#### FmtHighlightText\n", "```\n", "FmtHighlightText(bold=True, italic=True, font_color=colors.BLUE)\n", "```\n", "Sets various properties of character display.\n", "```\n", ":bold True/False, sets bold font-style\n", ":italic True/False, sets italic font-style\n", ":font_color CSS color, sets text color\n", "```\n", "#### FmtHighlightBackground\n", "```\n", "FmtHighlightBackground(color=colors.RED)\n", "```\n", "Sets the background color of specified cells.\n", "\n", "#### FmtBold\n", "```\n", "FmtBold()\n", "```\n", "Sets font-style bold in specified cells.\n", "\n", "### Displaying numbers\n", "The following formatters only apply to numbers. Please note that some convert the number to a string when applied.\n", "\n", "#### FmtNumbers, FmtDecimals, FmtThousandSeparator, FmtPercent\n", "E.g.\n", "```\n", "FmtPercent(n_decimals)\n", "```\n", "If cell content is a number, it is changed to a string with approriate formatting, e.g. number of decimals (FmtDecimals), with a comma as thousands separator (FmtThousandSeparator), or as percent with a trailing '%' sign(FmtPercent).\n", "```\n", "FmtNumbers(fmt_string)\n", "```\n", "is the general purpose formatting class, which accets any formatting string. For more information about formatting strings, see https://pyformat.info/\n", "\n", "#### FmtMultiplyCellValue, FmtValueToMillion, FmtValueToPercent, FmtValueToBps\n", "E.g.\n", "```\n", "FmtValueToPercent(suffix='%')\n", "```\n", "Multiplies number in cell by a given factor, thus keeping is a number. A suffix an be added to the columns header. This is useful for tables, which all contain percentage values and where a '%' sign after each value is undesireable.\n", "```\n", "FmtMultiplyCellValue(d, suffix)\n", "```\n", "is the general purpose function, multiplying by any given factor.\n", "\n", "### Heatmaps\n", "The table formatting has a very flexible heatmap formatter.\n", "#### FmtHeatmap\n", "```\n", "FmtHeatmap(min_color=colors.HEATMAP_RED, max_color=colors.HEATMAP_GREEN, threshold=0.,axis=None)\n", "```\n", "Creates a heatmap in the intersection of specified columns and rows.\n", "```\n", ":min_color CSS color, which is the color applied as background-color at the minimum negative value\n", ":max_color CSS color, which is the color applied as background-color at the maximum positive value\n", ":threshold specifies an interval around 0, in which no heatmapping is applied\n", ":axis Number, either 0 (horizontal), or 1 (vertical) or None. If None, heatmap is applied over all selected cells. If set to a number, heatmap is applied column-wise or row-wise reprectively.\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " aa\n", " \n", " bb\n", " \n", " cc\n", " \n", " dd\n", " \n", " ee\n", " \n", " ff\n", " \n", " gg\n", " \n", " hh\n", " \n", " ii\n", " \n", " jj\n", " \n", " kk\n", " \n", " ll\n", " \n", " mm\n", " \n", " nn\n", " \n", " oo\n", "
\n", " a\n", " \n", " 0.89\n", " \n", " 0.95\n", " \n", " 0.52\n", " \n", " 0.00\n", " \n", " 0.76\n", " \n", " 0.23\n", " \n", " 0.97\n", " \n", " 0.23\n", " \n", " 0.02\n", " \n", " 0.29\n", " \n", " 0.78\n", " \n", " 0.38\n", " \n", " 0.23\n", " \n", " 0.57\n", " \n", " 0.05\n", "
\n", " b\n", " \n", " 0.17\n", " \n", " 0.93\n", " \n", " 0.70\n", " \n", " 0.71\n", " \n", " 0.48\n", " \n", " 0.77\n", " \n", " 0.08\n", " \n", " 0.06\n", " \n", " 0.22\n", " \n", " 0.99\n", " \n", " 0.73\n", " \n", " 0.36\n", " \n", " 0.60\n", " \n", " 0.60\n", " \n", " 0.63\n", "
\n", " c\n", " \n", " 0.77\n", " \n", " 0.46\n", " \n", " 0.25\n", " \n", " 0.97\n", " \n", " 0.08\n", " \n", " 0.15\n", " \n", " 0.40\n", " \n", " 0.49\n", " \n", " 0.36\n", " \n", " 0.64\n", " \n", " 0.20\n", " \n", " 0.94\n", " \n", " 0.03\n", " \n", " 0.54\n", " \n", " 0.49\n", "
\n", " d\n", " \n", " 0.93\n", " \n", " 0.28\n", " \n", " 0.28\n", " \n", " 0.47\n", " \n", " 0.07\n", " \n", " 0.17\n", " \n", " 0.28\n", " \n", " 0.25\n", " \n", " 0.74\n", " \n", " 0.65\n", " \n", " 0.20\n", " \n", " 0.79\n", " \n", " 0.81\n", " \n", " 0.82\n", " \n", " 0.32\n", "
\n", " e\n", " \n", " 0.34\n", " \n", " 0.09\n", " \n", " 0.84\n", " \n", " 0.92\n", " \n", " 0.95\n", " \n", " 0.50\n", " \n", " 0.50\n", " \n", " 0.32\n", " \n", " 0.36\n", " \n", " 0.16\n", " \n", " 0.92\n", " \n", " 0.01\n", " \n", " 0.34\n", " \n", " 0.49\n", " \n", " 0.56\n", "
\n", " f\n", " \n", " 0.44\n", " \n", " 0.10\n", " \n", " 0.63\n", " \n", " 0.61\n", " \n", " 0.86\n", " \n", " 0.09\n", " \n", " 0.31\n", " \n", " 0.97\n", " \n", " 0.71\n", " \n", " 0.87\n", " \n", " 0.09\n", " \n", " 0.23\n", " \n", " 0.03\n", " \n", " 0.73\n", " \n", " 0.71\n", "
\n", " g\n", " \n", " 0.55\n", " \n", " 0.74\n", " \n", " 0.67\n", " \n", " 0.49\n", " \n", " 0.89\n", " \n", " 0.56\n", " \n", " 0.25\n", " \n", " 0.10\n", " \n", " 0.94\n", " \n", " 0.78\n", " \n", " 0.57\n", " \n", " 0.73\n", " \n", " 0.61\n", " \n", " 0.92\n", " \n", " 0.86\n", "
\n", " h\n", " \n", " 0.21\n", " \n", " 0.15\n", " \n", " 0.95\n", " \n", " 0.53\n", " \n", " 0.47\n", " \n", " 0.48\n", " \n", " 0.27\n", " \n", " 0.94\n", " \n", " 0.02\n", " \n", " 0.09\n", " \n", " 0.29\n", " \n", " 0.65\n", " \n", " 0.55\n", " \n", " 0.28\n", " \n", " 0.56\n", "
\n", " i\n", " \n", " 0.10\n", " \n", " 0.50\n", " \n", " 0.44\n", " \n", " 0.58\n", " \n", " 0.54\n", " \n", " 0.54\n", " \n", " 0.11\n", " \n", " 0.90\n", " \n", " 0.00\n", " \n", " 0.21\n", " \n", " 0.09\n", " \n", " 0.95\n", " \n", " 0.70\n", " \n", " 0.18\n", " \n", " 0.19\n", "
\n", " j\n", " \n", " 0.55\n", " \n", " 0.86\n", " \n", " 0.17\n", " \n", " 0.73\n", " \n", " 0.79\n", " \n", " 0.51\n", " \n", " 0.47\n", " \n", " 0.17\n", " \n", " 0.67\n", " \n", " 0.77\n", " \n", " 1.00\n", " \n", " 0.29\n", " \n", " 0.90\n", " \n", " 0.45\n", " \n", " 0.91\n", "
\n", " k\n", " \n", " 0.85\n", " \n", " 0.93\n", " \n", " 0.69\n", " \n", " 0.54\n", " \n", " 0.40\n", " \n", " 0.29\n", " \n", " 0.74\n", " \n", " 0.64\n", " \n", " 0.34\n", " \n", " 0.85\n", " \n", " 0.23\n", " \n", " 0.72\n", " \n", " 0.25\n", " \n", " 0.35\n", " \n", " 0.14\n", "
\n", " l\n", " \n", " 0.14\n", " \n", " 0.83\n", " \n", " 0.89\n", " \n", " 0.46\n", " \n", " 0.36\n", " \n", " 0.98\n", " \n", " 0.68\n", " \n", " 0.57\n", " \n", " 0.53\n", " \n", " 0.10\n", " \n", " 0.55\n", " \n", " 0.67\n", " \n", " 0.96\n", " \n", " 0.24\n", " \n", " 0.43\n", "
\n", " m\n", " \n", " 0.16\n", " \n", " 0.65\n", " \n", " 0.78\n", " \n", " 0.04\n", " \n", " 0.03\n", " \n", " 0.16\n", " \n", " 0.57\n", " \n", " 0.40\n", " \n", " 0.48\n", " \n", " 0.02\n", " \n", " 0.30\n", " \n", " 0.95\n", " \n", " 0.82\n", " \n", " 0.71\n", " \n", " 0.99\n", "
\n", " n\n", " \n", " 0.05\n", " \n", " 0.79\n", " \n", " 0.58\n", " \n", " 0.84\n", " \n", " 0.58\n", " \n", " 0.76\n", " \n", " 0.61\n", " \n", " 0.06\n", " \n", " 0.32\n", " \n", " 0.47\n", " \n", " 0.03\n", " \n", " 0.37\n", " \n", " 0.75\n", " \n", " 0.73\n", " \n", " 0.24\n", "
\n", " o\n", " \n", " 0.55\n", " \n", " 0.66\n", " \n", " 0.17\n", " \n", " 0.81\n", " \n", " 0.49\n", " \n", " 0.78\n", " \n", " 0.85\n", " \n", " 0.41\n", " \n", " 0.94\n", " \n", " 0.02\n", " \n", " 0.21\n", " \n", " 0.41\n", " \n", " 0.40\n", " \n", " 0.98\n", " \n", " 0.49\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import string\n", "# Create DataFrame\n", "df_size = 15\n", "index = [c for c in string.ascii_lowercase[:df_size]]\n", "columns = [c+c for c in string.ascii_lowercase[:df_size]]\n", "df = pd.DataFrame(np.random.rand(df_size,df_size), index=index, columns=columns)\n", "\n", "# Specify heatmap formatters\n", "# All values in range\n", "fmt_heatmap1 = tf.FmtHeatmap(rows=index[10:16],columns=columns[:5])\n", "# By row\n", "fmt_heatmap2 = tf.FmtHeatmap(rows=index[:3], axis=0, max_color=(255,0,255))\n", "# By column\n", "fmt_heatmap3 = tf.FmtHeatmap(rows=index[5:],columns=columns[10:], axis=1, max_color=(255,255,0))\n", "\n", "formatters =[fmt_heatmap1, fmt_heatmap2, fmt_heatmap3]\n", "\n", "Block(df, formatters=formatters)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multi-index tables\n", "Multi-index dataframes can be expanded to sinlesimple index dataframes with special formatting applied.\n", "#### FmtExpandMultiIndex\n", "```\n", "FmtExpandMultiIndex(total_columns=None, operator=OP_SUM, bold=True, indent_px=20, hline_color=colors.DARK_BLUE, level_background_colors=None, level_text_colors=None)\n", "```\n", "See example below. Can handle non-unique indices.\n", "```\n", ":total_columns List of columns on which to apply operator at higher index levels\n", ":operator Computational operation to perform on columns, e.g. tf.OP_SUM, tf.OP_MEAN, tf.OP_NONE\n", ":bold True/False, changes higher-level font-style to bold\n", ":index_px Indentation space per level in pixels\n", ":hline_color CSS color, sets the color of the horizontal line separating higher-level rows\n", ":level_background_colors List of CSS colors with size equal to number of index-levels, background_color applied in each index-level row\n", ":level_text_colors List of CSS colors with size equal to number of index-levels, text color applied in each index-level row\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " column0 column1 column2\n", "a-level aa-level \n", "a aa 0.0 1.0 2.0\n", " ab 3.0 4.0 5.0\n", "b ba 6.0 7.0 8.0\n", " bb 9.0 10.0 11.0\n" ] }, { "data": { "text/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", " column0\n", " \n", " column1\n", " \n", " column2\n", "
\n", " a\n", " \n", " 3.0\n", " \n", " 5.0\n", " \n", " 7.0\n", "
\n", " aa\n", " \n", " 0.0\n", " \n", " 1.0\n", " \n", " 2.0\n", "
\n", " ab\n", " \n", " 3.0\n", " \n", " 4.0\n", " \n", " 5.0\n", "
\n", " b\n", " \n", " 15.0\n", " \n", " 17.0\n", " \n", " 19.0\n", "
\n", " ba\n", " \n", " 6.0\n", " \n", " 7.0\n", " \n", " 8.0\n", "
\n", " bb\n", " \n", " 9.0\n", " \n", " 10.0\n", " \n", " 11.0\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def make_multiindex_table():\n", " fmt = tf.FmtExpandMultiIndex()\n", " idx = np.array([['a', 'a', 'b', 'b'], ['aa', 'ab', 'ba', 'bb']])\n", " idx_tuples = list(zip(*idx))\n", " multi_index = pd.MultiIndex.from_tuples(idx_tuples, names=['a-level', 'aa-level'])\n", " columns = ['column0', 'column1', 'column2']\n", " data = pd.DataFrame(np.arange(12, dtype=float).reshape(4, 3), index=multi_index, columns=columns)\n", " return data\n", "\n", "mi_df = make_multiindex_table()\n", "print(mi_df)\n", "\n", "fmt_multiindex = tf.FmtExpandMultiIndex(operator=tf.OP_SUM)\n", "Block(mi_df, formatters=[fmt_multiindex], use_default_formatters=False)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Writing custom formatters\n", "Custom formatters can either be either added to pybloqs or included with user-space code. The latter is useful for very specific formatters, which have little chance of being reused and thus do not need to sit in the code base.\n", "In general, formatters are classes that inherit from ```TableFormatter``` base class. The base class provides the following function hooks, which do not need to be all implemented by the new formatter. In fact, most formatters only make use of one or two function hooks. The available hooks are:\n", "* ```_insert_additional_html```: Can be used to put HTML or JavaScript code in front of table.\n", "* ```_modify_dataframe```: Access and potentially modify DataFrame before any other hook functions become active.\n", "* ```_modify_cell_content```: Access and potentially modify cell value. Called for each cell separately. \n", "* ```create_table_level_css```: Insert CSS inline style on `````` level\n", "* ```_create_thead_level_css```: Insert CSS inline style on `````` level\n", "* ```_create_row_level_css```: Insert CSS inline style on ``````level\n", "* ```_create_cell_level_css```: Insert CSS inline style on ```
``` and ```
``` level\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 1 }