<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 HTML Tables and Table Formatting
Creating a HTML Table from pandas.DataFrame
The following is hopefully sufficient for most applications (feedback welcome!)
Step1:  NB
Step2:  General formatters
The following formatters handle miscalleneous general tasks
Replace NaN
FmtReplaceNaN(value='')
Replaces all np.nan values in specified range with provided value. By default uses empty string.
FmtAlignCellContents
FmtAlignCellContents(alignment='center')
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).
FmtAlignTable
FmtAlignTable(alignment)
Aligns the entire table relative to its entironment. Valid values for alignment are center, right, left.
FmtHeader
FmtHeader(fixed_width='100%', index_width=None, column_width=None, rotate_deg=0,top_padding=None, no_wrap=True)
Creates a table with fixed-width columns. 
Step3:  FmtStripeBackground
FmtStripeBackground(first_color=colors.LIGHT_GREY, second_color=colors.WHITE, header_color=colors.WHITE,
Creates a repeating color patters in the background of the specified cells.
Step4:  Multi-index tables
Multi-index dataframes can be expanded to sinlesimple index dataframes with special formatting applied.
FmtExpandMultiIndex
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)
See example below. Can handle non-unique indices.
<ASSISTANT_TASK:>
Python Code:
%%capture
import pybloqs as abl
from pybloqs import Block
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(4,4), index=['a','b','c','d'], columns = ['aa','bb','cc','dd'])
df.index.name = 'ABC'
Block(df)
Explanation: HTML Tables and Table Formatting
Creating a HTML Table from pandas.DataFrame
The following is hopefully sufficient for most applications (feedback welcome!):
from pybloqs import Blockb = Block(df)
When called only with DataFrame as parameter, a set of default formatters is applied: 
table_block = Block(df, formatters=None, use_default_formatters=True)
End of explanation
import pybloqs.block.table_formatters as tf
table_block = Block(df)
table_block_raw = Block(df, use_default_formatters=False)
fmt_pct = tf.FmtPercent(1, columns=['bb','cc'], apply_to_header_and_index=False)
fmt_totals = tf.FmtAppendTotalsRow(total_columns=['aa','dd'])
fmt_highlight = tf.FmtHighlightText(columns=['bb'], rows=['d'], apply_to_header_and_index=False)
formatters=[fmt_pct,fmt_totals, fmt_highlight]
table_block_additional_formatters = Block(df, formatters=formatters)
fmt_mult = tf.FmtMultiplyCellValue(1e6, '')
fmt_sep = tf.FmtThousandSeparator()
formatters=[fmt_mult, fmt_sep]
table_block_new_formatters = Block(df, formatters=formatters, use_default_formatters=False)
row1 = abl.HStack([table_block, table_block_raw])
row2 = abl.HStack([table_block_additional_formatters, table_block_new_formatters])
abl.VStack([row1,row2])
Explanation: NB: The grid between cells is from jupyter default CSS. It will not show if the block is displayed with b.show() .
Formatting Tables with Table Formatters
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.
Use of default formatters can be disabled completely. Additional formatters can be used on top or instead of default formatters.
Formatters change appearance by modifying cell values and adding CSS styles.
'Exotic' formatters, which are used only in a single context, can be defined locally.
A set of general use formatters can be found in pybloqs.block.table_formatters.
All formatters take the following parameters:
:rows List of rows, where formatter should be applied
:columns List of columns, where formatter should be applied
:apply_to_header_and_index True/False, if set to True, formatter will be applied to all index and header cells
If rows and columns are both None, formatter is applied to all cells in table.
An example:
End of explanation
fmt_header = tf.FmtHeader(fixed_width='20cm',index_width='30%', top_padding='3cm', rotate_deg=30)
Block(df, formatters=[fmt_header])
Explanation: General formatters
The following formatters handle miscalleneous general tasks
Replace NaN
FmtReplaceNaN(value='')
Replaces all np.nan values in specified range with provided value. By default uses empty string.
FmtAlignCellContents
FmtAlignCellContents(alignment='center')
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).
FmtAlignTable
FmtAlignTable(alignment)
Aligns the entire table relative to its entironment. Valid values for alignment are center, right, left.
FmtHeader
FmtHeader(fixed_width='100%', index_width=None, column_width=None, rotate_deg=0,top_padding=None, no_wrap=True)
Creates a table with fixed-width columns. 
:fixed_width Total width of table
:index_width Fixed width of index column
:column_width Fixed width of all other columns
:rotate_deg Value in degrees by which to rotate table header cells
:top_padding: additional vertical space above table, may be necessary when using rotated headers
:no_wrap True/False, disables line-breaks within header cell when set to True
An example (NB, jypiter ignores top-padding, which otherwise works in direkt browser display and PDF output):
End of explanation
import string
# Create DataFrame
df_size = 15
index = [c for c in string.ascii_lowercase[:df_size]]
columns = [c+c for c in string.ascii_lowercase[:df_size]]
df = pd.DataFrame(np.random.rand(df_size,df_size), index=index, columns=columns)
# Specify heatmap formatters
# All values in range
fmt_heatmap1 = tf.FmtHeatmap(rows=index[10:16],columns=columns[:5])
# By row
fmt_heatmap2 = tf.FmtHeatmap(rows=index[:3], axis=0, max_color=(255,0,255))
# By column
fmt_heatmap3 = tf.FmtHeatmap(rows=index[5:],columns=columns[10:], axis=1, max_color=(255,255,0))
formatters =[fmt_heatmap1, fmt_heatmap2, fmt_heatmap3]
Block(df, formatters=formatters)
Explanation: FmtStripeBackground
FmtStripeBackground(first_color=colors.LIGHT_GREY, second_color=colors.WHITE, header_color=colors.WHITE,
Creates a repeating color patters in the background of the specified cells.
:first_color CSS color, for odd row numbers
:second_color CSS color, for even row numbers
:header_color CSS color applied to header row
FmtAddCellPadding
FmtAddCellPadding(left=None, right=None, top=None, bottom=None, length_unit='px')
Add space on sides of selected cells.
FmtAppendTotalsRow
FmtAppendTotalsRow(row_name='Total', operator=OP_SUM, bold=True, background_color=colors.LIGHT_GREY, font_color=None, total_columns=None)
Adds a line at the end of the table filled with values computed columnwise. For an example, see section [Formatting Tables with Table Formatters]
:row_name Label for additional row shown in index
:operator Computational operation to perform on columns, e.g. tf.OP_SUM, tf.OP_MEAN, tf.OP_NONE
:total_columns Names of columns to apply operator to. If None, operator is applied to all columns.
:bold True/False, applied bold font-style to cells in added row
:font_color CSS color for cell text in added row
:background_color CSS color for cell background in added row
FmtHideRows
FmtHideCells(rows=None, columns=None)
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.
FmtPageBreak
FmtPageBreak(no_break=True, repeat_header=True)
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.
:no_break True/False, avoids splitting table on page break
:repeat_header True/False, when table is split accross page, repeat header on the next page
Displaying text
The following formatters modify the display of both text and numbers.
FmtFontsize
FmtFontsize(fontsize, format='px')
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.
FmtHighlightText
FmtHighlightText(bold=True, italic=True, font_color=colors.BLUE)
Sets various properties of character display.
:bold True/False, sets bold font-style
:italic True/False, sets italic font-style
:font_color CSS color, sets text color
FmtHighlightBackground
FmtHighlightBackground(color=colors.RED)
Sets the background color of specified cells.
FmtBold
FmtBold()
Sets font-style bold in specified cells.
Displaying numbers
The following formatters only apply to numbers. Please note that some convert the number to a string when applied.
FmtNumbers, FmtDecimals, FmtThousandSeparator, FmtPercent
E.g.
FmtPercent(n_decimals)
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).
FmtNumbers(fmt_string)
is the general purpose formatting class, which accets any formatting string. For more information about formatting strings, see https://pyformat.info/
FmtMultiplyCellValue, FmtValueToMillion, FmtValueToPercent, FmtValueToBps
E.g.
FmtValueToPercent(suffix='%')
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.
FmtMultiplyCellValue(d, suffix)
is the general purpose function, multiplying by any given factor.
Heatmaps
The table formatting has a very flexible heatmap formatter.
FmtHeatmap
FmtHeatmap(min_color=colors.HEATMAP_RED, max_color=colors.HEATMAP_GREEN, threshold=0.,axis=None)
Creates a heatmap in the intersection of specified columns and rows.
:min_color CSS color, which is the color applied as background-color at the minimum negative value
:max_color CSS color, which is the color applied as background-color at the maximum positive value
:threshold specifies an interval around 0, in which no heatmapping is applied
: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.
End of explanation
def make_multiindex_table():
    fmt = tf.FmtExpandMultiIndex()
    idx = np.array([['a', 'a', 'b', 'b'], ['aa', 'ab', 'ba', 'bb']])
    idx_tuples = list(zip(*idx))
    multi_index = pd.MultiIndex.from_tuples(idx_tuples, names=['a-level', 'aa-level'])
    columns = ['column0', 'column1', 'column2']
    data = pd.DataFrame(np.arange(12, dtype=float).reshape(4, 3), index=multi_index, columns=columns)
    return data
mi_df = make_multiindex_table()
print(mi_df)
fmt_multiindex = tf.FmtExpandMultiIndex(operator=tf.OP_SUM)
Block(mi_df, formatters=[fmt_multiindex], use_default_formatters=False)
Explanation: Multi-index tables
Multi-index dataframes can be expanded to sinlesimple index dataframes with special formatting applied.
FmtExpandMultiIndex
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)
See example below. Can handle non-unique indices.
:total_columns List of columns on which to apply operator at higher index levels
:operator Computational operation to perform on columns, e.g. tf.OP_SUM, tf.OP_MEAN, tf.OP_NONE
:bold True/False, changes higher-level font-style to bold
:index_px Indentation space per level in pixels
:hline_color CSS color, sets the color of the horizontal line separating higher-level rows
:level_background_colors List of CSS colors with size equal to number of index-levels, background_color applied in each index-level row
:level_text_colors List of CSS colors with size equal to number of index-levels, text color applied in each index-level row
End of explanation
<END_TASK>